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

jianhuijin jianhuijin 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
在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);
}
}
...全文

相关推荐

评论 2
收藏 已收藏
分享
返回顶部
jianhuijin
Android 80,490 成员 91,375 内容