2011-04-07 63 views
6

如何旋轉RatingBar? 是否有垂直自定義RatingBar的示例?Android:垂直RatingBar?

+0

這尤其適用於列表視圖項小酒吧評價有用。 – 2011-12-13 22:19:54

回答

6

這是垂直的評價吧示例代碼...

public class VerticalRatingBar extends RatingBar { 
    private int x, y, z, w; 

    @Override 
    protected void drawableStateChanged() { 
     // TODO Auto-generated method stub 
     super.drawableStateChanged(); 
    } 

    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) { 
     super.onSizeChanged(h, w, oldh, oldw); 
     this.x = w; 
     this.y = h; 
     this.z = oldw; 
     this.w = oldh; 
    } 

    @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); 

    } 
} 
+0

我試過這段代碼,完全按照它的寫法,但它沒有工作,可能是因爲我在我的RatingBar中使用了自定義樣式? – 4gus71n 2015-12-01 20:34:33

0

在視圖上有一個setRotation方法,這將有望實現您的目標。

+0

自API Level 11以來就存在了。那麼舊版本呢? – Oli 2011-12-15 10:45:35

+0

您可以嘗試擴展RatingBar,以便覆蓋onDraw方法:protected void onDraw(Canvas c){c.rotate(90); super.onDraw(C); } – BDFun 2011-12-15 11:48:20