公司要做一个产品,产品要连接WiFi,我呢,就弄了一个小的自定义View,来显示Wifi的信号强度,上个图,然后把代码贴上来。
Screenshot_20200825_170843_com.example.hytapparat.jpgpackage com.example.hytapparatus.views;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.net.wifi.WifiManager;
import android.util.AttributeSet;
import android.view.View;
import androidx.annotation.Nullable;
public class WiFiStateView extends View {
private int wifi_state = 3;
private int height,width;
private Paint paint;
private int startAngle,sweepAngle;
private int wifiHeight = 0;
private int padding_bottom ;
private int bottom_x,bottom_y;
enum Style{
RECT,ROUND
}
private Style style = Style.ROUND;
public WiFiStateView(Context context) {
this(context,null);
}
public WiFiStateView(Context context, @Nullable AttributeSet attrs) {
this(context, attrs,0);
}
public WiFiStateView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
private void init() {
paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setColor(Color.BLACK);
paint.setAntiAlias(true);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
//获取宽的测量模式
int heightSize = MeasureSpec.getSize(heightMeasureSpec);//测量值
height = resolveSize(heightSize, heightMeasureSpec);
int widthSize = MeasureSpec.getSize(widthMeasureSpec);//测量值
width = resolveSize(widthSize, widthMeasureSpec);
int calc_wifi_height = (int) (width / (Math.sqrt(2)));
wifiHeight = Math.min(calc_wifi_height,height);
padding_bottom = (height - wifiHeight)/2;
bottom_x = width /2;
bottom_y = height - padding_bottom;
setMeasuredDimension(width, height);
}
public void setWifi_state(int level) {
this.wifi_state = WifiManager.calculateSignalLevel(level,5);
postInvalidate();
}
public void setStyle(Style style) {
this.style = style;
postInvalidate();
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
int signalRadius = wifiHeight / 4;
int paint_width = signalRadius/2;
paint.setStrokeWidth(paint_width);
if(style == Style.RECT){
paint.setStrokeCap(Paint.Cap.BUTT);
startAngle = -135;
sweepAngle = 90;
}else {
paint.setStrokeCap(Paint.Cap.ROUND);
startAngle = -130;
sweepAngle = 80;
}
for(int i = 1;i <= 4; i ++){
float radius = signalRadius * i;
if(i<=wifi_state){
paint.setColor(Color.parseColor("#4b4b4b"));
}else {
paint.setColor(Color.parseColor("#c9c9c9"));
}
RectF rectf;
if(i == 1){
paint.setStyle(Paint.Style.FILL);
if(style == Style.RECT){
rectf = new RectF(bottom_x - radius ,bottom_y - radius,bottom_x + radius,bottom_y + radius);
canvas.drawArc(rectf,startAngle,sweepAngle,true , paint);
}else {
canvas.drawCircle(bottom_x,bottom_y -paint_width,paint_width,paint );
}
}else {
paint.setStyle(Paint.Style.STROKE);
rectf = new RectF(bottom_x - radius + paint_width/2,bottom_y - radius + paint_width/2,bottom_x + radius - paint_width/2,bottom_y + radius - paint_width/2);
canvas.drawArc(rectf,startAngle,sweepAngle,false , paint);
}
}
}
}
网友评论