源码简介:仿微信雷达扫描,仿安卓微信、云播雷达扫描动画效果点击中间的黑色圆圈开始扫描动画,再次点击复位,需要这种效果的朋友可以自己下载看一下。
源码效果:
源码片段:
- package com.example.tz_demo_8_7;
- import android.content.Context;
- import android.graphics.Canvas;
- import android.graphics.Color;
- import android.graphics.Matrix;
- import android.graphics.Paint;
- import android.graphics.Paint.Style;
- import android.graphics.Shader;
- import android.graphics.SweepGradient;
- import android.os.Handler;
- import android.util.AttributeSet;
- import android.widget.FrameLayout;
- public class MyRadarView extends FrameLayout {
- private Paint mPaintNormal; // 绘制普通圆圈和线的画笔
- private Paint mPaintCircle;// 绘制渐变圆
- private int w,h; // 手机屏幕的宽高,雷达视图父容器的宽高
- private Matrix matrix;
- private Handler handler=new Handler();
- private int start;
- private Runnable r=new Runnable() {
- @Override
- public void run() {
- // 执行循环旋转动画,并且刷新UI
- start=start+2;
- matrix=new Matrix();
- matrix.postRotate(start, w/2, h/2);// 设置画布旋转
- MyRadarView.this.invalidate(); // 刷新UI
- handler.postDelayed(r,20);
- }
- };
- public MyRadarView(Context context, AttributeSet attrs) {
- super(context, attrs);
- // 初始化画笔
- initPaint();
- setBackgroundResource(R.drawable.bg);
- // 获取手机屏幕宽高
- this.w=context.getResources().getDisplayMetrics().widthPixels;
- this.h=context.getResources().getDisplayMetrics().heightPixels;
- handler.post(r);
- }
- /**
- * @author Xubin Single QQ:215298766
- */
- private void initPaint() {
- mPaintNormal=new Paint();// 创建画笔
- mPaintNormal.setColor(Color.parseColor("#A1A1A1"));
- mPaintNormal.setStrokeWidth(3);// 设置线条
- mPaintNormal.setAntiAlias(true);// 设置抗锯齿
- mPaintNormal.setStyle(Style.STROKE);
- // 绘制渐变圆
- mPaintCircle=new Paint();
- mPaintCircle.setColor(0x9D00ff00);// 16进制
- mPaintCircle.setAntiAlias(true);
- }
- /**
- * 测量控件的宽高
- */
- @Override
- protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
- // 设置此视图的宽高
- setMeasuredDimension(w, h);
- }
- /**
- * 确定控件的视图
- */
- @Override
- protected void onDraw(Canvas canvas) {
- // 屏幕中心,x:w/2,y:h/2
- canvas.drawCircle(w/2, h/2, w/6, mPaintNormal); // 绘制小圆
- canvas.drawCircle(w/2, h/2, 2*w/6, mPaintNormal); // 绘制中圆
- canvas.drawCircle(w/2, h/2, 11*w/20, mPaintNormal); // 绘制中大圆
- canvas.drawCircle(w/2, h/2, 7*h/16, mPaintNormal); // 绘制大圆
- // 绘制渐变色的圆
- Shader shader=new SweepGradient(w/2, h/2, Color.TRANSPARENT, Color.parseColor("#AAAAAAAA"));
- mPaintCircle.setShader(shader);
- canvas.concat(matrix);
- canvas.drawCircle(w/2, h/2, 7*h/16, mPaintCircle); //绘制渐变圆
- super.onDraw(canvas);
- }
- }