属性动画05-ObjectAnimator的高级使用
来源:互联网 发布:linux环境变量的作用 编辑:程序博客网 时间:2024/04/30 17:39
如果看了objectanimator(真正的劳动者)和TypeEvaluator的使用那么你对自定义TypeEvaluator和对任意属性做动画这两个概念就应该很熟悉了.下面这个例子就是对二者的综合使用.
我们知道补间动画的效果很局限于移动,缩放,旋转,和淡入淡出这四种效果.通过ObjectAnimator我们可以完成对任意属性设置动画效果,通过TypeEvaluator我们可以改变动画的效果显示状态.
需求:移动小球,并改变小球颜色.
blic class MyView extends View { private Paint mPaint; private Point mPoint; private String color; public String getColor() { return color; } public void setColor(String color) { this.color = color; mPaint.setColor(Color.parseColor(color)); invalidate(); } public MyView(Context context) { super(context); init(); } public MyView(Context context, AttributeSet attrs) { super(context, attrs); init(); } public MyView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(); } private void init() { mPaint = new Paint(Paint.ANTI_ALIAS_FLAG); mPaint.setColor(Color.BLUE); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); if (mPoint == null) { mPoint = new Point(50, 50); drawCircle(canvas); startAnimation(); } else { drawCircle(canvas); } } private void drawCircle(Canvas canvas) { canvas.drawCircle(mPoint.getX(), mPoint.getY(), 50, mPaint); } private void startAnimation() { Point startPoint = new Point(50, 50); Point endPoint = new Point(getWidth() - 50, getHeight() - 50); ValueAnimator animator = ValueAnimator .ofObject(new PointEvaluator(), startPoint, endPoint); animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { @Override public void onAnimationUpdate(ValueAnimator animation) { mPoint = ((Point) animation.getAnimatedValue()); LogUtil.i(mPoint.getX() + "------" + mPoint.getY()); invalidate(); } }); ObjectAnimator colorAnimator = ObjectAnimator.ofObject(this, "color", new ColorEvaluator(), "#0000ff", "#ff0000"); AnimatorSet animatorSet = new AnimatorSet(); animatorSet.play(animator).with(colorAnimator); animatorSet.setDuration(5000); animatorSet.start(); }}
颜色的控制
public class ColorEvaluator implements TypeEvaluator<String> { private int currentRed = -1; private int currentGreen = -1; private int currentBlue = -1; @Override public String evaluate(float fraction, String startValue, String endValue) { int startRed = Integer.parseInt(startValue.substring(1, 3), 16); int startGreen = Integer.parseInt(startValue.substring(3, 5), 16); int startBlue = Integer.parseInt(startValue.substring(5, 7), 16); int endRed = Integer.parseInt(endValue.substring(1, 3), 16); int endGreen = Integer.parseInt(endValue.substring(3, 5), 16); int endBlue = Integer.parseInt(endValue.substring(5, 7), 16); if (currentRed == -1) { currentRed = startRed; } if (currentBlue == -1) { currentBlue = startBlue; } if (currentGreen == -1) { currentGreen = startGreen; } int diffRed = Math.abs(startRed - endRed); int diffGreen = Math.abs(startGreen - endGreen); int diffBlue = Math.abs(startBlue - endBlue); if (currentRed != endRed) { currentRed = getCurrentColor(startRed, endRed, diffRed, 0, fraction); } else if (currentGreen != endGreen) { currentGreen = getCurrentColor(startGreen, endGreen, diffGreen, diffRed, fraction); } else if (currentBlue != endBlue) { currentBlue = getCurrentColor(startBlue, endBlue, diffBlue, diffRed + diffGreen, fraction); } String currentColor = "#" + getHexString(currentRed) + getHexString(currentGreen) + getHexString(currentBlue); LogUtil.i(currentColor); return currentColor; } private int getCurrentColor(int startColor, int endColor, int diffColor, int offset, float fraction) { int currentColor = 0; if (startColor > endColor) { currentColor = (int) (startColor - (fraction * diffColor - offset)); if (currentColor < endColor) { currentColor = endColor; } } else { currentColor = (int) (startColor + (fraction * diffColor - 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; }}
效果:
0 0
- 属性动画05-ObjectAnimator的高级使用
- 属性动画ValueAnimator和ObjectAnimator的使用
- Android 属性动画ValueAnimator和ObjectAnimator的高级用法
- Android属性动画,ValueAnimator和ObjectAnimator的高级用法
- Android 属性动画ValueAnimator和ObjectAnimator的高级用法
- Android属性动画(一):ValueAimator和ObjectAnimator的基本使用
- Android 属性动画valueAnimator和objectAnimator的使用
- Android中属性动画2----ObjectAnimator监听的使用
- Android属性动画之ObjectAnimator使用
- Android属性动画完全解析(中),ValueAnimator和ObjectAnimator的高级用法
- Android属性动画完全解析(中),ValueAnimator和ObjectAnimator的高级用法
- Android属性动画完全解析(中),ValueAnimator和ObjectAnimator的高级用法
- Android属性动画完全解析(中),ValueAnimator和ObjectAnimator的高级用法
- Android属性动画完全解析(中),ValueAnimator和ObjectAnimator的高级用法
- Android属性动画完全解析(中),ValueAnimator和ObjectAnimator的高级用法
- Android属性动画完全解析(中),ValueAnimator和ObjectAnimator的高级用法
- Android属性动画完全解析(中),ValueAnimator和ObjectAnimator的高级用法
- Android属性动画完全解析(中),ValueAnimator和ObjectAnimator的高级用法
- 搜索入门(DFS)最简单的部分和问题
- poj_1850 Code(组合数学/dfs)
- 11月28日 课堂笔记
- 【Leetcode】318. Maximum Product of Word Lengths
- 阅读资料
- 属性动画05-ObjectAnimator的高级使用
- 常见对象_把字符串中的数字排序案例
- 基于maven配置jetty运行
- TMS320F28335/ <2> CCS5.5调试过程如何查看GPIO端口的状态
- 半结构化、结构化以及非结构化数据
- 链表
- Nsight Monitor无法正常打开
- 51 NOD 1521 一维战舰(并查集)
- HBase的WAL机制