android 怎么自定义音量条
1个回答
2016-07-04 · 百度知道合伙人官方认证企业
兄弟连教育
兄弟连教育成立于2006年,11年来专注IT职业教育,是国内专业的IT技术培训学校。2016年成功挂牌新三板(股票代码:839467)市值过亿。开设专注程序员培训专注php、Java、UI、云计算、Python、HTML5、
向TA提问
关注
展开全部
public class SoundSeekBar extends View{
/**
* 当前值
*/
private int percentage=0;
/**
* 左右间距
*/
private int padding=10;
/**
* 画笔
*/
Paint myPaint=new Paint();
/**
* 当前值监听接口
*/
private OnPercentageChangeListener myOnPercentageChangeListener;
public int getPercentage() {
return percentage;
}
public void setPercentage(int percentage) {
this.percentage = percentage;
}
public int getPadding() {
return padding;
}
public void setPadding(int padding) {
this.padding = padding;
}
public SoundSeekBar(Context context) {
super(context);
}
public SoundSeekBar(Context context, AttributeSet attrs) {
super(context, attrs);
}
public SoundSeekBar(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public void setOnPercentageChangeListener(OnPercentageChangeListener onPercentageChangeListener)
{
this.myOnPercentageChangeListener=onPercentageChangeListener;
};
@Override
protected void onDraw(Canvas canvas) {
myPaint.setColor(0xffdcdcdc);
myPaint.setStyle(Paint.Style.FILL);
Path backPath=new Path();
// 去锯齿
myPaint.setAntiAlias(true);
backPath.moveTo(padding, getHeight());
backPath.lineTo(getWidth()-padding, 0);
backPath.lineTo(getWidth()-padding, getHeight());
backPath.close();
canvas.drawPath(backPath, myPaint);
Path percentagePath = new Path();
percentagePath.moveTo(0, getHeight());
percentagePath.lineTo(padding+(getWidth()-2*padding) * percentage / 100, getHeight());
percentagePath.lineTo(padding+(getWidth()-2*padding) * percentage / 100, getHeight()*(float)(1-(percentage/100.0)));
percentagePath.close();
myPaint.setColor(0xffff0000);
canvas.drawPath(percentagePath,myPaint);
super.onDraw(canvas);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if(event.getAction()==MotionEvent.ACTION_DOWN ||event.getAction()==MotionEvent.ACTION_MOVE)
{
int per=(int)(((event.getX()-padding-getX())/(float)(getWidth()-2*padding))*100);
if (percentage != per && per >= 0 && per <= 100) {
percentage = per;
postInvalidate();
} else if(percentage>100)
{
percentage = 100;
postInvalidate();
}else if(percentage<0)
{
percentage=0;
postInvalidate();
}
if(myOnPercentageChangeListener!=null)
{
myOnPercentageChangeListener.onPercentagerChange(percentage);
}
}
return true;
}
public interface OnPercentageChangeListener
{
public void onPercentagerChange(int percentage) ;
}
}
/**
* 当前值
*/
private int percentage=0;
/**
* 左右间距
*/
private int padding=10;
/**
* 画笔
*/
Paint myPaint=new Paint();
/**
* 当前值监听接口
*/
private OnPercentageChangeListener myOnPercentageChangeListener;
public int getPercentage() {
return percentage;
}
public void setPercentage(int percentage) {
this.percentage = percentage;
}
public int getPadding() {
return padding;
}
public void setPadding(int padding) {
this.padding = padding;
}
public SoundSeekBar(Context context) {
super(context);
}
public SoundSeekBar(Context context, AttributeSet attrs) {
super(context, attrs);
}
public SoundSeekBar(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public void setOnPercentageChangeListener(OnPercentageChangeListener onPercentageChangeListener)
{
this.myOnPercentageChangeListener=onPercentageChangeListener;
};
@Override
protected void onDraw(Canvas canvas) {
myPaint.setColor(0xffdcdcdc);
myPaint.setStyle(Paint.Style.FILL);
Path backPath=new Path();
// 去锯齿
myPaint.setAntiAlias(true);
backPath.moveTo(padding, getHeight());
backPath.lineTo(getWidth()-padding, 0);
backPath.lineTo(getWidth()-padding, getHeight());
backPath.close();
canvas.drawPath(backPath, myPaint);
Path percentagePath = new Path();
percentagePath.moveTo(0, getHeight());
percentagePath.lineTo(padding+(getWidth()-2*padding) * percentage / 100, getHeight());
percentagePath.lineTo(padding+(getWidth()-2*padding) * percentage / 100, getHeight()*(float)(1-(percentage/100.0)));
percentagePath.close();
myPaint.setColor(0xffff0000);
canvas.drawPath(percentagePath,myPaint);
super.onDraw(canvas);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if(event.getAction()==MotionEvent.ACTION_DOWN ||event.getAction()==MotionEvent.ACTION_MOVE)
{
int per=(int)(((event.getX()-padding-getX())/(float)(getWidth()-2*padding))*100);
if (percentage != per && per >= 0 && per <= 100) {
percentage = per;
postInvalidate();
} else if(percentage>100)
{
percentage = 100;
postInvalidate();
}else if(percentage<0)
{
percentage=0;
postInvalidate();
}
if(myOnPercentageChangeListener!=null)
{
myOnPercentageChangeListener.onPercentagerChange(percentage);
}
}
return true;
}
public interface OnPercentageChangeListener
{
public void onPercentagerChange(int percentage) ;
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
大雅新科技有限公司
2024-11-19 广告
2024-11-19 广告
这方面更多更全面的信息其实可以找下大雅新。深圳市大雅新科技有限公司从事KVM延长器,DVI延长器,USB延长器,键盘鼠标延长器,双绞线视频传输器,VGA视频双绞线传输器,VGA延长器,VGA视频延长器,DVI KVM 切换器等,优质供应商,...
点击进入详情页
本回答由大雅新科技有限公司提供
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询