Tuesday, January 24, 2012

Creaete AnimationDrawable and copy frames from another AnimationDrawable

Modify from ast exercise "Set alpha of AnimationDrawable", we are going to create a new AnimationDrawable, copy frame from the original myAnimationDrawable, and re-arrange frame order in reverse.

Creaete AnimationDrawable and copy frames from another AnimationDrawable

Modify main.xml to add a ImageView for the new animation.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Set Alpha" />
<SeekBar
android:id="@+id/setalpha"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:progress="255"
android:max="255" />
<Button
android:id="@+id/startanimation"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Start Animation" />
<Button
android:id="@+id/stopanimation"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Stop Animation" />
<ImageView
android:id="@+id/myanimation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@anim/anim_android"
/>
<ImageView
android:id="@+id/myanimation2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>


Modify the Java code, implement a new method copyReversedAnim(). It return the new AnimationDrawable. Then set it the ImageDrawable of the ImageView.

package com.exercise.AndroidAnimation;

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;
import android.widget.SeekBar;
import android.widget.Toast;

public class AndroidAnimationActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

ImageView myAnimation = (ImageView)findViewById(R.id.myanimation);
ImageView myAnimation2 = (ImageView)findViewById(R.id.myanimation2);
final AnimationDrawable myAnimationDrawable
= (AnimationDrawable)myAnimation.getDrawable();

//Copy a new AnimationDrawable in reversed order
final AnimationDrawable reversedAnimationDrawable = copyReversedAnim(myAnimationDrawable);
//apply the new AnimationDrawable
myAnimation2.setImageDrawable(reversedAnimationDrawable);

SeekBar setAnimationAlpha = (SeekBar)findViewById(R.id.setalpha);
Button startAnimation = (Button)findViewById(R.id.startanimation);
Button stopAnimation = (Button)findViewById(R.id.stopanimation);

startAnimation.setOnClickListener(new Button.OnClickListener(){

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
if(!reversedAnimationDrawable.isRunning()){
myAnimationDrawable.start();
reversedAnimationDrawable.start();
}
}});

stopAnimation.setOnClickListener(new Button.OnClickListener(){

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
if(reversedAnimationDrawable.isRunning()){
myAnimationDrawable.stop();
reversedAnimationDrawable.stop();
}
}});

setAnimationAlpha.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener(){

@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
myAnimationDrawable.setAlpha(progress);
reversedAnimationDrawable.setAlpha(progress);

}

@Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub

}

@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub

}});

}

private AnimationDrawable copyReversedAnim(AnimationDrawable src){

AnimationDrawable newAnim = new AnimationDrawable();

int numberOfFrame = src.getNumberOfFrames();

for(int i = 0; i < numberOfFrame; i++){
newAnim.addFrame(
src.getFrame(numberOfFrame - i - 1),
src.getDuration(numberOfFrame - i - 1));
}
newAnim.setOneShot(false);

return newAnim;
}
}


Download the files.

No comments:

Post a Comment