SeekBar和RatingBar

SeekBar功能及用法

SeekBar拖动条和进度条非常相似,只是进度条采用颜色填充来表明进度完成的程度,而拖动条则通过滑块的位置来标识数值—而且拖动条允许用户拖动滑块来改变值,因此拖动条通常用于对系统的某种数值进行调节,比如调节音量等。

SeekBar允许用户改变拖动条的滑块外观,改变滑块外观通过如下属性来制定。

1、Android:thumb:指定一个Drawable对象,该对象将作为自定义滑块。
2、为了让程序能响应拖动条滑块位置的改变,程序可以考虑为它绑定一个OnSeekBarChangeListener监听器。

  • 废话不多说,直接看效果(通过seekBar空间控制图片的透明度)!

这里写图片描述

  • 首先看一下SeekBar的布局,布局很简单一个ImageView和一个SeekBar
 <ImageView
        android:id="@+id/image"
        android:layout_width="match_parent"
        android:layout_height="240dp"
        android:src="@drawable/icon_setting_avatar"/>
    <!-- 定义一个拖动条,并改变它的滑块外观 -->
    <SeekBar
        android:id="@+id/seekbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:max="255"
        android:progress="255"
        android:thumb="@drawable/dot_focus"/>
  • 代码中引用布局,逻辑实现
import android.annotation.TargetApi;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.Toast;

public class SeekBarActivity extends AppCompatActivity {
    ImageView image;
    private int prog;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.seekbar);
        setView();
    }

    private void setView() {

        image = (ImageView) findViewById(R.id.image);
        SeekBar seekBar = (SeekBar) findViewById(R.id.seekbar);

        seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener()
        {
            // 当拖动条的滑块位置发生改变时触发该方法
            @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
            public void onProgressChanged(SeekBar arg0, int progress, boolean fromUser) {
                // 动态改变图片的透明度
                image.setImageAlpha(progress);
                prog = progress;
            }
            public void onStartTrackingTouch(SeekBar bar){
                Toast.makeText(SeekBarActivity.this,"开始" + prog,Toast.LENGTH_SHORT).show();
            }
            public void onStopTrackingTouch(SeekBar bar){
                Toast.makeText(SeekBarActivity.this,"bar:" + prog,Toast.LENGTH_SHORT).show();
            }
        });
    }
}

RatingBar功能及用法

星级评分条与拖动条十分相似,他们甚至有相同的父亲:AbsSeekBar。实际上星级评分条与拖动条的用法、功能十分接近:他们都允许用户拖动来改变进度。RatingBar与SeekBar最大区别在于:RatingBar通过星星来表示进度。

监听:OnRatingBarChangeListener监听器。

  • 老规矩,先看效果
    这里写图片描述

  • 布局文件,也只有两个控件

<ImageView
        android:id="@+id/image"
        android:layout_width="match_parent"
        android:layout_height="240dp"
        android:src="@drawable/icon_setting_avatar"/>
    <!-- 定义一个星级评分条 -->
    <RatingBar
        android:id="@+id/rating"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:numStars="5"
        android:max="255"
        android:progress="255"
        android:stepSize="0.5"/>
  • 代码逻辑,一看就知道了
import android.annotation.TargetApi;
import android.app.Activity;
import android.os.Build;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.RatingBar;

/**
 * Created by Administrator on 2016/9/17 0017.
 */
public class RatingActivity extends Activity {
    private ImageView image;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ratingbar);
        image = (ImageView) findViewById(R.id.image);
        RatingBar ratingBar = (RatingBar) findViewById(R.id.rating);
        ratingBar.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener(){
            // 当星级评分条的评分发生改变时触发该方法
            @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
            @Override
            public void onRatingChanged(RatingBar arg0, float rating,boolean fromUser){
                // 动态改变图片的透明度,其中255是星级评分条的最大值
                // 5个星星就代表最大值255
                image.setImageAlpha((int) (rating * 255 / 5));
            }
        });
    }
}

最近很烦,这个月把Android基础复习完。10月就开始学习关于java的。培训出来的就是底子差。

dome下载:http://download.csdn.net/detail/bobo8945510/9637000

好的,我知道了。下面是您所需要的代码: XML文件: ``` <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <ImageView android:id="@+id/imageView" android:layout_width="match_parent" android:layout_height="match_parent" android:src="@drawable/ic_launcher_background" /> <SeekBar android:id="@+id/seekBar" android:layout_width="match_parent" android:layout_height="wrap_content" android:max="255" android:layout_alignParentBottom="true"/> </RelativeLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center"> <ProgressBar android:id="@+id/progressBar" style="?android:attr/progressBarStyleHorizontal" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="50dp" android:layout_marginRight="50dp" android:layout_weight="1" android:max="100" android:progress="0" /> <RatingBar android:id="@+id/ratingBar" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:numStars="5" android:stepSize="1.0" /> </LinearLayout> </LinearLayout> ``` Java文件: ``` public class MainActivity extends AppCompatActivity { private ImageView imageView; private SeekBar seekBar; private ProgressBar progressBar; private RatingBar ratingBar; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); imageView = findViewById(R.id.imageView); seekBar = findViewById(R.id.seekBar); progressBar = findViewById(R.id.progressBar); ratingBar = findViewById(R.id.ratingBar); seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { imageView.setAlpha((float) progress / 255); } @Override public void onStartTrackingTouch(SeekBar seekBar) {} @Override public void onStopTrackingTouch(SeekBar seekBar) {} }); ratingBar.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() { @Override public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) { Toast.makeText(MainActivity.this, "Rating: " + rating, Toast.LENGTH_SHORT).show(); } }); } } ``` 运行效果: ![image](https://user-images.githubusercontent.com/38335321/122733356-37ccff00-d2a8-11eb-9a0f-3c9843dc5d5a.png) 希望这可以帮助到您!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值