码迷,mamicode.com
首页 > 移动开发 > 详细

安卓学习-界面-ui-RatingBar

时间:2014-09-09 11:00:08      阅读:239      评论:0      收藏:0      [点我收藏+]

标签:android   style   blog   http   color   os   io   java   ar   

bubuko.com,布布扣

activity_main.xml

bubuko.com,布布扣
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/linearLayout1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:padding="10dp" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="改变图片透明度"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <!-- 
        android:numStars 多少颗星
        android:rating 默认多少颗星
        android:stepSize 最小变动0.5颗星
     -->
    <RatingBar
        android:id="@+id/ratingBar1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:numStars="4"
        android:rating="4"
        android:stepSize="0.5"
         />

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/pic1" />

</LinearLayout>
View Code

 

MainActivity.java

bubuko.com,布布扣
public class MainActivity extends Activity{

    RatingBar ratingBar1;
    ImageView imageView1;
    
    Handler handler=new Handler(){
        public void handleMessage(Message msg) {
            if(msg.what==123){
                float alpha =msg.getData().getFloat("alpha");
                imageView1.setImageAlpha((int)alpha);
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        imageView1=(ImageView)findViewById(R.id.imageView1);
        ratingBar1=(RatingBar)findViewById(R.id.ratingBar1);
        ratingBar1.setOnRatingBarChangeListener(new OnRatingBarChangeListener() {
            
            @Override
            public void onRatingChanged(RatingBar ratingBar, float rating,
                    boolean fromUser) {
                Message msg=new Message();
                msg.what=123;
                Bundle data=new Bundle();
                //rating:当前是几颗星
                //getNumStars:总共多少颗星
                //rating*255/ratingBar1.getNumStars():在255中的位置
                data.putFloat("alpha", rating*255/ratingBar1.getNumStars());
                msg.setData(data);
                handler.sendMessage(msg);
                
            }
        });
        
    }

}
View Code

 

安卓学习-界面-ui-RatingBar

标签:android   style   blog   http   color   os   io   java   ar   

原文地址:http://www.cnblogs.com/weijj/p/3961391.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!