Android(安卓)椭圆轨迹动画
用Path属性记录椭圆的轨迹:
Path path = new Path(); RectF rectF = new RectF(left, top, right, bottom); path.addOval(rectF, Path.Direction.CW);
API21以上可直接用属性动画里的方法(5.0以上)
ObjectAnimator traslateAnimator = ObjectAnimator.ofFloat(imageView, "x", "y", path); traslateAnimator.start();
如果API为21以下就要通过PathMeasure:
package com.wyt.hcy.trajectoryanimationdemo;import android.animation.ObjectAnimator;import android.graphics.Path;import android.graphics.PathMeasure;import android.graphics.RectF;import android.util.Log;import android.widget.ImageView;import com.nineoldandroids.animation.ValueAnimator;/** * Created by hcy on 2017/7/14 0014. * * func 动画工具类 */public class AnimationUtils { private static float[] mCurrentPosition = new float[2]; /** * 圆形轨迹动画 */ public static void circleAnimation(float left, float top, float right, float bottom, final ImageView imageView) { Path path = new Path(); RectF rectF = new RectF(left, top, right, bottom); path.addOval(rectF, Path.Direction.CW); final PathMeasure mPathMeasure = new PathMeasure(path, true); ValueAnimator valueAnimator = ValueAnimator.ofFloat(0, mPathMeasure.getLength()); valueAnimator.setDuration(5000); valueAnimator.setRepeatCount(ValueAnimator.INFINITE); valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { @Override public void onAnimationUpdate(ValueAnimator animation) { // 当插值计算进行时,获取中间的每个值, // 这里这个值是中间过程中的曲线长度(下面根据这个值来得出中间点的坐标值) float value = (Float) animation.getAnimatedValue(); Log.i("Value::", value + ""); // ★★★★★获取当前点坐标封装到mCurrentPosition // boolean getPosTan(float distance, float[] pos, float[] tan) : // 传入一个距离distance(0<=distance<=getLength()),然后会计算当前距 // 离的坐标点和切线,pos会自动填充上坐标,这个方法很重要。 mPathMeasure.getPosTan(value, mCurrentPosition, null);//mCurrentPosition此时就是中间距离点的坐标值 // 移动的商品图片(动画图片)的坐标设置为该中间点的坐标 imageView.setTranslationX(mCurrentPosition[0]); imageView.setTranslationY(-mCurrentPosition[1]); } }); valueAnimator.start(); }}
更多相关文章
- android 获得当前view在屏幕的坐标
- Android属性动画源代码解析(超详细)
- ViewPager的切换动画
- Android(安卓)动画资源
- Android(安卓)使用动画效果后的控件位置处理 类似系统通知栏下拉
- Android中的图片处理——色彩、形状拉伸变化(Matrix,ColorMatrix)
- Android(安卓)仿今日头条频道管理(上)(GridView之间Item的移动和拖
- Android实现左滑退出Activity(完美封装)
- 一次偶然机遇找到一个不错的关于Android实现加载gif动画的实例,写