Android自定义View 雷达扫描效果,android开发从入门到精通第二版

孔安福
2023-12-01

private int measureWidth(int measureSpec, int defaultSize) {
int result = 0;
int specMode = MeasureSpec.getMode(measureSpec);
int specSize = MeasureSpec.getSize(measureSpec);

if (specMode == MeasureSpec.EXACTLY) {
result = specSize;
} else {
result = defaultSize + getPaddingLeft() + getPaddingRight();
if (specMode == MeasureSpec.AT_MOST) {
result = Math.min(result, specSize);
}
}
result = Math.max(result, getSuggestedMinimumWidth());
return result;
}

/**

  • 测量高
  • @param measureSpec
  • @param defaultSize
  • @return
    */
    private int measureHeight(int measureSpec, int defaultSize) {
    int result = 0;
    int specMode = MeasureSpec.getMode(measureSpec);
    int specSize = MeasureSpec.getSize(measureSpec);

if (specMode == MeasureSpec.EXACTLY) {
result = specSize;
} else {
result = defaultSize + getPaddingTop() + getPaddingBottom();
if (specMode == MeasureSpec.AT_MOST) {
result = Math.min(result, specSize);
}
}
result = Math.max(result, getSuggestedMinimumHeight());
return result;
}

@Override
protected void onDraw(Canvas canvas) {

//计算圆的半径
int width = getWidth() - getPaddingLeft() - getPaddingRight();
int height = getHeight() - getPaddingTop() - getPaddingBottom();
int radius = Math.min(width, height) / 2;

//计算圆的圆心
int cx = getPaddingLeft() + (getWidth() - getPaddingLeft() - getPaddingRight()) / 2;
int cy = getPaddingTop() + (getHeight() - getPaddingTop() - getPaddingBottom()) / 2;

drawCircle(canvas, cx, cy, radius);

if (isShowCross) {
drawCross(canvas, cx, cy, radius);
}

//正在扫描
if (isScanning) {
if (isShowRaindrop) {
drawRaindrop(canvas, cx, cy, radius);
}
drawSweep(canvas, cx, cy, radius);
//计算雷达扫描的旋转角度
mDegrees = (mDegrees + (360 / mSpeed / 60)) % 360;

//触发View重新绘制,通过不断的绘制实现View的扫描动画效果
invalidate();
}
}

/**

  • 画圆
    */
    private void drawCircle(Canvas canvas, int cx, int cy, int radius) {
    //画mCircleNum个半径不等的圆圈。
    for (int i = 0; i < mCircleNum; i++) {
    canvas.drawCircle(cx, cy, radius - (radius / mCircleNum * i), mCirclePaint);
    }
    }

/**

  • 画交叉线
    */
    private void drawCross(Canvas canvas, int cx, int cy, int radius) {
    //水平线
    canvas.drawLine(cx - radius, cy, cx + radius, cy, mCirclePaint);

//垂直线
canvas.drawLine(cx, cy - radius, cx, cy + radius, mCirclePaint);
}

/**

  • 生成水滴。水滴的生成是随机的,并不是每次调用都会生成一个水滴。
    */
    private void generateRaindrop(int cx, int cy, int radius) {

// 最多只能同时存在mRaindropNum个水滴。
if (mRaindrops.size() < mRaindropNum) {
// 随机一个20以内的数字,如果这个数字刚好是0,就生成一个水滴。
// 用于控制水滴生成的概率。
boolean probability = (int) (Math.random() * 20) == 0;
if (probability) {
int x = 0;
int y = 0;
int xOffset = (int) (Math.random() * (radius - 20));
int yOffset = (int) (Math.random() * (int) Math.sqrt(1.0 * (radius - 20) * (radius - 20) - xOffset * xOffset));

if ((int) (Math.random() * 2) == 0) {
x = cx - xOffset;
} else {
x = cx + xOffset;
}

if ((int) (Math.random() * 2) == 0) {
y = cy - yOffset;
} else {
y = cy + yOffset;
}

mRaindrops.add(new Raindrop(x, y, 0, mRaindropColor));
}
}
}

/**

  • 删除水滴
    */
    private void removeRaindrop() {
    Iterator iterator = mRaindrops.iterator();

while (iterator.hasNext()) {
Raindrop raindrop = iterator.next();
if (raindrop.radius > 20 || raindrop.alpha < 0) {
iterator.remove();
}
}
}

/**

  • 画雨点(就是在扫描的过程中随机出现的点)。
    */
    private void drawRaindrop(Canvas canvas, int cx, int cy, int radius) {
    generateRaindrop(cx, cy, radius);
    for (Raindrop raindrop : mRaindrops) {
    mRaindropPaint.setColor(raindrop.changeAlpha());
    canvas.drawCircle(raindrop.x, raindrop.y, raindrop.radius, mRaindropPaint);
    //水滴的扩散和透明的渐变效果
    raindrop.radius += 1.0f * 20 / 60 / mFlicker;
    raindrop.alpha -= 1.0f * 255 / 60 / mFlicker;
    }
    removeRaindrop();
    }

/**

  • 画扫描效果
    */
    private void drawSweep(Canvas canvas, int cx, int cy, int radius) {
    //扇形的透明的渐变效果
    SweepGradient sweepGradient = new SweepGradient(cx, cy,
    new int[]{Color.TRANSPARENT, changeAlpha(mSweepColor, 0), changeAlpha(mSweepColor, 168),
    changeAlpha(mSweepColor, 255), changeAlpha(mSweepColor, 255)
    }, new float[]{0.0f, 0.6f, 0.99f, 0.998f, 1f});
    mSweepPaint.setShader(sweepGradient);
    //先旋转画布,再绘制扫描的颜色渲染,实现扫描时的旋转效果。
    canvas.rotate(-90 + mDegrees, cx, cy);
    canvas.drawCircle(cx, cy, radius, mSweepPaint);
    }

/**

  • 开始扫描
    */
    public void start() {
    if (!isScanning) {
    isScanning = true;
    invalidate();
    }
    }

/**

  • 停止扫描
    */
    public void stop() {
    if (isScanning) {
    isScanning = false;
    mRaindrops.clear();
    mDegrees = 0.0f;
    }
    }

/**

  • 水滴数据类
    */
    private static class Raindrop {

int x;
int y;
float radius

《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》

【docs.qq.com/doc/DSkNLaERkbnFoS0ZF】 完整内容开源分享

;
int color;
float alpha = 255;

public Raindrop(int x, int y, float radius, int color) {
this.x = x;
this.y = y;
this.radius = radius;
this.color = color;
}

/**

  • 获取改变透明度后的颜色值
  • @return
    */
    public int changeAlpha() {
    return RadarView.changeAlpha(color, (int) alpha);
    }

}

/**

  • dp转px
    */
    private static int dp2px(Context context, float dpVal) {
    return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
    dpVal, context.getResources().getDisplayMetrics());
    }

/**

  • 改变颜色的透明度
  • @param color
  • @param alpha
  • @return
    */
    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);
 类似资料: