seekbar自定义android,Android SeekBar自定义使用图片和颜色显示

案例使用的图片如下:

28dc28df52f06c95b1beb109e0326498.png         

e9232522a5238ea1ccff0d7edcd13c12.png               

171e4a49a843e84b9f07a106de43dbf9.png

1.在res/drawable目录下新增一个xml风格文件,seekbar_define_style.xml

xmlns:android="http://schemas.android.com/apk/res/android">

android:id="@android:id/background"

android:drawable="@drawable/hou"/>

android:id="@android:id/progress"

android:drawable="@drawable/qian"/>

android:id="@android:id/secondaryProgress"

android:drawable="@drawable/qian"/>

2.在res/drawable下定义个seekbar_thumb.xml文件

android:state_focused="true"

android:state_pressed="true"

android:drawable="@drawable/ic_launcher" />

android:state_focused="false"

android:state_pressed="false"

android:drawable="@drawable/orbino_icon_pack_006" />

android:state_focused="true"

android:state_pressed="false"

android:drawable="@drawable/ios" />

android:state_focused="true"

android:drawable="@drawable/ios"/>

3.在res/layut下定义布局资源文件seekbar_define.xml

android:layout_width="fill_parent"

android:layout_height="fill_parent"

>

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:orientation="vertical"

>

android:id="@+id/seekbar_tetview_one"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="SeekBar自定义"

/>

android:id="@+id/seekbar_tetview_two"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="SeekBar拖动时信息提示"

/>

android:layout_width="321px"

android:layout_height="wrap_content"

android:layout_centerInParent="true"

android:maxHeight="20px"

android:minHeight="20px"

android:paddingLeft="18px"

android:paddingRight="18px"

android:max="100"

android:progressDrawable="@drawable/seekbar_define_style"

android:thumb="@drawable/seekbar_thumb"

android:id="@+id/seekBar"/>

4.定义java文件通过 引用布局文件:

package com.test;

import android.R.integer;

import android.app.Activity;

import android.os.Bundle;

import android.os.Handler;

import android.os.Message;

import android.widget.SeekBar;

import android.widget.SeekBar.OnSeekBarChangeListener;

import android.widget.TextView;

public class SeekBarDemo_DefineDemo extends Activity {

private SeekBar seekBar;

private TextView textView_one, textView_two;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.seekbar_define);

seekBar = (SeekBar) findViewById(R.id.seekBar);

textView_one = (TextView) findViewById(R.id.seekbar_tetview_one);

textView_two = (TextView) findViewById(R.id.seekbar_tetview_two);

seekBar.setOnSeekBarChangeListener(seekbarChangeListener);

}

private OnSeekBarChangeListener seekbarChangeListener = new OnSeekBarChangeListener() {

// 停止拖动时执行

@Override

public void onStopTrackingTouch(SeekBar seekBar) {

// TODO Auto-generated method stub

textView_two.setText("停止拖动了!");

}

// 在进度开始改变时执行

@Override

public void onStartTrackingTouch(SeekBar seekBar) {

// TODO Auto-generated method stub

textView_two.setText("进度开始改变");

}

// 当进度发生改变时执行

@Override

public void onProgressChanged(SeekBar seekBar, int progress,

boolean fromUser) {

textView_two.setText("正在进行拖动操作,还没有停下来一直再拖动");

Message message = new Message();

Bundle bundle = new Bundle();// 存放数据

float pro = seekBar.getProgress();

float num = seekBar.getMax();

float result = (pro / num) * 100;

bundle.putFloat("key", result);

message.setData(bundle);

message.what = 0;

handler.sendMessage(message);

}

};

/**

* 用Handler来更新UI

*/

private Handler handler = new Handler() {

@Override

public void handleMessage(Message msg) {

textView_one.setText("当前拖动位置占 : "

+ msg.getData().getFloat("key") + "/100");

}

};

}

最后执行效果:

5b56d1e91e0eda4faf10cbc2ae4c8080.png

a48b8708be932dedf837555539d82486.png

d6f602dc6a17c3dd3886acc3564c0338.png

二:使用颜色显示,和尚面是一样的,只有我们定义颜色资源来替代图片资源文件seekbar_define_color_style.xml:如下:

android:paddingTop="3px"

android:paddingBottom="3px">

android:startColor="#ffffffff"

android:centerColor="#ff000000"

android:endColor="#ff808A87"

android:centerY="0.45"

android:angle="270"/>

android:paddingTop="3px"

android:paddingBottom="3px" >

android:startColor="#ffffffff"

android:centerColor="#ffFFFF00"

android:endColor="#ffAABD00"

android:centerY="0.45"

android:angle="270"/>

之后再SeekBar标签使用如下属性进行引入:其他保持不变

android:progressDrawable="@drawable/seekbar_define_color_style"

执行效果:

53be0a5b8e16c0fba477ca42050f8939.png

由于SeekBar的属性thumb引入了自定义的seekbar_thumb.xml文件,拖动图标是我们自定义的图片:除去这个属性

android:thumb="@drawable/seekbar_thumb"

就回复系统默认状态效果最后效果如下:

9f0ced222bb12100780ce704e454d1a5.png

我们可以通过颜色值再次休息seekbar_thumb.xml文件,使拖动按钮设置成自定义颜色:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值