您的位置:首页 > 移动开发 > Android开发

Android动画之TranslateAnimation使用

2016-04-18 17:01 671 查看

TranslateAnimation即位移动画,很多常见APP里都带有该效果

直接上干货~~

先看下xml文件:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="1000"
android:interpolator="@android:anim/accelerate_interpolator"
android:fillAfter="true"
android:fillEnabled="true"
android:fromXDelta="0"
android:fromYDelta="0"
android:repeatCount="1"
android:repeatMode="reverse"
android:toXDelta="100%"
android:toYDelta="0" />

<!--android:toXDelta="50%"
这里填写百分比则以自己或者父控件为参照物
若填写具体数字如1000则为绝对坐标,即该手机的像素点对应的长度-->
</set>

//属性解析:

float fromXDelta,这个参数表示动画开始的点离当前View X坐标上的差值

float toXDelta,这个参数表示动画结束的点离当前View X坐标上的差值

float fromYDelta,这个参数表示动画开始的点离当前View Y坐标上的差值

float toYDelta,这个参数表示动画开始的点离当前View Y坐标上的差值

如果view在A(x,y)点 那么动画就是从B点(x+fromXDelta, y+fromYDelta)点移动到C 点(x+toXDelta,y+toYDelta)点.
如上面属性表示view的起始位置A(x,y),从A(x+0,y+0)移动到B(x+x,y+0)


Java代码调用该xml文件

/**
* 位移动画
*
* @param context
* @param view 目标view
*/
public static void startTranslateAnim(Context context, View view) {
Animation animation = AnimationUtils.loadAnimation(context, R.anim.anim_translate);
if (view != null)
view.startAnimation(animation);
}


最终效果如下:



下面看纯Java代码实现:

public static void animTranslate(View view){
//        TranslateAnimation(int fromXType, float fromXValue, int toXType, float toXValue,int fromYType, float fromYValue, int toYType, float toYValue)
//当这里的 type=Animation.ABSOLUTE时,就和上面的xml效果一样
//经测试这里的 value 取值为当前参照物的多少倍,即:
// type = Animation.RELATIVE_TO_SELF 表示x坐标移动到相对自己的0.5倍距离
// type = Animation.RELATIVE_TO_PARENT 表示x坐标移动到自己父控件x轴的0.5倍距离 即 x + 0.5ParentX

TranslateAnimation animation = new TranslateAnimation(Animation.RELATIVE_TO_SELF,0,Animation.RELATIVE_TO_SELF,0.5f,
Animation.RELATIVE_TO_SELF,0,Animation.RELATIVE_TO_SELF,0);
animation.setDuration(1000);
animation.setRepeatMode(Animation.REVERSE);
animation.setInterpolator(new AccelerateInterpolator());
animation.setFillAfter(true);
if (view != null)
view.startAnimation(animation);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  位移动画