自定义动画,高级动画的实现

package com.example.administrator.myanimation4;

import android.animation.ValueAnimator;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


    }
}

我们知道颜色都是由ARGB组成,A表示alpha透明度,R表示red红色,G表示green绿色,B表示blue蓝色。在动画中,颜色的变化从R-G-B。

package com.example.administrator.myanimation4;

import android.animation.TypeEvaluator;

/**
 * Created by hd on 2015/12/19.
 */

/**实现颜色变化的动画接口
 */
public class ColorEvaluator implements TypeEvaluator {

    private int mCurrentRed = -1;

    private int mCurrentGreen = -1;

    private int mCurrentBlue = -1;

    @Override
    public Object evaluate(float fraction, Object startValue, Object endValue) {
        String startColor = (String) startValue;
        String endColor = (String) endValue;
        //抽取开始颜色和结束颜色的rgb的值
        int startRed = Integer.parseInt(startColor.substring(1, 3), 16);
        int startGreen = Integer.parseInt(startColor.substring(3, 5), 16);
        int startBlue = Integer.parseInt(startColor.substring(5, 7), 16);
        int endRed = Integer.parseInt(endColor.substring(1, 3), 16);
        int endGreen = Integer.parseInt(endColor.substring(3, 5), 16);
        int endBlue = Integer.parseInt(endColor.substring(5, 7), 16);
        //初始化颜色,仅执行一次
        if(mCurrentRed == -1){
            mCurrentRed = startRed;
        }
        if(mCurrentGreen == -1){
            mCurrentGreen = startGreen;
        }
        if(mCurrentBlue == -1){
            mCurrentBlue = startBlue;
        }
        //取出开始颜色与结束颜色之间rgb的绝对值,也就是色差
        int redDiff = Math.abs(startRed - endRed);
        int greenDiff = Math.abs(startGreen - endGreen);
        int blueDiff = Math.abs(startBlue - endBlue);
        //总色差
        int colorDiff = redDiff+greenDiff+blueDiff;
        if(mCurrentRed != endRed){
            mCurrentRed = getCurrentColor(startRed,endRed,colorDiff,0,fraction);
        } else if (mCurrentGreen != endGreen) {
            mCurrentGreen = getCurrentColor(startGreen, endGreen, colorDiff, redDiff, fraction);
        } else if (mCurrentBlue != endBlue) {
            mCurrentBlue = getCurrentColor(startBlue, endBlue, colorDiff, redDiff + greenDiff, fraction);
        }
        String currentColor = "#" + getHexString(mCurrentRed) + getHexString(mCurrentGreen) + getHexString(mCurrentBlue);
        return currentColor;
    }
        //计算接下去要变化的颜色
    private int getCurrentColor(int startColor, int endColor, int colorDiff, int offset, float fraction) {
        int currentColor;
         //如果开始颜色的值大于结束颜色的值,也就是从深变浅
        if (startColor > endColor) {
            currentColor = (int) (startColor - (fraction * colorDiff - offset));
            if(currentColor<endColor){
                currentColor = endColor;
            }
        }else {
         //如果开始颜色的值小于结束颜色的值,也就是从浅变深
            currentColor = (int) (startColor + (fraction * colorDiff - offset));
            if(currentColor>endColor){
                currentColor = endColor;
            }
        }
        return  currentColor;
    }
        //把十进制转成十六进制
    private String getHexString(int Value){
        String hexString = Integer.toHexString(Value);
        if(hexString.length() == 1){
            hexString = "0" +hexString;
        }
        return  hexString;

    }


}
package com.example.administrator.myanimation4;

/**
 * Created by hd on 2015/12/19.
 */

//点的位置
public class Point {

    private float x;

    private float y;

    public Point(float x, float y) {
        this.x = x;
        this.y = y;
    }

    public float getX() {
        return x;
    }

    public void setX(float x) {
        this.x = x;
    }

    public float getY() {
        return y;
    }

    public void setY(float y) {
        this.y = y;
    }
}
package com.example.administrator.myanimation4;

import android.animation.TypeEvaluator;
import android.util.Log;

/**
 * Created by hd on 2015/12/19.
 */
// 点移动,形成移动的动画
public class PointEvaluator implements TypeEvaluator {

    @Override
    public Object evaluate(float fraction, Object startValue, Object endValue) {
        Point startPoint = (Point) startValue;
        Point endPoint = (Point) endValue;
        float x = startPoint.getX() + fraction * (endPoint.getX() - startPoint.getX());
        float y = startPoint.getY() + fraction * (endPoint.getY() - startPoint.getY());
        Point point = new Point(x, y);
        Log.i("hhhd", "fraction is " + fraction);
        return point;
    }
}
package com.example.administrator.myanimation4;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

/**
 * Created by hd on 2015/12/19.
 */
public class PointView extends View {

    private Paint mPaint;

    private Point currentPoint;

    public static final float RADIUS = 50f;

    public PointView(Context context, AttributeSet attrs) {
        super(context, attrs);
        //初始化画笔
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint.setColor(Color.RED);
    }

    @Override
    protected void onDraw(Canvas canvas) {
           if(currentPoint == null){
               currentPoint = new Point(RADIUS, RADIUS);
               drawView(canvas);
               startAnimation();
           }else {
               drawView(canvas);
           }
    }

    private void startAnimation() {
        Point startPoint = new Point(RADIUS, RADIUS);
        Point endPoint = new Point(getWidth() - RADIUS, getHeight() - RADIUS);
        ValueAnimator animator = ValueAnimator.ofObject(new PointEvaluator(), startPoint, endPoint);
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                currentPoint = (Point) animation.getAnimatedValue();
                invalidate();
            }
        });
        ObjectAnimator animator2 = ObjectAnimator.ofObject(this, "color", new ColorEvaluator(), "#0000FF", "#FF0000");
        AnimatorSet animatorSet = new AnimatorSet();
        animatorSet.play(animator).with(animator2);
        animatorSet.setDuration(5 * 1000);
        animatorSet.start();
    }

    private void drawView(Canvas canvas) {
        float x = currentPoint.getX();
        float y = currentPoint.getY();
        canvas.drawCircle(x, y, RADIUS, mPaint);
    }

    private String color;

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
        mPaint.setColor(Color.parseColor(color));
        invalidate();
    }
}

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.example.administrator.myanimation4.PointView
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</RelativeLayout>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值