Android —— VauleAnimator动画进阶
来源:互联网 发布:淘宝店铺设置最低折扣 编辑:程序博客网 时间:2024/05/17 04:38
博主以前写过属性动画,这里由于近期看到不错的文章有了新的体会,这里加上实例大概再讲一下喽。
这里也做一个具有回弹效果的弹力球吧^_^
参考博文
大致思路:
1.由于要对一个圆做动画,这里我们建一个带有半径的圆Piont的bean类。
public class Point { private int radius; public Point(int radius){ this.radius = radius; } public int getRadius() { return radius; } public void setRadius(int radius) { this.radius = radius; }}
2.创建一个自定义的view:
public class MyPointView extends View { private Point mCurPoint; public MyPointView(Context context) { super(context); } public MyPointView(Context context, AttributeSet attrs) { super(context, attrs); } public MyPointView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } @TargetApi(Build.VERSION_CODES.LOLLIPOP) public MyPointView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { super(context, attrs, defStyleAttr, defStyleRes); } @Override protected void onDraw(Canvas canvas) {//重写onDraw方法 super.onDraw(canvas); if (mCurPoint != null){//当当前Point有对象时,取半径画圆 Paint paint = new Paint(); paint.setAntiAlias(true); paint.setColor(Color.GREEN); paint.setStyle(Paint.Style.FILL); canvas.drawCircle(300,300,mCurPoint.getRadius(),paint); } } /** * 设置view的动画效果 */ public void doPointAnim(){ ValueAnimator animator = ValueAnimator.ofObject(new PointEvaluator(),new Point(20),new Point(200));//需要自己写PointEvaluator(重点) animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {//监听当前状态 @Override public void onAnimationUpdate(ValueAnimator animation) { mCurPoint = (Point)animation.getAnimatedValue(); invalidate();//强制刷新界面 } }); animator.setDuration(1000); animator.setInterpolator(new BounceInterpolator());//回弹插值器,以弹动的效果做出动画 animator.start(); }}
3.创建PointEvaluator。
在构造ofObject中,我们也可以知道,初始值和动画中间值的类型都是Point类型,所以PointEvaluator输入的返回类型都应该是Point类型的,先看看PointEvaluator的完整代码:
实现TypeEvaluator接口,对于ofObject的使用是很重要的部分,它用来返回不同的动画进度下的状态**
public class PointEvaluator implements TypeEvaluator<Point> { @Override public Point evaluate(float fraction, Point startValue, Point endValue) {//圆是通过半径来画的 int start = startValue.getRadius(); int end = endValue.getRadius(); int curValue = (int) (start + fraction * (end - start));//通过fraction动画进度计算当前进度下的圆的半径 return new Point(curValue);//返回Point对象 }}
4.在MainActivity中使用’
public class MainActivity extends Activity { private Button btnStart; private MyPointView mPointView; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btnStart = (Button) findViewById(R.id.btn); mPointView = (MyPointView)findViewById(R.id.myView); btnStart.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { mPointView.doPointAnim(); } }); }}
代码已经上传到博主的资源喽资源
0 0
- Android —— VauleAnimator动画进阶
- Android动画进阶—使用开源动画库nineoldandroids
- Android动画进阶—使用开源动画库nineoldandroids
- Android动画进阶—使用开源动画库nineoldandroids
- Android动画进阶—使用开源动画库nineoldandroids
- Android动画进阶—使用开源动画库nineoldandroids
- Android动画进阶—使用开源动画库nineoldandroids
- Android动画进阶—使用开源动画库nineoldandroids
- Android动画进阶—使用开源动画库nineoldandroids
- Android动画进阶—使用开源动画库nineoldandroids
- Android动画进阶—使用开源动画库nineoldandroids
- Android动画进阶—使用开源动画库nineoldandroids
- Android动画进阶—使用开源动画库nineoldandroids
- Android动画进阶—使用开源动画库nineoldandroids
- Android动画进阶—使用开源动画库nineoldandroids
- Android技术进阶的要素——Android属性动画
- 动画二 Android动画进阶—使用开源动画库nineoldandroids
- Android进阶——属性动画Property Animation详解(一)
- git之rebase和merge学习记录
- oracle数据处理需求01
- C#文件下载
- Spring学习总结(17)——Spring AOP权限管理
- ZOJ 3353 Chess Board && NEFU OJ 506(高斯消元好题)
- Android —— VauleAnimator动画进阶
- 9.9学习记录
- LINUX集群--均衡负载 LVS(一) LVS认知
- finalize的使用场景
- Java阻塞IO与非阻塞IO
- 排序算法
- 九度 OJ 1456 胜利大逃亡
- JS 关于replace第二个参数是函数
- 高性能网络编程(3)----TCP消息的接收 (B)