【安卓】自己定义基于onDraw的随意动画(不不过平移/旋转/缩放/alpha)、!

思路:

1.基于时间的显示映射。如:给定度数,显示圆弧,加上时序,就可以有圆弧动画的效果

2.给定时序。

用于驱动动画的一帧帧绘制

方案一基于ObjectAnimator。动画运作时会调用degree相应set函数(基于放射调用),即setDegree。

ObjectAnimator ani=ObjectAnimator.ofInt(myView, "degree", 0,300);
ani.start();

注:1>混编后,默认会将setDegree混掉,导致找不到函数,故混编后这样的机制会失效。

解决方法是1.proguard中防止该段代码混编(详细方法百度) 2.用法二

     2>ObjectAnimator在3.0后才支持,可使用NineOldAndroids库,效果全然一样。

方案二仍然基于ObjectAnimator。但基于回调,这样的方法未用到反射,故混编时仍ok

ObjectAnimator ani=ObjectAnimator.ofInt(myView, new Prop(), 0,300);
ani.start();

class Prop extends Property<View, Integer> { public Prop() { // TODO Auto-generated constructor stub super(Integer.class, "kk"); } @Override public void set(View object, Integer value) { // TODO Auto-generated method stub ((MyView1)object).setDegree(value); } @Override public Integer get(View object) { // TODO Auto-generated method stub return null; } };

方案三用animation提供时序。

interpolatedTime为0~1,即时间的百分比。

Animation ani=new Animation() { @Override protected void applyTransformation(float interpolatedTime, Transformation t) { // TODO Auto-generated method stub myView.setDegree((int)(interpolatedTime*300f)); } }; ani.setDuration(3000); myView.startAnimation(ani);

//===========================================================================

自己定义视图,setDegress可改变圆弧角度:

private class MyView1 extends ImageView { public int degree = 0; public MyView1(Context ct) { // TODO Auto-generated constructor stub super(ct); } @Override protected void onDraw(Canvas canvas) { // TODO Auto-generated method stub super.onDraw(canvas); Rect r = new Rect(); getLocalVisibleRect(r); canvas.drawArc(new RectF(r), 0, degree, true, pt); } public void setDegree(int degree) { this.degree = degree; invalidate(); } }

效果:

技术分享   技术分享

相关文章