Android進度條(星級評等)使用詳解(二)

來源:互聯網
上載者:User

標籤: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進度條(星級評等)使用詳解(二)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.