把連續圖片放入 drawable 目錄 以及在該目錄建立建立一個xml 文件android
<?xml version="1.0" encoding="utf-8"?> <animation-list xmlns:android="http://schemas.android.com/apk/res/android" xmlns:oneshot="false" > <item android:drawable="@drawable/a1001" android:duration="200"/> <item android:drawable="@drawable/a1002" android:duration="200"/> <item android:drawable="@drawable/a1003" android:duration="200"/> <item android:drawable="@drawable/a1004" android:duration="200"/> <item android:drawable="@drawable/a1005" android:duration="200"/> <item android:drawable="@drawable/a1001" android:duration="200"/> <item android:drawable="@drawable/a1006" android:duration="200"/> <item android:drawable="@drawable/a1007" android:duration="200"/> <item android:drawable="@drawable/a1008" android:duration="200"/> <item android:drawable="@drawable/a1009" android:duration="200"/> <item android:drawable="@drawable/a1010" android:duration="200"/> </animation-list>
程序片斷ide
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); iv = (ImageView) findViewById(R.id.iv); iv.setBackgroundResource(R.drawable.gril_list); an = (AnimationDrawable) iv.getBackground(); } public void play(View view) { an.start(); }
##2. 程序建立動畫動畫
//平移 public void playTranslate(View view) { TranslateAnimation ta = new TranslateAnimation( Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 1.0f, Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 1.0f); ta.setDuration(2000); ta.setRepeatCount(2); ta.setRepeatMode(Animation.REVERSE); lanucher.startAnimation(ta); } //透明度 public void alpha(View view) { AlphaAnimation an = new AlphaAnimation(0.0f, 1.0f); an.setDuration(2000); an.setRepeatCount(2); an.setRepeatMode(Animation.REVERSE); lanucher.startAnimation(an); } //縮放 public void scale(View view) { ScaleAnimation sa = new ScaleAnimation(0.2f, 2.0f, 0.2f, 2.0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); sa.setDuration(2000); sa.setRepeatCount(2); sa.setRepeatMode(Animation.REVERSE); lanucher.startAnimation(sa); } //旋轉 public void rotate(View view) { RotateAnimation ra = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); ra.setDuration(2000); ra.setRepeatCount(2); ra.setRepeatMode(Animation.REVERSE); lanucher.startAnimation(ra); } //動畫集合 public void set(View view) { AnimationSet set = new AnimationSet(false); RotateAnimation ra = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); ra.setDuration(2000); ra.setRepeatCount(2); ra.setRepeatMode(Animation.REVERSE); TranslateAnimation ta = new TranslateAnimation( Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 1.0f, Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 1.0f); ta.setDuration(2000); ta.setRepeatCount(2); ta.setRepeatMode(Animation.REVERSE); lanucher.startAnimation(ta); set.addAnimation(ra); set.addAnimation(ta); lanucher.startAnimation(set); }
##3. xml配置動畫this
<?xml version="1.0" encoding="utf-8"?> <alpha xmlns:android="http://schemas.android.com/apk/res/android" android:fromAlpha="1.0" android:toAlpha="0.0" android:fillBefore="true" android:duration="2000" > </alpha>
<?xml version="1.0" encoding="utf-8"?> <rotate android:fromDegrees="0" android:toDegrees="360" android:pivotX="50%" android:pivotY="50%" android:duration="2000" xmlns:android="http://schemas.android.com/apk/res/android"> </rotate>
<?xml version="1.0" encoding="utf-8"?> <scale android:fromXScale="0.2" android:toXScale="2.0" android:fromYScale="0.2" android:toYScale="2.0" android:fillAfter="true" android:repeatCount="2" xmlns:android="http://schemas.android.com/apk/res/android"> </scale>
<?xml version="1.0" encoding="utf-8"?> <translate android:fromXDelta="20%" android:toXDelta="50%p" android:fromYDelta="20%" android:toYDelta="50%p" android:duration="2000" android:repeatCount="2" android:repeatMode="reverse" xmlns:android="http://schemas.android.com/apk/res/android"> </translate>
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> <scale android:fromXScale="0.2" android:toXScale="2.0" android:fromYScale="0.2" android:toYScale="2.0" android:fillAfter="true" android:repeatCount="2"> </scale> <rotate android:fromDegrees="0" android:toDegrees="360" android:pivotX="50%" android:pivotY="50%" android:duration="2000"> </rotate> </set>
程序片斷code
private ImageView xiv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.xml_main); xiv = (ImageView) findViewById(R.id.xiv); } //透明度 public void alpha(View view) { Animation am = AnimationUtils.loadAnimation(this, R.anim.alpha); xiv.startAnimation(am); }