android学习—SeekBar和RatingBar

android学习—SeekBar和RatingBar

大家好,又见面了,我是全栈君,今天给大家准备了Idea注册码。

全栈程序员社区此处内容已经被作者隐藏,请输入验证码查看内容
验证码:
请关注本站微信公众号,回复“验证码”,获取验证码。在微信里搜索“全栈程序员社区”或者“www_javaforall_cn”或者微信扫描右侧二维码都可以关注本站微信公众号。

SeekBar 拖动条:拖动条和滚动栏类似,当是拖动条能够拖动滑块改变进度

RatingBar 星级评分条:星级评分条与拖动条相似

SeekBar特有的xml属性

android:thumb    指定一个Arawable对象,作为之定义滑块

android学习---SeekBar和RatingBar

RatingBar特有的xml属性

android:isIndicator   是否同意用户改变(true为不同意改动)

android:numStars   共同拥有多少个星级

android:rating   默认的星级

android:stepSize   每次至少改变多少个星级

android学习---SeekBar和RatingBar

为了知道改变的进度而做对应的操作,我们须要加入监听

SeekBar 的监听 OnSeekBarChangeListener

RatingBar的监听 OnRatingBarChangeListener

以下我们通过实例来熟悉它们

(1)编写布局文件   activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/linearLayout" 
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    
    <ImageView 
        android:id="@+id/imgView1"
        android:layout_width="120dp"
        android:layout_height="100dp"
        android:src="@drawable/ic_launcher"/>
    <SeekBar 
        android:id="@+id/seekBar"
        android:layout_width="180dp"
        android:layout_height="wrap_content"
        android:thumb="@drawable/ic_launcher"
        android:max="255"
        android:progress="255"/>
      <ImageView 
        android:id="@+id/imgView2"
        android:layout_width="120dp"
        android:layout_height="100dp"
        android:src="@drawable/img01"/>
      <RatingBar 
          android:id="@+id/reatingBar"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:max="255"
          android:progress="255"
          android:rating="5"
          android:stepSize="0.5"
          android:numStars="5"/>
    
</LinearLayout>

(2)编写 MainActivity.java

package com.example.bar;

import android.annotation.TargetApi;
import android.app.Activity;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.RatingBar.OnRatingBarChangeListener;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public class MainActivity extends Activity {
	
	private ImageView imgViewSB = null; //定义ImageView
	private ImageView imgViewRB = null;
	private SeekBar seekBar = null;  //定义SeekBar
	private RatingBar ratingBar = null; //定义RatingBar

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		//布局组件
		this.imgViewSB = (ImageView) findViewById(R.id.imgView1);
		this.imgViewRB = (ImageView) findViewById(R.id.imgView2);
		this.seekBar = (SeekBar) findViewById(R.id.seekBar);
		this.ratingBar = (RatingBar) findViewById(R.id.reatingBar);
		
		//seekBar设置监听,改变图片透明度
		this.seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
			
			@Override
			public void onStopTrackingTouch(SeekBar seekBar) {
				Log.i("onStopTrackingTouch", "停止拖动触发的方法");
				
			}
			
			@Override
			public void onStartTrackingTouch(SeekBar seekBar) {
				Log.i("onStartTrackingTouch", "開始拖动触发的方法");
				
			}
			
			
			/**
			 * seekBar: SeekBar对象
			 * progress:拖动条的进度
			 * fromUser:是否用户手动改变
			 */
			@Override
			public void onProgressChanged(SeekBar seekBar, int progress,
					boolean fromUser) {
				imgViewSB.setAlpha(progress);  //设置图片的透明度
				Log.i("onProgressChanged", "拖动滑块位置发生改变时触发的方法");
				
			}
		});
		
		//ratingBar设置监听,改变图片透明度
		this.ratingBar.setOnRatingBarChangeListener(new OnRatingBarChangeListener() {
			
			
			/**
			 * ratingBar:RatingBar对象
			 * rating :星级的大小
			 * fromUser:是否用户手动改变
			 */
			@Override
			public void onRatingChanged(RatingBar ratingBar, float rating,
					boolean fromUser) {
				imgViewRB.setAlpha((int)(rating*255/5));
				
			}
		});
		
		
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}

同意之后,效果例如以下:

android学习---SeekBar和RatingBar

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/117977.html原文链接:https://javaforall.net

(0)
上一篇 2021年12月15日 下午3:00
下一篇 2021年12月15日 下午3:00


相关推荐

  • HQChart实战教程41 -新浪+腾讯A股数据源对接 – uniapp版本 (源码付费)「建议收藏」

    HQChart实战教程41-新浪+腾讯A股数据源对接-uniapp版本数据源支持周期平台支持demo安卓apk下载源码售价580元效果截图HQChart代码地址数据源使用公开的新浪+腾讯的A股票Api数据,h5页面需要自己配代理跨域.app直接就可以访问,无需自己提供数据后台.支持周期分时,5日,日K,周K,1分钟K,5分钟K,15分钟K,30分钟K,36分钟K平台支持uniapp开发,支持h5和appdemo安卓apk下载https://opensource

    2022年4月15日
    90
  • qt中connect函数信号写法_qt中connect函数用法

    qt中connect函数信号写法_qt中connect函数用法*.cpp//带参数的拉姆达表达式connect(this,&Widget::mySignal,[=](intcount){qDebug()<<count;});mySignal(123456);*.hsignals:voidmySignal(intcount);注意:要在pro文件中加上CONFIG+=C++11…

    2025年11月28日
    12
  • 银行家算法-C语言实现

    银行家算法-C语言实现算法简介银行家算法(Banker’sAlgorithm)是一个避免死锁(Deadlock)的著名算法,是由艾兹格·迪杰斯特拉在1965年为T.H.E系统设计的一种避免死锁产生的算法。它以银行借贷系统的分配策略为基础,判断并保证系统的安全运行。—百度百科当一个进程申请使用资源的时候,银行家算法通过先试探分配给该进程资源,然后通过安全性算法判断分配后的系统是否处于安全状态,若不安全则试探分配作废,让该进程继续等待。安全性算法是判断分配后的系统是否会进入不安全状态,若不存在安全序列,则判定系统已经进入

    2022年6月5日
    37
  • python技巧(setdefault 与 defaultdict)

    python技巧(setdefault 与 defaultdict)转自 https mp weixin com s DFF8sRSW7W4n 从题目引出 data p 1 p 2 p 3 h 1 h 2 h 3 要转换成 result p 1 2 3 h 1 2 3 一般都会用的方法 d

    2026年3月19日
    2
  • 获得sql server的table的表结构 — 转到word中

    获得sql server的table的表结构 — 转到word中

    2021年11月15日
    44
  • HTMLTestRunner.py的使用

    HTMLTestRunner.py的使用下载地址 http tungwaiyip info software HTMLTestRunn html 使用时 先建立一个 PyDevPackage 将下载下来的 HTMLTestRunn py 文件拷贝在该目录下 例子 testcase5 dynamic pyimportunit widgetimport

    2026年3月18日
    2

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号