Frame based animation : Animation « Animation « Android






Frame based animation

    

package app.test;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.ComponentName;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import android.app.Activity;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;


 class FrameAnimationActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.frame_animations_layout);
        this.setupButton();
    }

    private void setupButton()
    {
       Button b = (Button)this.findViewById(R.id.startFAButtonId);
       b.setOnClickListener(
             new Button.OnClickListener(){
                public void onClick(View v)
                {
                   parentButtonClicked(v);
                }
             });
    }
    private void parentButtonClicked(View v)
    {
      animate();
    }    
    private void animate()
    {
       ImageView imgView = (ImageView)findViewById(R.id.imageView);
       imgView.setVisibility(ImageView.VISIBLE);
       imgView.setBackgroundResource(R.drawable.frame_animation);

       AnimationDrawable frameAnimation = 
         (AnimationDrawable) imgView.getBackground();

       if (frameAnimation.isRunning())
       {
           frameAnimation.stop();
       }
       else
       {
           frameAnimation.stop();
           frameAnimation.start();
       }
    }
}
public class Test extends Activity 
{
  Menu myMenu = null;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) 
    {
      //call the parent to attach any system level menus
      super.onCreateOptionsMenu(menu);
      
      this.myMenu = menu;

      MenuInflater mi = this.getMenuInflater();
      mi.inflate(R.menu.main_menu,menu);
      
      return true;
    }
    
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) 
    {
        handleMenus(item);
      return true;
    }
    private void handleMenus(MenuItem item)
    {
    this.appendMenuItemText(item);
    if (item.getItemId() == R.id.menu_clear)
    {
      this.emptyText();
    }
    else if (item.getItemId() == R.id.menu_list_animation)
    {
      Intent intent = new Intent(this, FrameAnimationActivity.class);
      startActivity(intent);
    }
    }
    
    private TextView getTextView()
    {
         TextView tv = 
           (TextView)this.findViewById(R.id.textViewId);
         return tv;
    }
    public void appendText(String text)
    {
         TextView tv = 
           (TextView)this.findViewById(R.id.textViewId);
         tv.setText(tv.getText() + text);
    }
    public void appendMenuItemText(MenuItem menuItem)
    {
      String title = menuItem.getTitle().toString();
         TextView tv = 
           (TextView)this.findViewById(R.id.textViewId);
         tv.setText(tv.getText() + "\n" + title + ":" + menuItem.getItemId());
    }
    private void emptyText()
    {
         TextView tv = 
           (TextView)this.findViewById(R.id.textViewId);
         tv.setText("");
    }    
}

//main.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android">
<!-- This group uses the default category. -->
<group android:id="@+id/menuGroup_Main">

    <item android:id="@+id/menu_list_animation"
        android:orderInCategory="1"
        android:title="Animate Frame" />
        
    <item android:id="@+id/menu_clear"
        android:orderInCategory="10"
        android:title="clear" />
</group>
</menu>

   
    
    
    
  








Related examples in the same category

1.Animation: fade in, fade out
2.extends Animation to create your own animation
3.Slide out down animation, bounce in down animation
4.XML for Fade in animation
5.Xml for Fade out animation
6.XML for slide out animation
7.Animation Cloning
8.Animation Loading
9.Multi Property Animation
10.Animation: push up in,push up out,push left in,push left out,fade in,fade out,hyperspace in,hyperspace out
11.Using AlphaAnimation class to do animation in code
12.Animation Interpolator
13.Using TranslateAnimation class
14.Demonstrates the seeking capability of ValueAnimator.
15.Animating by calling invalidate() from draw(),loading and drawing resources, handling onPause() in an animation