標籤:android 應用 介面 ui
一、SeekBar拖動條使用 SeekBar繼承於ProgressBar,ProgressBar所支援的XML屬性和方法完全適用於SeekBar。拖動條和進度條的區別是:進度條採用顏色填充來表明進度完成的程度,而拖動條則通過滑塊位置來標識數值並且運行使用者拖動滑塊來改變值。因此,拖動條通常用於對系統的某種數值進行調節,比如調節音量、圖片的透明度等。
1.拖動條效果
2.代碼實現功能:通過拖動滑塊該動態改變圖片的透明度
public class MainActivity extends ActionBarActivity { private ImageView image = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); image = (ImageView)findViewById(R.id.image); SeekBar seekBar = (SeekBar) findViewById(R.id.seekBar); seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() { //當拖動條發生改變時觸發該方法 public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { //Notification that the progress level has changed image.setImageAlpha(progress);//動態改變圖片的透明度 } public void onStopTrackingTouch(SeekBar seekBar) {//Notification that the user has finished a touch gesture } public void onStartTrackingTouch(SeekBar seekBar) {//Notification that the user has started a touch gesture } }); }}其中,介面布局檔案main.xml為:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <ImageView android:id="@+id/image" android:layout_width="fill_parent" android:layout_height="250dp" android:src="@drawable/hehe"/> <!--定義一個拖動條,並改變它的滑塊外觀 --> <SeekBar android:id="@+id/seekBar" android:layout_width="match_parent" android:layout_height="wrap_content" android:max="255" //設定拖動條的最大值 android:progress="255" //設定拖動條當前預設值 android:thumb="@drawable/android" /> </LinearLayout>
註:SeekBar允許使用者改變拖動條的滑塊外觀,通過android:thumb屬性指定一個Drawable對象實現。
二、RatingBar星際評分條使用
星際評分條與拖動條都繼承於AbsSeekBar,它們都允許使用者通過拖動來改變進度。RatingBar與SeekBar最大的區別是:RatingBar通過星星來表示。RatingBar支援的常見XML屬性如下: android:isIndicator:設定該星級評等條是否允許使用者改變(true為不允許修改) android:numStarts:設定該星級評等總共有多少個星級 android:rating:設定該星級評等條預設的星級 android:stepSize:設定每次需要改變多少個星級
1.星級評等效果
2.代碼實現功能:通過星級評等改變圖片的透明度
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); image = (ImageView) findViewById(R.id.image); RatingBar ratingBar = (RatingBar) findViewById(R.id.ratingBar); ratingBar.setOnRatingBarChangeListener(new OnRatingBarChangeListener() { public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) { image.setImageAlpha((int)(rating*255)/5);// 動態改變圖片的透明度 } });其中,RatingBar定義(main.xml)為:
<RatingBar android:id="@+id/ratingBar" android:layout_width="wrap_content" android:layout_height="wrap_content" android:max="255" android:numStars="5" android:stepSize="0.5" android:progress="255"/>
Android進度條(星級評等)使用詳解(二)