Android2.3支持RatingBar垂直的问题,setRating不能设置

jianhuijin 2013-03-25 03:39:59
在Android4.0里可以直接对RatingBar进行垂直的操作,但是在Android2.3里面却没有这个功能 ,所以我们须要重新进行改写,代码见下面,但是改写后存在一个问题,就是setRating设置有问题,不能设置出来,请问有没有高手帮我解答一下。
public class VerticalRatingBar extends RatingBar {
private int x, y, z, w;
@Override
protected void drawableStateChanged() {
// TODO Auto-generated method stub

super.drawableStateChanged();
}
@Override
public void setStepSize(float stepSize) {
// TODO Auto-generated method stub
super.setStepSize(stepSize);
}
public VerticalRatingBar(Context context) {
super(context);
}

public VerticalRatingBar(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public VerticalRatingBar(Context context, AttributeSet attrs) {
super(context, attrs);
}

protected void onSizeChanged(int w, int h, int oldw, int oldh) {
this.x = w;
this.y = h;
this.w = oldw;
this.z = 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 boolean onTouchEvent(MotionEvent event) {
if (!isEnabled()) {
return false;
}
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
setSelected(true);
setPressed(true);
break;
case MotionEvent.ACTION_MOVE:
setProgress(getMax()
- (int) (getMax()*event.getY()/getHeight()));
onSizeChanged(getWidth(), getHeight(), 0, 0);
break;
case MotionEvent.ACTION_UP:
setSelected(false);
setPressed(false);
break;
case MotionEvent.ACTION_CANCEL:
break;

}
return true;
}
@Override
public synchronized void setProgress(int progress) {
if (progress >= 0)
super.setProgress(progress);
else
super.setProgress(0);
onSizeChanged(x, y, z, w);
}
}
...全文
158 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
sophia淑 2014-09-17
  • 打赏
  • 举报
回复
同问,不知道楼主这个问题解决了没有呢
jianhuijin 2013-03-25
  • 打赏
  • 举报
回复
setRating(1),的时候可以,setRating(2)或者setRating(3)的时候就不行了

80,349

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧