在做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开源项目:Android显示GIF动画
  2. android 属性动画
  3. Android(安卓)揭露效果兼容低版本
  4. Android(安卓)7.0 如何去掉灭屏动画
  5. Android(安卓)RecyclerView
  6. android style 退出动画 解决退出动画无效问题
  7. 介绍一个Android开源项目:GifView——Android显示GIF动画
  8. XamarinAndroid组件教程RecylerView动画组件使用动画(3)
  9. Android(安卓)的补间动画

随机推荐

  1. Tablayout属性以及设置行间距和列间距
  2. GAE和android的几个中文问题
  3. android控件的监听绑定方法
  4. Android(安卓)自定义View 标识当前选中的
  5. android 日历开发附源码(附源码)
  6. 在Mer系统中启动Android系统(一)
  7. Android Bitmap的常用压缩方式
  8. Android开发之侧拉栏的使用
  9. Android产品研发(二十三)-->Android中保存
  10. android中关于scrollview内部组件 androi