在做android APP开发时,我们需要自定义View和自定义动画,例如:

实现过程为
代码:

package com.example.administrator.view;import android.content.Context;import android.graphics.Canvas;import android.graphics.Paint;import android.util.AttributeSet;import android.util.Log;import android.view.View;import android.view.animation.Animation;import android.view.animation.LinearInterpolator;import android.view.animation.Transformation;/** * Created by HBL on 2016/2/1. */public class AmountView extends View {    Paint paint;    int count = 120; //总刻度数    int tempcount = 0;    private BarAnimation anim;    public AmountView(Context context, AttributeSet attrs) {        super(context, attrs);        paint = new Paint();        paint.setColor(0xff388e3c);        paint.setStrokeJoin(Paint.Join.ROUND);        paint.setStrokeCap(Paint.Cap.ROUND);        paint.setStrokeWidth(3);        paint.setAntiAlias(true);        paint.setStyle(Paint.Style.STROKE);        anim = new BarAnimation();    }    public AmountView(Context context) {        super(context);    }    @Override    protected void onDraw(Canvas canvas) {        super.onDraw(canvas);        canvas.translate(canvas.getWidth() / 2, canvas.getHeight() / 2);        Paint tmpPaint = new Paint(paint); //小刻度画笔        tmpPaint.setStrokeWidth(2);        float y = 180;        for (int i = 0; i < tempcount; i++) {            canvas.drawLine(0f, y, 0f, y + 45f, tmpPaint);            canvas.rotate(360 / count, 0f, 0f); //旋转画纸            Log.d("AmountView", "canvas.restore();" + i);        }    }    public void startAnim() {        this.startAnimation(anim);    }    public class BarAnimation extends Animation {        @Override        protected void applyTransformation(float interpolatedTime, Transformation t) {            super.applyTransformation(interpolatedTime, t);            if (interpolatedTime < 1.0f) {                tempcount = (int) (count * interpolatedTime);                postInvalidate();                Log.d("AmountView", "tempcount:" + tempcount);            } else {                tempcount=count;            }        }        @Override        public void initialize(int width, int height, int parentWidth, int parentHeight) {            super.initialize(width, height, parentWidth, parentHeight);            setDuration(700);            //设置动画结束后保留效果            setFillAfter(true);            tempcount = 0;            setInterpolator(new LinearInterpolator());        }    }}

http://wang-peng1.iteye.com/blog/572886

更多相关文章

  1. android 属性动画
  2. Android 7.0 如何去掉灭屏动画
  3. android style 退出动画 解决退出动画无效问题
  4. 介绍一个Android开源项目:GifView——Android显示GIF动画
  5. XamarinAndroid组件教程RecylerView动画组件使用动画(3)
  6. Android 的补间动画
  7. Android属性动画之ObjectAnimator
  8. Android的View动画
  9. Android Animation动画 控制动画的执行效果,速度等

随机推荐

  1. android字符太长自动变成省略号
  2. Android的源代码结构
  3. Android(安卓)UI之ImageButton(带图标的
  4. Learning Android(安卓)第1章 - 工欲善其
  5. Android进程的内存管理分析
  6. Android项目优化宝典
  7. android进程在管理器中被杀死
  8. Android(安卓)高手进阶教程(十四)之----A
  9. Android(安卓)开发中Parcel存储类型和数
  10. Android(安卓)学习