Android动画(View动画效果)
main.xml,对于代码创建动画效果在image 及手势识别的日志里
1 <?xml version="1.0" encoding="utf-8"?> 2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="fill_parent" 4 android:layout_height="fill_parent" > 5 6 <Button 7 android:onClick="alphaAnim" 8 android:layout_width="fill_parent" 9 android:layout_height="wrap_content" 10 android:text="播放透明度的动画" /> 11 12 <ImageView 13 android:id="@+id/iv" 14 android:layout_width="wrap_content" 15 android:layout_height="wrap_content" 16 android:layout_centerInParent="true" 17 android:src="@drawable/ic_launcher" /> 18 19 </RelativeLayout>
动画的.xml文件
alpha.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <alpha xmlns:android="http://schemas.android.com/apk/res/android" 3 android:fromAlpha="0.0" 4 android:toAlpha="1.0" 5 android:duration="1000" 6 > 7 </alpha>
rotate.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <rotate xmlns:android="http://schemas.android.com/apk/res/android" 3 android:duration="2000" 4 android:fromDegrees="0" 5 android:repeatCount="2" 6 android:repeatMode="reverse" 7 android:toDegrees="360" > 8 9 </rotate>
scale.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <scale xmlns:android="http://schemas.android.com/apk/res/android" 3 android:fromXScale="0.5" 4 android:toXScale="2.0" 5 android:fromYScale="0.5" 6 android:toYScale="2.0" 7 android:duration="2000" 8 9 > 10 11 12 </scale>
set.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <set xmlns:android="http://schemas.android.com/apk/res/android" > 3 4 <rotate 5 xmlns:android="http://schemas.android.com/apk/res/android" 6 android:duration="2000" 7 android:fromDegrees="0" 8 android:repeatCount="2" 9 android:repeatMode="reverse" 10 android:toDegrees="360" > 11 </rotate> 12 13 <translate 14 xmlns:android="http://schemas.android.com/apk/res/android" 15 android:duration="2000" 16 android:fromXDelta="0" 17 android:fromYDelta="0" 18 android:toXDelta="50%p" 19 android:toYDelta="40%p" > 20 </translate> 21 22 <scale 23 xmlns:android="http://schemas.android.com/apk/res/android" 24 android:duration="2000" 25 android:fromXScale="0.5" 26 android:fromYScale="0.5" 27 android:toXScale="2.0" 28 android:toYScale="2.0" > 29 </scale> 30 31 </set>
trans.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <translate xmlns:android="http://schemas.android.com/apk/res/android" 3 4 android:fromXDelta="0" 5 android:toXDelta="50%p" 6 android:fromYDelta="0" 7 android:toYDelta="40%p" 8 android:duration="2000" 9 10 > 11 12 13 </translate>
Activity代码
1 package cn.itcast.viewanim; 2 3 import android.app.Activity; 4 import android.os.Bundle; 5 import android.view.View; 6 import android.view.animation.AlphaAnimation; 7 import android.view.animation.Animation; 8 import android.view.animation.AnimationUtils; 9 import android.view.animation.ScaleAnimation; 10 import android.view.animation.TranslateAnimation; 11 import android.widget.ImageView; 12 13 public class DemoActivity extends Activity { 14 private ImageView iv; 15 @Override 16 public void onCreate(Bundle savedInstanceState) { 17 super.onCreate(savedInstanceState); 18 setContentView(R.layout.main); 19 iv = (ImageView) findViewById(R.id.iv); 20 } 21 22 public void alphaAnim(View view){ 23 24 //Animation a = AnimationUtils.loadAnimation(this, R.anim.set); 25 26 AlphaAnimation aa = new AlphaAnimation(0.0f, 1.0f); 27 aa.setDuration(2000); 28 //TranslateAnimation ta = new TranslateAnimation(fromXDelta, toXDelta, fromYDelta, toYDelta); 29 //ScaleAnimation sa = new ScaleAnimation(0.0f, 1.0f, 0.0f, 1.0f); 30 iv.startAnimation(aa); 31 32 } 33 }
基础/day08/viewanim