像一些手机安全卫士或者评分的软件都带有雷达扫描的效果,那么这个效果怎么实现呢,其实很简单! 上源码和效果图

第一层

第二层

第三层
那么只需要绘制这三张图片,然后蓝色覆盖灰色的,用clipRect函数显示百分比,然后用canvas旋转第三张的雷达图片即可。是不是很简单。
源码:
package com.fenjin.app.view;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.view.View;
import com.fenjin.app.humiture.R;
import com.fenjin.app.util.CachedThreadPool;
import com.fenjin.library.http.Tools;
public class RadarView extends View {
Bitmap bitmapBlue;
Bitmap bitmapGray;
Bitmap bitmapRadar;
int height;
float percent;
int value;// 显示的值
float degrees;
float ox;
float oy;
float cx;
float cy;
boolean hideRadar;
public RadarView(Context context, AttributeSet attrs) {
super(context, attrs);
bitmapBlue = BitmapFactory.decodeResource(getResources(),
R.drawable.circle_blue);
bitmapGray = BitmapFactory.decodeResource(getResources(),
R.drawable.circle_gray);
bitmapRadar = BitmapFactory.decodeResource(getResources(),
R.drawable.radar);
ox = (bitmapBlue.getWidth() - bitmapRadar.getWidth()) >> 1;
oy = (bitmapBlue.getHeight() - bitmapRadar.getHeight()) >> 1;
cx = bitmapBlue.getWidth() >> 1;
cy = bitmapBlue.getHeight() >> 1;
hideRadar = true;
}
public void dismiss() {
hideRadar = true;//停止线程
}
public void startScanfThread() {
hideRadar = false;//启动一个线程进行不断的旋转角度
CachedThreadPool.execute(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
while (!hideRadar) {
degrees = (degrees += 2) >= 360 ? 0 : degrees;
postInvalidate();
Tools.sleep(10);
}
}
});
}
@Override
protected void onLayout(boolean changed, int left, int top, int right,
int bottom) {
// TODO Auto-generated method stub
super.onLayout(changed, left, top, right, bottom);
height = getHeight();
percent = height / 100f;
}
@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
canvas.drawBitmap(bitmapBlue, 0, 0, null);
canvas.save();
canvas.clipRect(0, 0, getWidth(), (100 - value) * percent);
canvas.drawBitmap(bitmapGray, 0, 0, null);
canvas.restore();
if (!hideRadar) {
canvas.save();
canvas.rotate(degrees, cx, cy);
canvas.drawBitmap(bitmapRadar, ox, oy, null);
canvas.restore();
}
}
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
public float getDegrees() {
return degrees;
}
public void setDegrees(float degrees) {
this.degrees = degrees;
}
public boolean isHideRadar() {
return hideRadar;
}
public void setHideRadar(boolean hideRadar) {
this.hideRadar = hideRadar;
}
}
显示效果:

本文链接:https://it72.com/1458.htm