android关于自定义seekbar控件的问题(将横向seekbar改成竖向seekbar) 10
自定义了个seekbar,将横向的seekbar改为了竖向的seekbar,目前只支持触摸,但是用遥控器控制时候按上下按钮没响应,按下左右按钮seekbar才会被拖动追了...
自定义了个seekbar,将横向的seekbar改为了竖向的seekbar,目前只支持触摸,但是用遥控器控制时候按上下按钮没响应,按下左右按钮seekbar才会被拖动 追了下代码seekbar继承自AbsSeekbar,AbsSeekBar继承自progressBar,ProgressBar继承自View,我该如何实现将按下左右键seekbar的progress值发生改变改为按下上下键seekbar的progress值发生改变?以下是我写的自定义seekbar的代码:
import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.widget.SeekBar;
public class VerticalSeekBarTwo extends SeekBar {
public VerticalSeekBarTwo(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
}
public VerticalSeekBarTwo(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}
public VerticalSeekBarTwo(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(h, w, oldh, oldw);
}
@Override
protected synchronized void onMeasure(int widthMeasureSpec,
int heightMeasureSpec) {
super.onMeasure(heightMeasureSpec, widthMeasureSpec);
setMeasuredDimension(getMeasuredHeight(), getMeasuredWidth());
}
protected void onDraw(Canvas c) {
c.rotate(-90);
c.translate(-getHeight(), 0);
super.onDraw(c);
}
@Override
public synchronized void setProgress(int progress) {
super.setProgress(progress);
onSizeChanged(getWidth(), getHeight(), 0, 0);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if (!isEnabled()) {
return false;
}
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_MOVE:
case MotionEvent.ACTION_UP:
setProgress(getMax()
- (int) (getMax() * event.getY() / getHeight()));
onSizeChanged(getWidth(), getHeight(), 0, 0);
break;
case MotionEvent.ACTION_CANCEL:
break;
}
return true;
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (isEnabled()) {
int progress = getProgress();
switch (keyCode) {
case KeyEvent.KEYCODE_DPAD_UP:
if (progress <= 0) break;
setProgress(progress - 1);
return true;
case KeyEvent.KEYCODE_DPAD_DOWN:
if (progress >= getMax()) break;
setProgress(progress + 1);
return true;
case KeyEvent.KEYCODE_DPAD_LEFT:
break;
case KeyEvent.KEYCODE_DPAD_RIGHT:
break;
}
}
return super.onKeyDown(keyCode, event);
}
} 展开
import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.widget.SeekBar;
public class VerticalSeekBarTwo extends SeekBar {
public VerticalSeekBarTwo(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
}
public VerticalSeekBarTwo(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}
public VerticalSeekBarTwo(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(h, w, oldh, oldw);
}
@Override
protected synchronized void onMeasure(int widthMeasureSpec,
int heightMeasureSpec) {
super.onMeasure(heightMeasureSpec, widthMeasureSpec);
setMeasuredDimension(getMeasuredHeight(), getMeasuredWidth());
}
protected void onDraw(Canvas c) {
c.rotate(-90);
c.translate(-getHeight(), 0);
super.onDraw(c);
}
@Override
public synchronized void setProgress(int progress) {
super.setProgress(progress);
onSizeChanged(getWidth(), getHeight(), 0, 0);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if (!isEnabled()) {
return false;
}
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_MOVE:
case MotionEvent.ACTION_UP:
setProgress(getMax()
- (int) (getMax() * event.getY() / getHeight()));
onSizeChanged(getWidth(), getHeight(), 0, 0);
break;
case MotionEvent.ACTION_CANCEL:
break;
}
return true;
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (isEnabled()) {
int progress = getProgress();
switch (keyCode) {
case KeyEvent.KEYCODE_DPAD_UP:
if (progress <= 0) break;
setProgress(progress - 1);
return true;
case KeyEvent.KEYCODE_DPAD_DOWN:
if (progress >= getMax()) break;
setProgress(progress + 1);
return true;
case KeyEvent.KEYCODE_DPAD_LEFT:
break;
case KeyEvent.KEYCODE_DPAD_RIGHT:
break;
}
}
return super.onKeyDown(keyCode, event);
}
} 展开
1个回答
2015-12-14
展开全部
是不是按键值出错了,你在onKeyDown里每次按键都打印出来按键值System.out.println("键值:"+keyCode);
然后看看按键值是不是跟你写的对应
然后看看按键值是不是跟你写的对应
追问
谢谢你了,自己搞反了,已经解决了,话说现在这百度知道的效果不行啊,这问题上周发出去的。
追答
我没注意时间。。。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询