標簽:動畫 tween android animation 縮放
tween有四种動畫效果:alpha(透明)、rotate(旋轉), translate(移動),scale(縮放);
可以通過硬編碼和xml文件這兩種方式來實現。
xml實現:
第一步:在項目的res文件下面新建一個文件夾名字是anim(必須)
第二步:在anim文件夹下面新建新的xml文件,在xml文件中具体设置動畫效果
第三步:在Activity中使用 AnimationUtils.loadAnimation(MainActivity.this,R.anim.xx);來獲取。
1、alpha
alpha的xml配置文件:
<?xml version="1.0" encoding="utf-8"?> <alpha xmlns:android="http://schemas.android.com/apk/res/android" android:duration="5000" android:fillAfter="true" android:fromAlpha="1.0" android:toAlpha="0.0" > </alph
duration:执行这个動畫要用的时间
fillAfter:動畫执行结束后是否停留在動畫的最后一帧,默认为false,当動畫结束后图片会回到开始的位置
fromAlpha:開始時的透明度
toAlpha:結束時的透明度
alpha的View:
<ImageView android:id="@+id/iv_alpha" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic_launcher" /> <Button android:id="@+id/bt_alpha" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/iv_alpha" android:text="透明" />成員變量:
private ImageView ivalpha, ivrotate, ivtranslate, ivscale; private Animation alphaAnimation, rotateAnimation, translateAnimation, scaleAnimation; private Button btalpha, btrotate, bttranslate, btscale;
btalpha的點擊事件:
alphaAnimation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.alpha); ivalpha.startAnimation(alphaAnimation);
2、scale
scale的xml文件:
<?xml version="1.0" encoding="utf-8"?> <scale xmlns:android="http://schemas.android.com/apk/res/android" android:fromXScale="1" android:fromYScale="1" android:toXScale="0" android:toYScale="0" android:duration="5000"> </scale>
fromXScale、fromYScale:動畫开始时X轴和Y轴的縮放比例,比如:fromXScale为0.5,fromYScale为2,那么图片的宽就变为原来的一半,高为原来的2倍然后再开始縮放。
toXScale、toYScale: 结束时的图片X轴和Y轴的縮放比例.
編碼實現:
1、創建Animation對象
2、设置動畫的参数
3、ImageView設置setAnimation
translate
<ImageView android:id="@+id/iv_translate" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_below="@id/bt_alpha" android:src="@drawable/ic_launcher" /> <Button android:id="@+id/bt_translate" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_below="@id/iv_translate" android:text="移动" />
translateAnimation=new TranslateAnimation(0, 100, 0, 100); translateAnimation.setDuration(2000); ivtranslate.setAnimation(translateAnimation);
TranslateAnimation(float fromXDelta, float toXDelta, float fromYDelta, float toYDelta)fromXDelta:開始的X軸坐標位置
toXDelta:結束時x軸坐標的位置
fromYDelta:開始的y軸坐標位置
toYDelta:結束時y軸坐標的位置
这是一个图片移动的動畫效果。
rotate
rotateAnimation=new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); rotateAnimation.setDuration(2000); rotateAnimation.setFillAfter(true); ivrotate.setAnimation(rotateAnimation);
new RotateAnimation(fromDegrees, toDegrees, pivotXType, pivotXValue, pivotYType, pivotYValue);
toDegrees:旋轉到什麽角度
pivotXType:以哪裏爲參考旋轉,在這裏是圖片本身
pivotXValue:以本身哪個點開始旋轉,這裏是圖片寬度的一半,也就是圖片中心點。
通過編碼和xml文件兩種方式實現tween動畫,布布扣,bubuko.com
標簽:動畫 tween android animation 縮放
原文地址:http://blog.csdn.net/zhong1113/article/details/25829985