ImageView之类的控件有现成的rotate方法,button,progressbar之类的如何做到,求指教。
我自己翻了下,progressbar的父类view的XML属性里貌似有旋转的方法,progressbar里不能使用啊
自定义控件继承progressbar的话,对android还不熟,不知道该覆写哪些啊

解决方案 »

  1.   

    android没有自带的竖直显示progressBar,这你需要研究下源码,自写一个类完成竖直显示,应该不难,做出来了,告知一下
      

  2.   

    一晚上我还真想出来了其实很简单。。
    继承progressbar,覆写他的ondraw方法,添加canvas.rotate(),canvas.translate()之类的方法就可以完成我要的效果了。
    不枉我发呆发到凌晨2点
      

  3.   

    其实只要加两行,如下,覆写onDraw,
    protected synchronized void onDraw(Canvas canvas) {
    // TODO Auto-generated method stub
    canvas.translate(00, X);
    canvas.rotate(-90);

    super.onDraw(canvas);
    }
    X的值需要自己试一下,貌似有细微的不同
    canvas.rotate(-90);就是逆时针转90°,就会变成从下往上涨的一个progressbar,当然之前要设置下横向的style,不然默认是圆形的
      

  4.   

    覆写 是在source->overrid/   里添加的吗
      

  5.   

    恩,不错的,thanks  lz
      

  6.   

    给你代码你参考吧import android.content.Context;
    import android.graphics.Canvas;
    import android.graphics.Rect;
    import android.graphics.drawable.Drawable;
    import android.util.AttributeSet;
    import android.view.KeyEvent;
    import android.view.MotionEvent;
    import android.widget.AbsSeekBar;public class VolumeSeekBar extends AbsSeekBar{

    private Drawable mThumb;

    public VolumeSeekBar(Context context) {
            this(context, null);
    }

    public VolumeSeekBar(Context context, AttributeSet attrs) {
            this(context, attrs, android.R.attr.seekBarStyle);
    } public VolumeSeekBar(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
    }

    public interface OnSeekBarChangeListener {
            void onProgressChanged(VolumeSeekBar VolumeSeekBar, int progress, boolean fromUser);
            void onStartTrackingTouch(VolumeSeekBar VolumeSeekBar);
            void onStopTrackingTouch(VolumeSeekBar VolumeSeekBar);
        }    private OnSeekBarChangeListener mOnSeekBarChangeListener;
        
        public void setOnSeekBarChangeListener(OnSeekBarChangeListener l) {
        
            mOnSeekBarChangeListener = l;
            
        }
        
        void onStartTrackingTouch() {
            if (mOnSeekBarChangeListener != null) {
                mOnSeekBarChangeListener.onStartTrackingTouch(this);
            }
        }
        
        void onStopTrackingTouch() {
            if (mOnSeekBarChangeListener != null) {
                mOnSeekBarChangeListener.onStopTrackingTouch(this);
            }
        }
        
        void onProgressRefresh(float scale, boolean fromUser) { 
            Drawable thumb = mThumb;
            if (thumb != null) {
                setThumbPos(getHeight(), thumb, scale, Integer.MIN_VALUE);
                invalidate();
            }
            if (mOnSeekBarChangeListener != null) {
             mOnSeekBarChangeListener.onProgressChanged(this, getProgress(), fromUser);
            }
        } protected synchronized void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
        {
    //               height = View.MeasureSpec.getSize(heightMeasureSpec);
    //            width = View.MeasureSpec.getSize(widthMeasureSpec);
                this.setMeasuredDimension(29, 120);    }

    /**
     * seekbar 由水平变为竖直 需要交换长宽的位置
     */
    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
    super.onSizeChanged(h, w, oldw, oldh);
    }

    public boolean onTouchEvent(MotionEvent event) {
            if (!isEnabled()) {
                return false;
            }        
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    setPressed(true);
                    onStartTrackingTouch();
                    trackTouchEvent(event);
                    break;
                    
                case MotionEvent.ACTION_MOVE:
                    trackTouchEvent(event);
                    attemptClaimDrag();
                    break;
                    
                case MotionEvent.ACTION_UP:
                    trackTouchEvent(event);
                    onStopTrackingTouch();
                    setPressed(false);
                    break;
                    
                case MotionEvent.ACTION_CANCEL:
                    onStopTrackingTouch();
                    setPressed(false);
                    break;
            }
            return true;
        }

     private void attemptClaimDrag() {
            if (getParent() != null) {
             getParent().requestDisallowInterceptTouchEvent(true);
            }
     }

    @Override
    protected synchronized void onDraw(Canvas canvas) {

    // 旋转
    canvas.rotate(-90);        //控制左右位置,要自己调试,如果SeekBar有偏差可以在这里调整
    canvas.translate(-this.getHeight(), 0);
    super.onDraw(canvas);
    }

     private void trackTouchEvent(MotionEvent event) {
     final int height = getHeight();
            final int available = height - this.getPaddingBottom() - this.getPaddingTop();
            int y = (int)event.getY();
            float scale;
            float progress = 0;
            if (y > height - getPaddingBottom()) {
                scale = 0.0f;
            } else if (y  < getPaddingTop()) {
                scale = 1.0f;
            } else {
                scale = (float)(height - getPaddingBottom()-y) / (float)available;
            }
            final int max = getMax();
            progress += scale * max;
            
            this.setProgress((int) progress);
    //         setThumbPos(getHeight(), mThumb, scale, 0) ;     
     }
     
     private void setThumbPos(int w, Drawable thumb, float scale, int gap) {
             int available = w+getPaddingLeft()-getPaddingRight();
             int thumbWidth = thumb.getIntrinsicWidth();
             int thumbHeight = thumb.getIntrinsicHeight();
             available -= thumbWidth;
             // The extra space for the thumb to move on the track
             available += getThumbOffset() * 2;
             int thumbPos = (int) (scale * available);
             int topBound, bottomBound;
             if (gap == Integer.MIN_VALUE) {
                 Rect oldBounds = thumb.getBounds();
                 topBound = oldBounds.top;
                 bottomBound = oldBounds.bottom;
             } else {
                 topBound = gap;
                 bottomBound = gap + thumbHeight;
             }
             thumb.setBounds(thumbPos, topBound, thumbPos + thumbWidth, bottomBound);
         }
     
      @Override
    public void setThumb(Drawable thumb)
    {
            mThumb = thumb;
    super.setThumb(thumb);
    }
     
      public boolean dispatchKeyEvent(KeyEvent event) {
         if(event.getAction()==KeyEvent.ACTION_DOWN)
         {
         KeyEvent newEvent = null;
         switch(event.getKeyCode())
         {
         case KeyEvent.KEYCODE_DPAD_UP:
         newEvent = new KeyEvent(KeyEvent.ACTION_DOWN,KeyEvent.KEYCODE_DPAD_RIGHT);
         break;
         case KeyEvent.KEYCODE_DPAD_DOWN:
         newEvent = new KeyEvent(KeyEvent.ACTION_DOWN,KeyEvent.KEYCODE_DPAD_LEFT);
         break;
         case KeyEvent.KEYCODE_DPAD_LEFT:
         newEvent = new KeyEvent(KeyEvent.ACTION_DOWN,KeyEvent.KEYCODE_DPAD_DOWN);
         break;
         case KeyEvent.KEYCODE_DPAD_RIGHT:
         newEvent = new KeyEvent(KeyEvent.ACTION_DOWN,KeyEvent.KEYCODE_DPAD_UP);
         break;
         default:
         newEvent = new KeyEvent(KeyEvent.ACTION_DOWN,event.getKeyCode());
    break;
         }
         return newEvent.dispatch(this);
         }
         return false;
         }
    }
    此处一个地方写死了 固定值29 120 这个是seekbar的竖直状态的宽度和高度;
    而seekbar的父类是progressbar 所以我这里onDraw的 rotate你可以参考着用
    还有就是所有的进度的更新的x ,y 你要颠倒下 就像我的setThumbPos 方法里那样的传的参数实际上是高度源码我没有怎么看 但是就是这个道理
      

  7.   

    楼主,竖立之后,progress的高度  没办法控制啊