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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • todoMVC_mvc框架是什么

    todoMVC_mvc框架是什么依赖cssnpmitodomvc-commontodomvc-app-cssapp.component.tsimport{Component}from’@angular/core’;consttodos=[{id:1,title:’吃饭’,done:true},{id:1,title:’工作’,done:false},{id:1,title:’运动’,

    2022年9月11日
  • 最小二乘法求回归方程的推导[通俗易懂]

    最小二乘法求回归方程的推导[通俗易懂]这里手写的最小二乘法​​​​​​​的推导过程。  

    2022年10月24日
  • How to Remember New Words

    How to Remember New Words

    2021年5月3日
  • Tomcat闪退问题 Tomcat在eclipse运行失败

    Tomcat闪退问题 Tomcat在eclipse运行失败Tomcat原本单独运行没问题,在eclipse里面运行失败后,单独运行也不行了这个方框我看了一晚上,我要看吐了。。。查了很多关于Tomcat闪退的资料,大概分为这几个解决方法:1.环境配置问题2.端口被占用3.直接修改startup.bat和shutdown.bat文件,在前面添加jdk和jre的安装路径…还有很多这些方法随便一搜到处都是我都试过了但是还是没有用…

    2022年5月11日
  • 离散数学传递闭包_传递闭包一定等于自身的是

    离散数学传递闭包_传递闭包一定等于自身的是给定 n 个变量和 m 个不等式。其中 n 小于等于 26,变量分别用前 n 的大写英文字母表示。不等式之间具有传递性,即若 A>B 且 B>C,则 A>C。请从前往后遍历每对关系,每次遍历时判断:如果能够确定全部关系且无矛盾,则结束循环,输出确定的次序;如果发生矛盾,则结束循环,输出有矛盾;如果循环结束时没有发生上述两种情况,则输出无定解。输入格式输入包含多组测试数据。每组测试数据,第一行包含两个整数 n 和 m。接下来 m 行,每行包含一个不等式,不等式全部为小于关系

    2022年8月9日
  • 使用XSLT转换XML

    使用XSLT转换XMLSLT1.0是W3C标准,主要用于对XML文档的转换,包括将XML转换成HMTL,TEXT或者另外格式的XML文件.XSLT1.0可以与XPATH1.0标准一起使用,XPATH会告诉你要转换的节点而

    2022年7月2日

发表回复

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

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