zoukankan      html  css  js  c++  java
  • 5月19日学习日志

    今天学习了Evaluator自定义。

    定义一个对象Point.java,对象中只有x,y两个属性以及get,set方法~

    public class Point {
    
        private float x;
        private float y;
    
        public Point() {
        }
    
        public Point(float x, float y) {
            this.x = x;
            this.y = y;
        }
    
        public float getX() {
            return x;
        }
    
        public float getY() {
            return y;
        }
    
        public void setX(float x) {
            this.x = x;
        }
    
        public void setY(float y) {
            this.y = y;
        }
    }

    接着自定义Evaluator类:PointEvaluator.java,实现接口重写evaluate方法~

    public class PointEvaluator implements TypeEvaluator<Point>{
        @Override
        public Point evaluate(float fraction, Point startValue, Point endValue) {
            float x = startValue.getX() + fraction * (endValue.getX() - startValue.getX());
            float y = startValue.getY() + fraction * (endValue.getY() - startValue.getY());
            Point point = new Point(x, y);
            return point;
        }
    }

    然后自定义一个View类:AnimView.java,很简单~

    public class AnimView extends View {
    
        public static final float RADIUS = 80.0f;
        private Point currentPoint;
        private Paint mPaint;
    
        public AnimView(Context context) {
            this(context, null);
        }
    
        public AnimView(Context context, AttributeSet attrs) {
            super(context, attrs);
            init();
        }
    
        public AnimView(Context context, AttributeSet attrs, int defStyleAttr) {
            super(context, attrs, defStyleAttr);
        }
    
        private void init() {
            mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
            mPaint.setColor(Color.BLUE);
        }
    
        private void drawCircle(Canvas canvas){
            float x = currentPoint.getX();
            float y = currentPoint.getY();
            canvas.drawCircle(x, y, RADIUS, mPaint);
        }
    
        private void startAnimation() {
            Point startPoint = new Point(RADIUS, RADIUS);
            Point endPoint = new Point(getWidth() - RADIUS, getHeight() - RADIUS);
            ValueAnimator anim = ValueAnimator.ofObject(new PointEvaluator(), startPoint, endPoint);
            anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                @Override
                public void onAnimationUpdate(ValueAnimator animation) {
                    currentPoint = (Point) animation.getAnimatedValue();
                    invalidate();
                }
            });
            anim.setDuration(3000l);
            anim.start();
        }
    
        @Override
        protected void onDraw(Canvas canvas) {
            if (currentPoint == null) {
                currentPoint = new Point(RADIUS, RADIUS);
                drawCircle(canvas);
                startAnimation();
            } else {
                drawCircle(canvas);
            }
        }
    }

    最后MainActivity.java处实例化这个View即可~

    public class MainActivity extends AppCompatActivity {
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(new AnimView(this));
        }
    }
  • 相关阅读:
    Nginx log日志参数详解
    sea.js模块加载工具
    sea.js模块加载工具
    Airbnb React/JSX 编码规范
    4.2 react patterns(转)
    4.1 react 代码规范
    3.5 compose redux sages
    3.3 理解 Redux 中间件(转)
    3.4 redux 异步
    3.1 开始使用 redux
  • 原文地址:https://www.cnblogs.com/20193925zxt/p/14910663.html
Copyright © 2011-2022 走看看