概述
第一步:
点击事件会出现弹窗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
- 绘制圆弧的画笔
s = s + one + distance;
}
paint.setColor(0xaa25221C);
//绘制未选中
for (int i = 1; i <= size - istrue; i++) {
canvas.drawArc(f, s, one, false, paint);
s = s + one + distance;
}
int record=0;
BitmapFactory.Options options=new BitmapFactory.Options();
// options.inJustDecodeBounds=true;
// BitmapFactory.decodeResource(getResources(), R.drawable.sound,options);
// options.inSampleSize=calculateInSampleSize(options,50,50);
// options.inJustDecodeBounds=false;
if (istrue==0) {
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.mute, options);
int c = (getWidth() - bitmap.getWidth()) / 2;
record=c;
//参数为所需要绘制的bitmap 左边位置 顶部位置 画笔
canvas.drawBitmap(bitmap, c, c, paint);
}
else
{
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.sound, options);
int c = (getWidth() - bitmap.getWidth()) / 2;
record=c;
//参数为所需要绘制的bitmap 左边位置 顶部位置 画笔
canvas.drawBitmap(bitmap, c, c, paint);
}
Bitmap add=BitmapFactory.decodeResource(getResources(), R.drawable.add,options);
//参数为所需要绘制的bitmap 左边位置 顶部位置 画笔
canvas.drawBitmap(add, (record-add.getWidth())/2,(getHeight()-add.getHeight())/2, paint);
Bitmap minus=BitmapFactory.decodeResource(getResources(), R.drawable.minus,options);
//参数为所需要绘制的bitmap 左边位置 顶部位置 画笔
canvas.drawBitmap(minus, 3/2*getWidth()-record/2-minus.getWidth()/2,(getHeight()-minus.getHeight())/2, paint);
}
private int calculateInSampleSize(BitmapFactory.Options options, int i, int i1) {
int height=options.outHeight;
int width=options.outWidth;
int inSampleSize=1;
if(height>i||width>i1)
{
int halfHeight=height/2;
int halfWidth=width/2;
while ((halfHeight/inSampleSize)>=i&&(halfWidth/inSampleSize)>=i1)
{
inSampleSize*=2;
}
}
return inSampleSize;
}
public void setNotifyListener(Notify notify)
{
this.notify=notify;
}
public interface Notify
{
void option(int option);
}
}
第三步 :
设置计时,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;
}
}
}
}
最后
以上就是俊逸店员为你收集整理的Android 笔记之自定义音量控件的全部内容,希望文章能够帮你解决Android 笔记之自定义音量控件所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复