android.graphics.drawable
bitmap is collection of pixels,shape is collection of line drawings.Android supports three bitmap image file formats: PNG, JPEG, and GIF.
Truecolor PNG32 images use a full 32 bits of data for each of the image pixels to represent the four image data channels that are in most bitmap images: alpha, red,green,and blue (RGBA).
Stay away from using GIFs for Android apps. Use PNG8 instead.
(1)Frame Animation(逐帧动画)
fps:frames per second.
use android:drawable in <item>,use android:src in others.
/drawable/logo_animation.xml
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="false">
<item android:drawable="@drawable/mtlogo0" android:duration="200"/>
<item android:drawable="@drawable/mtlogo1" android:duration="200"/>
<item android:drawable="@drawable/mtlogo2" android:duration="200"/>
<item android:drawable="@drawable/mtlogo3" android:duration="200"/>
<item android:drawable="@drawable/mtlogo4" android:duration="200"/>
<item android:drawable="@drawable/mtlogo5" android:duration="200"/>
<item android:drawable="@drawable/mtlogo6" android:duration="200"/>
<item android:drawable="@drawable/mtlogo7" android:duration="200"/>
<item android:drawable="@drawable/mtlogo8" android:duration="200"/>
<item android:drawable="@drawable/mtlogo9" android:duration="200"/>
</animation-list>
android:oneshot attribute set to true, which will prevent our animation from looping continuously. false will run the animation seamlessly as a loop.the duration of the frame display time is in milliseconds (ms).
animation_frame.xml is:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView
android:id="@+id/iv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
public class FrameAnimationActivity extends Activity {
private AnimationDrawable logoAnimation;
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
this.setContentView(R.layout.animation_frame);
//logoImage is the bridge between logo_animation and iv1
ImageView logoImage=(ImageView)findViewById(R.id.iv1);
logoImage.setBackgroundResource(R.drawable.logo_animation);
logoAnimation=(AnimationDrawable)logoImage.getBackground();
}
public boolean onTouchEvent(MotionEvent event){
if(event.getAction()==MotionEvent.ACTION_DOWN){
//the logoAnimation object is sent a start() method trigger.
logoAnimation.start();
return true;
}
return super.onTouchEvent(event);
}
}
(2)Tween Animation
Tween animation,you define the start and end positions of the shape, and Android fills in the gaps to make the animation work.tween animation is more powerful than frame-based animation.Tween animation goes in the /res/anim path.Frame animation goes in the /res/drawable path.
res/anim/text_animation.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false">
<scale android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:fromXScale="1.0"
android:toXScale="1.4"
android:fromYScale="1.0"
android:toYScale="0.6"
android:pivotX="0.5"
android:pivotY="0.5"
android:fillAfter="false"
android:duration="7000"/>
<set android:interpolator="@android:anim/decelerate_interpolator">
<scale
android:fromXScale="1.4"
android:toXScale="0"
android:fromYScale="0.6"
android:toYScale="0"
android:pivotX="0.5"
android:pivotY="0.5"
android:startOffset="7000"
android:duration="4000"
android:fillBefore="false"/>
<rotate
android:fromDegrees="0"
android:toDegrees="-45"
android:toYScale="0.0"
android:pivotX="50%"
android:pivotY="50%"
android:startOffset="7000"
android:duration="4000" />
</set>
</set>
animation_tween.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:tag="tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tween Animation"/>
</LinearLayout>
public class TweenAnimationActivity extends Activity {
private View rootView;
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
rootView=LayoutInflater.from(this).inflate(R.layout.animation_tween, null);
this.setContentView(rootView);
//tv is the bridge between text_animation and iv1
TextView tv=(TextView)rootView.findViewWithTag("tv1");
Animation anim=AnimationUtils.loadAnimation(this, R.anim.text_animation);
tv.startAnimation(anim);
}
}
(3)Using TransitionDrawable class
/drawable/image_transition.xml
<?xml version="1.0" encoding="utf-8"?>
<transition xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/image1"></item>
<item android:drawable="@drawable/image2"></item>
</transition>
the ImageView object to hold our image transition.
/layout/animation_transition.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView
android:tag="iv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/image1"/>
</LinearLayout>
public class TranAnimationActivity extends Activity {
private View rootView;
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
rootView=LayoutInflater.from(this).inflate(R.layout.animation_trasition, null);
this.setContentView(rootView);
TransitionDrawable trans=(TransitionDrawable) this.getResources().getDrawable(R.drawable.image_transition);
ImageView iv=(ImageView)rootView.findViewWithTag("iv1");
iv.setImageDrawable(trans);
trans.startTransition(10000);
}
}
(4)VideoView
/layout/animation_video.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<VideoView
android:tag="video1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
public class VieoAnimationActivity extends Activity {
private View rootView;
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
rootView=LayoutInflater.from(this).inflate(R.layout.animation_video, null);
this.setContentView(rootView);
Uri uri=Uri.parse("");
VideoView vv=(VideoView)rootView.findViewWithTag("video1");
vv.setVideoURI(uri);
vv.setMediaController(new MediaController(this));
vv.start();
}
}