You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
162 lines
5.5 KiB
Java
162 lines
5.5 KiB
Java
package com.common.commonlib.view;
|
|
|
|
import android.content.Context;
|
|
import android.graphics.Bitmap;
|
|
import android.graphics.BitmapFactory;
|
|
import android.graphics.Canvas;
|
|
import android.graphics.Color;
|
|
import android.graphics.Matrix;
|
|
import android.graphics.Paint;
|
|
import android.util.AttributeSet;
|
|
import android.util.DisplayMetrics;
|
|
import android.util.TypedValue;
|
|
import android.view.View;
|
|
|
|
import androidx.annotation.Nullable;
|
|
|
|
import com.common.commonlib.R;
|
|
|
|
/**
|
|
* mic自定义View
|
|
*
|
|
* @author miracle
|
|
* @since 2021/8/2
|
|
*/
|
|
public class MikeView extends View {
|
|
//进度条格数
|
|
private final int COUNT = 16;
|
|
//宽、高、间隔、进度条居上距离
|
|
private int mVoiceRectW = 1;
|
|
private int mVoiceRectH = 10;
|
|
private int mInterval = 3;
|
|
private int mVoiceRectMarginTop = 83;
|
|
//mic 文字 进度条 背景
|
|
private Paint mImagePaint;
|
|
private Paint mVoiceRectPaint;
|
|
private Paint mBackgroundPaint;
|
|
|
|
private Bitmap mVoiceRecording;//mic图片
|
|
|
|
//屏幕宽高
|
|
private int SCREEN_HEIGHT = 0;
|
|
private int SCREEN_WIDTH = 0;
|
|
|
|
private int mVoiceRectStart = 0, mDescriptionW = 0;//进度条开始左边位置、文字长度
|
|
|
|
private int mCurrentPosition = 0;//当前位置
|
|
private int mBackgroundRound = 3;//背景圆角
|
|
|
|
private int mImageW = 42;//新mic图片宽
|
|
private int mImageH = 42;//新mic图片高
|
|
|
|
private int mMarginTop = 25, mTextMarginBottom = 11;//mic图片居上距离 文字居底部距离
|
|
|
|
public MikeView(Context context) {
|
|
this(context, null);
|
|
}
|
|
|
|
public MikeView(Context context, @Nullable AttributeSet attrs) {
|
|
this(context, attrs, 0);
|
|
}
|
|
|
|
public MikeView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
|
|
super(context, attrs, defStyleAttr);
|
|
init();
|
|
}
|
|
|
|
private void init() {
|
|
DisplayMetrics dm = getResources().getDisplayMetrics();
|
|
|
|
mImageW = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, mImageW, dm);
|
|
mImageH = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, mImageH, dm);
|
|
mMarginTop = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, mMarginTop, dm);
|
|
mTextMarginBottom = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, mTextMarginBottom, dm);
|
|
mVoiceRectW = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, mVoiceRectW, dm);
|
|
mVoiceRectH = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, mVoiceRectH, dm);
|
|
mInterval = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, mInterval, dm);
|
|
mVoiceRectMarginTop = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, mVoiceRectMarginTop, dm);
|
|
mBackgroundRound = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, mBackgroundRound, dm);
|
|
|
|
mImagePaint = new Paint();
|
|
|
|
mVoiceRectPaint = new Paint();
|
|
mVoiceRectPaint.setColor(Color.WHITE);
|
|
|
|
mBackgroundPaint = new Paint();
|
|
mBackgroundPaint.setColor(Color.TRANSPARENT);//背景颜色
|
|
|
|
mVoiceRecording = BitmapFactory.decodeResource(getResources(), R.drawable.mike);
|
|
mVoiceRecording = scaleBitmap(mVoiceRecording, mImageW, mImageH);
|
|
|
|
setWillNotDraw(false);
|
|
}
|
|
|
|
@Override
|
|
protected void onDraw(Canvas canvas) {
|
|
super.onDraw(canvas);
|
|
if (SCREEN_HEIGHT == 0 || SCREEN_WIDTH == 0) {
|
|
SCREEN_HEIGHT = getHeight();
|
|
SCREEN_WIDTH = getWidth();
|
|
}
|
|
if (mVoiceRectStart <= 0) {
|
|
mVoiceRectStart = SCREEN_WIDTH / 2 - (COUNT * mVoiceRectW + (COUNT - 1) * mInterval) / 2;
|
|
}
|
|
|
|
canvas.drawRoundRect(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, mBackgroundRound, mBackgroundRound, mBackgroundPaint);//画灰色背景和圆角
|
|
|
|
canvas.drawBitmap(mVoiceRecording, SCREEN_WIDTH / 2 - mVoiceRecording.getWidth() / 2, mMarginTop, mImagePaint);//画mic图片
|
|
|
|
//画前几个是白色 后几个是灰色
|
|
for (int i = 0; i < COUNT; i++) {
|
|
if (i < mCurrentPosition) {
|
|
mVoiceRectPaint.setColor(Color.WHITE);
|
|
} else {
|
|
mVoiceRectPaint.setColor(Color.parseColor("#73FFFFFF"));
|
|
}
|
|
canvas.drawRect(i * mVoiceRectW + (i * mInterval) + mVoiceRectStart, mVoiceRectMarginTop, i * mVoiceRectW + (i * mInterval) + mVoiceRectW + mVoiceRectStart, mVoiceRectMarginTop + mVoiceRectH, mVoiceRectPaint);
|
|
}
|
|
}
|
|
|
|
public void setIndex(int p) {
|
|
if (p < 0) {
|
|
p = 0;
|
|
}
|
|
if (p >= COUNT) {
|
|
p = COUNT - 1;
|
|
}
|
|
|
|
mCurrentPosition = p;
|
|
postInvalidate();
|
|
}
|
|
|
|
public void setIndex(double percent) {
|
|
int p = (int) (COUNT * percent);
|
|
setIndex(p);
|
|
}
|
|
|
|
/**
|
|
* bitmap 缩放
|
|
*
|
|
* @param origin 源
|
|
* @param newWidth 宽
|
|
* @param newHeight 高
|
|
* @return bitmap
|
|
*/
|
|
private Bitmap scaleBitmap(Bitmap origin, int newWidth, int newHeight) {
|
|
if (origin == null) {
|
|
return null;
|
|
}
|
|
int height = origin.getHeight();
|
|
int width = origin.getWidth();
|
|
float scaleWidth = ((float) newWidth) / width;
|
|
float scaleHeight = ((float) newHeight) / height;
|
|
Matrix matrix = new Matrix();
|
|
matrix.postScale(scaleWidth, scaleHeight);// 使用后乘
|
|
Bitmap newBM = Bitmap.createBitmap(origin, 0, 0, width, height, matrix, false);
|
|
if (!origin.isRecycled()) {
|
|
origin.recycle();
|
|
}
|
|
return newBM;
|
|
}
|
|
}
|