1、效果图
2、自定义的View
public class View_07 extends View {
private Paint mPaint;
private static float[] circleProportion = {1 / 15f, 2 / 15f, 3 / 15f, 4 / 15f, 5 / 15f, 6 / 15f};
private Bitmap bitmap;
private Paint mPaintScan;//扫描的画笔
private Matrix matrix = new Matrix();// 旋转需要的矩阵
private int mRoteDegree;//扫描的角度
private Shader scanShader;
private static final String TAG = "View_07";
public View_07(Context context) {
this(context, null);
}
public View_07(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public View_07(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
mPaint = new Paint();
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeWidth(2);
mPaint.setColor(Color.WHITE);
mPaint.setAntiAlias(true);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int mWidth = MeasureSpec.getSize(widthMeasureSpec);
int mHeight = MeasureSpec.getSize(heightMeasureSpec);
setMeasuredDimension(mWidth, mHeight);
//设置扫描渲染的shader
scanShader = new SweepGradient(mWidth / 2, mHeight / 2,
new int[]{Color.TRANSPARENT, Color.parseColor("#84B5CA")}, null);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
int x = getWidth() / 2;
int y = getHeight() / 2;
//画⚪
for (int i = 0; i < circleProportion.length; i++) {
canvas.drawCircle(x, y, getWidth() * circleProportion[i], mPaint);
}
bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.foucs);
canvas.drawBitmap(bitmap, x - bitmap.getWidth() / 2, y - bitmap.getHeight() / 2, mPaint);
drawSweep(canvas, x, y, (int)(getWidth() * circleProportion[circleProportion.length-1]));
// LinearGradient 线性渐变
// shader=new LinearGradient(300,300,400,400, Color.RED, Color.BLUE
// , Shader.TileMode.CLAMP);
// RadialGradient 辐射渐变
// shader = new RadialGradient(300, 300, 200, Color.RED, Color.BLUE, Shader.TileMode.CLAMP);
// SweepGradient 扫描渐变
// shader=new SweepGradient(300,300,Color.RED, Color.BLUE);
// BitmapShader
// Bitmap bitmap=BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher);
// shader=new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
}
/**
* 画扫描效果
*/
private void drawSweep(Canvas canvas, int cx, int cy, int radius) {
//扇形的透明的渐变效果
SweepGradient sweepGradient = new SweepGradient(cx, cy,
new int[]{Color.TRANSPARENT, changeAlpha(Color.WHITE, 40), changeAlpha(Color.WHITE, 140)
}, new float[]{0.0f, 0.5f, 0.8f});
Paint mSweepPaint = new Paint();
mSweepPaint.setAntiAlias(true);
mSweepPaint.setShader(sweepGradient);
//先旋转画布,再绘制扫描的颜色渲染,实现扫描时的旋转效果。
canvas.rotate(mRoteDegree, cx, cy);
canvas.drawCircle(cx, cy, radius, mSweepPaint);
}
public void setMatrix(int degree) {
mRoteDegree = degree;
invalidate();
}
//线程叠加
public Runnable runnable=new Runnable() {
@Override
public void run() {
mRoteDegree+=1;
setMatrix(mRoteDegree);
postDelayed(runnable,20);
}
};
/**
* 改变颜色的透明度
*/
private static int changeAlpha(int color, int alpha) {
int red = Color.red(color);
int green = Color.green(color);
int blue = Color.blue(color);
return Color.argb(alpha, red, green, blue);
}
}
3、使用
public class ViewFragment_07 extends Fragment {
private static final String TAG = "ViewFragment_07";
private View_07 sweepView;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.view_07, container, false);
sweepView = view.findViewById(R.id.view_sweep);
new Thread(sweepView.runnable).start(); //开启线程
return view;
}
}