Android:自定义view实现动画
16lz
2021-12-04
在做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
更多相关文章
- 介绍两个Android开源项目:Android显示GIF动画
- android 属性动画
- Android(安卓)揭露效果兼容低版本
- Android(安卓)7.0 如何去掉灭屏动画
- Android(安卓)RecyclerView
- android style 退出动画 解决退出动画无效问题
- 介绍一个Android开源项目:GifView——Android显示GIF动画
- XamarinAndroid组件教程RecylerView动画组件使用动画(3)
- Android(安卓)的补间动画