第一步:
点击事件会出现弹窗popupwindow的初始化,在弹窗里显示自定义音量控件。
selfDefinedSound= LayoutInflater.from(MainActivity.this).inflate(R.layout.activity_sound, null);//拿到xml布局的view
sound= (SelfDefinedSound) selfDefinedSound.findViewById(R.id.sound);//拿到自定义的音量控件
sound.setNotifyListener(this);
soundPopupWindow=new PopupWindow(MainActivity.this);
soundPopupWindow.setContentView(selfDefinedSound);
soundPopupWindow.setBackgroundDrawable(getResources().getDrawable(R.drawable.soundstyle));
soundPopupWindow.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
soundPopupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
soundPopupWindow.setFocusable(true);
soundPopupWindow.setOutsideTouchable(true);
第二步:
自定义的音量控件:
public class SelfDefinedSound extends View {
private int thickness = 13;
Paint paint;
private Notify notify;
//音量的总个数
private int size = 12;
//音量的大小
private int istrue = 10;
//音量间的间距
private int distance = 15;
public SelfDefinedSound(Context context, AttributeSet attrs) {//一定要写,不然在xml文件中会报错!
super(context, attrs);
paint=new Paint();
}
public SelfDefinedSound(Context context) {
super(context);
paint=new Paint();
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_UP) {
float x = event.getX();
if (x > getRight() / 2) {
jian();
notify.option(ConstantCommands.MINUS_SOUND);
} else {
jia();
notify.option(ConstantCommands.ADD_SOUND);
}
}
return true;
}
//加
public void jia() {
if (istrue < size) {
istrue += 1;
invalidate();
}
}
//减
public void jian() {
if (istrue > 0) {
istrue -= 1;
invalidate();
}
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int width = 0;
int height = 0;
// 设置宽度
int specMode = MeasureSpec.getMode(widthMeasureSpec);
int specSize = MeasureSpec.getSize(widthMeasureSpec);
switch (specMode)
{
case MeasureSpec.EXACTLY: // 明确指定
width = specSize;
break;
case MeasureSpec.AT_MOST:
width = getPaddingLeft() + getPaddingRight();
break;
}
// 设置高度
specMode = MeasureSpec.getMode(heightMeasureSpec);
specSize = MeasureSpec.getSize(heightMeasureSpec);
switch (specMode)
{
case MeasureSpec.EXACTLY:
height = specSize;
break;
case MeasureSpec.AT_MOST:
height = width / 20;
break;
}
setMeasuredDimension(width, height);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
paint.setDither(true);
//防锯齿
paint.setAntiAlias(true);
//设置画笔的宽度
paint.setStrokeWidth(thickness);
//画笔颜色
paint.setColor(0xff4AD662);
//设置样式为空心
paint.setStyle(Paint.Style.STROKE);
//定义线段形状-设置了这个之后 线段的开头和结尾都会变得有弧度
paint.setStrokeCap(Paint.Cap.ROUND);
int a = getWidth() / 2;
//因为绘制的圆环是以画笔的宽度来决定内院和外圆的距离的所以要/2
int b = a - thickness / 2;
//参数为矩形的四个点的坐标
RectF f = new RectF(a - b, a - b, a + b, a + b);
//绘制圆弧 参数为 矩形 起始位置 结束距离 画笔
//总角度
int z = 250;
//每一个音量所占据的角度
int one = (z - ((size - 1) * distance)) / size;
//圆弧的起始位置
int s = -180 - (z - 180) / 2;
//绘制选中
for (int i = 1; i <= istrue; i++) {
//绘制圆弧 参数为 矩形 起始位置 结束距离 画笔
canvas.drawArc(f, s, one, false, paint);//
oval
- 用于确定圆弧形状与尺寸的椭圆边界(即椭圆外切矩形)
startAngle
- 开始角度(
以时钟3点的方向为0°,顺时针为正方向)
sweepAngle
- 扫过角度(
以时钟3点的方向为0°,顺时针为正方向)
useCenter
- 是否包含圆心
paint
- 绘制圆弧的画笔
第三步 :
设置计时,8秒
public class SoundThread extends Thread
{
@Override
public void run() {
while (flag) {
if (System.currentTimeMillis() - time > 8000) {
Message message=activityHandler.obtainMessage();
message.what=ConstantCommands.DISMISS_SOUND;
activityHandler.sendMessage(message);//关闭弹窗
flag=false;
}
}
}
}