ValueAnimator.ofObject的使用
2016-04-27 13:39
621 查看
ValueAnimator.ofObject的使用
1.activity_anim.xml文件
2.自定义PointView控件
在MainActivity文件中
Point类
1.activity_anim.xml文件
<?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" android:orientation="vertical"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/btn" android:text="开始"/> <www.weshared.anim.PointView android:id="@+id/ponitview" android:layout_width="200dp" android:layout_height="200dp" android:background="@android:color/transparent" android:layout_centerInParent="true" /> </RelativeLayout>
2.自定义PointView控件
public class PointView extends TextView { private Paint mPaint; private int mColor = Color.TRANSPARENT; private Point point; private boolean isFirst; public PointView(Context context) { super(context); init(); } public PointView(Context context, AttributeSet attrs) { super(context, attrs); init(); } public PointView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(); } /** * 初始化画笔,设置颜色为蓝色 */ private void init() { mColor = Color.CYAN; mPaint = new Paint(); //抗锯齿 mPaint.setAntiAlias(true); mPaint.setColor(mColor); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); int height = getHeight(); int width = getWidth(); int initR = Math.min(width, height) / 2; if (!isFirst) { //初始化点 point = new Point(initR); isFirst = true; } //画圆 canvas.drawCircle(width / 2, height / 2, point.getR(), mPaint); } public void showCharAnim() { ValueAnimator animator = ValueAnimator.ofObject(new CharEvaluator(), new Character('A'), new Character('z')); animator.setDuration(5000).start(); animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { @Override public void onAnimationUpdate(ValueAnimator animation) { char animatedValue = (char) animation.getAnimatedValue(); //将文字展示到控件上 PointView.this.setText(String.valueOf(animatedValue)); PointView.this.setTextSize(30); PointView.this.setTextColor(Color.RED); invalidate(); } }); } public void showColorAnim(){ ValueAnimator animator = ValueAnimator.ofArgb(0xFFFF0000, 0xFF0000FF, 0xFFFF0000, 0xFF0000FF); animator.setDuration(5000).start(); animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { @Override public void onAnimationUpdate(ValueAnimator animation) { int animatedValue = (int) animation.getAnimatedValue(); PointView.this.setTextColor(animatedValue); invalidate(); } }); } public void showPointAnim() { float width = getMeasuredWidth(); float height = getMeasuredHeight(); float r = Math.min(width, height) / 2; //用ObjectAnimator也可以实现,不过和属性值无关了,ObjectAnimator是ValueAnimator的子类,实际上还是通过addUpdateListener执行了ValueAnimator动画 // ObjectAnimator animator = ObjectAnimator.ofObject(this, "---", new PointEvaluator(), new Point(r * 0.1f), new Point(r * 1.2f), new Point(r * 0.3f), new Point(r)); ValueAnimator animator = ValueAnimator.ofObject(new PointEvaluator(), new Point(r), new Point(r * 0.2f), new Point(r * 1.2f), new Point(r * 0.2f), new Point(r)); animator.setInterpolator(new OvershootInterpolator()); animator.setDuration(5000).start(); animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { @Override public void onAnimationUpdate(ValueAnimator animation) { point = (Point) animation.getAnimatedValue(); invalidate(); } }); } public class PointEvaluator implements TypeEvaluator<Point> { @Override public Point evaluate(float fraction, Point startValue, Point endValue) { //拿到point的半径值 float startValueR = startValue.getR(); float endValueR = endValue.getR(); float currentValueR = startValueR + (endValueR - startValueR) * fraction; return new Point(currentValueR); } } public class CharEvaluator implements TypeEvaluator<Character> { @Override public Character evaluate(float fraction, Character startValue, Character endValue) { int start = startValue; int end = endValue; char current = (char) (start + (end - start) * fraction); return current; } } }
在MainActivity文件中
public class AnimActivity extends AppCompatActivity { private Button mButton; private PointView mPonitView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); initView(); initListener(); } private void initView() { setContentView(R.layout.activity_anim); mButton = (Button) findViewById(R.id.btn); mPonitView = (PointView) findViewById(R.id.ponitview); } private void initListener() { mButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { mPonitView.showPointAnim(); mPonitView.showCharAnim(); mPonitView.showColorAnim(); } }); } }
Point类
public class Point { public float r; public float x; public float y; public Point(float r){ this.r=r; } public Point(float x, float y,float r){ this.x=x; this.y=y; this.r=r; } public void setR(float r) { this.r = r; } public void setX(float x) { this.x = x; } public void setY(float y) { this.y = y; } public float getR() { return r; } public float getX() { return x; } public float getY() { return y; } }
相关文章推荐
- 论文笔记之:Active Object Localization with Deep Reinforcement Learning
- qt qobject_cast用法
- 深入理解Objective-C的Runtime机制
- Objective C的runtime
- objectarx 卸载加载arx模块
- [Java] Object类
- IOS开发之NSDictionary里setValue:forKey与setObject:forKey、objectForKey与valueF...
- 刨根问底Objective-C Runtime(4)- 成员变量与属性
- 刨根问底Objective-C Runtime(3)- 消息 和 Category
- 刨根问底Objective-C Runtime(1)- Self & Super
- Java 对象流ObjectInputStream和ObjectOutputStream类
- 图解Objectvie-C对象模型
- NSObject的load和initialize方法的加载时机和super关系
- 手机浏览器不支持 IDBObjectStore.getAll
- 缺少动态连接库.so--cannot open shared object file: No such file or directory
- 缺少动态连接库.so--cannot open shared object file: No such file or directory
- oc开发笔记3 录音时频率获取 以及声像显示
- 【转】object标签和embed标签
- Objective-C-内存管理相关知识
- Foundation => Objective-C _ Part3