简介
下面通过一个使用拖动滑块改变图片透明度的实例来学习SeekBar和RatingBar
step1:新建一个项目MySeekBar
step2:编写应用的UI界面
a. /layout/seekbar.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button android:layout_width="fill_parent"
android:layout_height="wrap_content" android:onClick="startRatingbar"
android:text="start Ratingbar"/>
<ImageView android:id="@+id/image" android:layout_width="fill_parent"
android:layout_height="240px"
android:src="@drawable/guitar" />
<!-- 定义一个拖动条,并改变它的滑动外观 -->
<SeekBar android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:max="255"
android:progress="255"
android:id="@+id/seekbar"
android:thumb="@drawable/star"
/>
</LinearLayout>
b. /layout/ratingbar.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button android:layout_width="fill_parent"
android:layout_height="wrap_content" android:onClick="startSeekBar"
android:text="start SeekBar"/>
<ImageView android:id="@+id/image" android:layout_width="fill_parent"
android:layout_height="240px" android:src="@drawable/wall" />
<!-- 定义一个星级评分条 -->
<RatingBar android:id="@+id/ratingbar" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:max="255"
android:progress="255" android:numStars="5" android:stepSize="0.5" />
</LinearLayout>
其中使用的图片有:
star.png
wall.jpg
guitar.jpg
step3:MySeekBar.java
package cn.roco.seekbar;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
public class MySeekBar extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.seekbar);
final ImageView imageView=(ImageView) findViewById(R.id.image);
SeekBar seekBar=(SeekBar) findViewById(R.id.seekbar);
seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
//当拖动条的滑块位置发生改变时触发该方法
@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
//动态改变图片的透明度
imageView.setAlpha(progress);
}
});
}
public void startRatingbar(View v){
Intent intent=new Intent(MySeekBar.this,MyRatingBar.class);
startActivity(intent);
}
}
step4:MyRatingBar.java
package cn.roco.seekbar;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.RatingBar.OnRatingBarChangeListener;
public class MyRatingBar extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.ratingbar);
final ImageView imageView=(ImageView) findViewById(R.id.image);
RatingBar ratingBar=(RatingBar) findViewById(R.id.ratingbar);
ratingBar.setOnRatingBarChangeListener(new OnRatingBarChangeListener() {
@Override
public void onRatingChanged(RatingBar ratingBar, float rating,
boolean fromUser) {
imageView.setAlpha((int)(rating*255/5));
}
});
}
public void startSeekBar(View v){
Intent intent=new Intent(MyRatingBar.this,MySeekBar.class);
startActivity(intent);
}
}
step5:AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="cn.roco.seekbar"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".MyRatingBar"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".MySeekBar"/>
</application>
</manifest>
step6:部署应用到模拟器上,查看运行效果
==================================================================================================
作者:欧阳鹏 欢迎转载,与人分享是进步的源泉!
转载请保留原文地址:http://blog.csdn.net/ouyang_peng
==================================================================================================
分享到:
相关推荐
安卓Android源码——Vertical-SeekBar-Android-master.zip
简单的ProgressBar,SeekBar,RatingBar
Android高级应用源码-垂直SeekBar(拖动条).zip
Android--开发--音量seekbar制作
Android自定义SeekBar滑动条
SeekBar-AdjustVolume This example was made by me for the open souce community to show how comfortable is to adjust volume without keys while streaming videos.
当滑动时:数值显示,滑动停止时显示数字,使用FrameLayout结合SeekBar。 首先我们看看。 Layout: <?xml version=1.0 encoding=utf-8?> <merge xmlns:android=http://schemas.android.com/apk/res/android ...
Android应用源码之垂直SeekBar(拖动条)
CircularSeekBar用于Android的定制圆形SeekBar(圆形,半圆形和椭圆形)
这个是在公司项目中准备使用的一个控件,本准备直接在网上搜一个用,可惜找到的很多都是 ProgressBar,并不具备拖拽功能,于是自己动手实现了一个,其功能和特性均仿照 SeekBar 进行设计。 效果预览 支持的特性 [x...
Android高级应用源码-Android seekbar滑动按钮源码.zip
本文实例讲述了Android中SeekBar和RatingBar用法。分享给大家供大家参考,具体如下: 什么是SeekBar? 可以拖动的进度条(在播放器中使用最常见) 1、在布局文件中声明 <SeekBar nandroid:layout_width=fill_parent ...
Customize the seekbar with beautiful and simple Android style
自定义的圆弧行SeekBar。(PS:不支持0%-100%跳转)
Android自定义垂直seekbar,可成功监听seekbar的各个状态,是很好的代码
一个横竖向都可以的SeekBar 控件
Android双向seekbar(带刻度),自定义view,控制上限和下限
Android-Seekbar-示例Android Seekbar示例
Android中ProgressBar、SeekBar、RatingBar、ImageSwitcher实现相册滑动示例代码