2012-02-09 46 views
3

我正在使用播放器,我想使用垂直Seekbar控制音量。任何人都可以告訴我如何垂直對齊seekbar,如果你有一些代碼,它會很好。Android中的垂直Seekbar

+1

可能重複[如何使Android的垂直搜索欄?](http://stackoverflow.com/questions/3333658 /如何對做-A-垂直搜索條功能於機器人) – Squonk 2012-02-09 14:49:55

回答

1

我已經通過重寫的onDraw方法和翻轉90度

VerticalSeekBar做垂直搜索欄

import android.content.Context; 
import android.graphics.Canvas; 
import android.util.AttributeSet; 
import android.view.MotionEvent; 

public class VerticalSeekBar extends SeekBar { 

    public VerticalSeekBar(Context context) { 
     super(context); 
    } 

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

    public VerticalSeekBar(Context context, AttributeSet attrs) { 
     super(context, attrs); 
    } 

    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()); 
    } 

/*@Override 
    public synchronized void setProgress(int progress) // it is necessary for calling setProgress on click of a button 
    { 
     super.setProgress(progress); 
     onSizeChanged(getWidth(), getHeight(), 0, 0); 
    }*/ 

    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: 
      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; 
    } 
} 

用它在你的XML佈局:

<yourPackage.name.VerticalSeekBar 
    android:id="@+id/seekBar1" 
    android:layout_width="wrap_content" 
    android:layout_height="200dp" 
    /> 

對於API 11及更高版本,可以使用搜索條的XML一個ttributes(android:rotation="270")垂直效果。

<SeekBar 
android:id="@+id/seekBar1" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:rotation="270"/> 
0

添加這些行的build.gradle。

dependencies { 
    compile 'com.h6ah4i.android.widget.verticalseekbar:verticalseekbar:0.7.2' 
} 

佈局XML

<!-- This library requires pair of the VerticalSeekBar and VerticalSeekBarWrapper classes --> 
<com.h6ah4i.android.widget.verticalseekbar.VerticalSeekBarWrapper 
    android:layout_width="wrap_content" 
    android:layout_height="150dp"> 
    <com.h6ah4i.android.widget.verticalseekbar.VerticalSeekBar 
     android:id="@+id/mySeekBar" 
     android:layout_width="0dp" 
     android:layout_height="0dp" 
     android:max="100" 
     android:progress="0" 
     android:splitTrack="false" 
     app:seekBarRotation="CW90" /> <!-- Rotation: CW90 or CW270 --> 
</com.h6ah4i.android.widget.verticalseekbar.VerticalSeekBarWrapper> 

Java代碼

public class TestVerticalSeekbar extends AppCompatActivity { 
    private SeekBar mseekbar = null; 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_test_vertical_seekbar); 

     mseekbar = (SeekBar) findViewById(R.id.mySeekBar); 

     mseekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { 
      int progressChanged = 0; 

      public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { 
       progressChanged = progress; 
      } 

      public void onStartTrackingTouch(SeekBar seekBar) { 
       // TODO Auto-generated method stub 
      } 

      public void onStopTrackingTouch(SeekBar seekBar) { 
       Toast.makeText(getApplicationContext(), "seek bar progress:" + progressChanged, 
         Toast.LENGTH_SHORT).show(); 
      } 
     }); 
    } 

}