自定义带刻度的seekbar

1.布局

<span style="font-family:SimHei;font-size:18px;"><com.imibaby.client.views.CustomSeekbar
android:id="@+id/myCustomSeekBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="14.33dp"
android:layout_marginRight="10.33dp" /></span>

2.在activity中使用

<span style="font-family:SimHei;font-size:18px;">private ArrayList<String> volume_sections = new ArrayList<String>();
volume_sections.add("静音");
volume_sections.add("低");
volume_sections.add("中");
volume_sections.add("高");
customSeekBar.initData(volume_sections);
customSeekBar.setProgress(int level);
customSeekBar.setResponseOnTouch(this);//activity实现了下面的接口ResponseOnTouch,每次touch会回调onTouchResponse public interface ResponseOnTouch {
public void onTouchResponse(int volume);
}</span>

3.自定义seekbar的代码

<span style="font-family:SimHei;font-size:18px;">package com.imibaby.client.views;  

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.MotionEvent;
import android.view.View; import com.imibaby.client.R;
import com.imibaby.client.interfaces.ResponseOnTouch; import java.util.ArrayList; public class CustomSeekbar extends View {
private final String TAG = "CustomSeekbar";
private int width;
private int height;
private int downX = ;
private int downY = ;
private int upX = ;
private int upY = ;
private int moveX = ;
private int moveY = ;
private float scale = ;
private int perWidth = ;
private Paint mPaint;
private Paint mTextPaint;
private Paint buttonPaint;
private Canvas canvas;
private Bitmap bitmap;
private Bitmap thumb;
private Bitmap spot;
private Bitmap spot_on;
private int hotarea = ;//点击的热区
private int cur_sections = ;
private ResponseOnTouch responseOnTouch;
private int bitMapHeight = ;//第一个点的起始位置起始,图片的长宽是76,所以取一半的距离
private int textMove = ;//字与下方点的距离,因为字体字体是40px,再加上10的间隔
private int[] colors = new int[]{0xffdf5600,0x33000000};//进度条的橙色,进度条的灰色,字体的灰色
private int textSize;
private int circleRadius;
private ArrayList<String> section_title;
public CustomSeekbar(Context context) {
super(context);
}
public CustomSeekbar(Context context, AttributeSet attrs) {
this(context, attrs, );
}
public CustomSeekbar(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
cur_sections = ;
bitmap = Bitmap.createBitmap(, , Bitmap.Config.ARGB_8888);
canvas = new Canvas();
canvas.setBitmap(bitmap);
thumb = BitmapFactory.decodeResource(getResources(), R.drawable.set_button_0);
spot = BitmapFactory.decodeResource(getResources(),R.drawable.spot);
spot_on = BitmapFactory.decodeResource(getResources(),R.drawable.spot_on);
bitMapHeight = thumb.getHeight()/;
textMove = bitMapHeight+;
textSize = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, , getResources().getDisplayMetrics());
circleRadius = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, , getResources().getDisplayMetrics());
mPaint = new Paint(Paint.DITHER_FLAG);
mPaint.setAntiAlias(true);//锯齿不显示
mPaint.setStrokeWidth();
mTextPaint = new Paint(Paint.DITHER_FLAG);
mTextPaint.setAntiAlias(true);
mTextPaint.setTextSize(textSize);
mTextPaint.setColor(0xffb5b5b4);
buttonPaint = new Paint(Paint.DITHER_FLAG);
buttonPaint.setAntiAlias(true);
//initData(null);
}
/**
* 实例化后调用,设置bar的段数和文字
*/
public void initData(ArrayList<String> section){
if(section != null){
section_title = section;
}else {
String[] str = new String[]{"低", "中", "高"};
section_title = new ArrayList<String>();
for (int i = ; i < str.length; i++) {
section_title.add(str[i]);
}
}
} @Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec); int widthMode = MeasureSpec.getMode(widthMeasureSpec);
int widthSize = MeasureSpec.getSize(widthMeasureSpec);
int heightMode = MeasureSpec.getMode(heightMeasureSpec);
int heightSize = MeasureSpec.getSize(heightMeasureSpec); width = widthSize;
float scaleX = widthSize / ;
float scaleY = heightSize / ;
scale = Math.max(scaleX,scaleY);
//控件的高度
// height = 185;
height = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, , getResources().getDisplayMetrics());
setMeasuredDimension(width, height);
width = width-bitMapHeight/;
perWidth = (width - section_title.size()*spot.getWidth() - thumb.getWidth()/) / (section_title.size()-);
hotarea = perWidth/;
} @Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
mPaint.setColor(Color.WHITE);
mPaint.setStyle(Paint.Style.FILL);
mPaint.setAlpha();
canvas.drawRect(, , getMeasuredWidth(), getMeasuredHeight(), mPaint);
canvas.drawBitmap(bitmap, , , null);
mPaint.setAlpha();
mPaint.setColor(colors[]);
canvas.drawLine(bitMapHeight, height * / , width - bitMapHeight - spot_on.getWidth() / , height * / , mPaint);
int section = ;
while(section < section_title.size()){
if(section < cur_sections) {
mPaint.setColor(colors[]);
canvas.drawLine(thumb.getWidth()/ + section * perWidth + (section+) * spot_on.getWidth(),height * / ,
thumb.getWidth()/ + section * perWidth + (section+) * spot_on.getWidth() + perWidth,height * / ,mPaint);
canvas.drawBitmap(spot_on, thumb.getWidth()/ + section * perWidth + section * spot_on.getWidth(),height * / - spot_on.getHeight()/,mPaint);
}else{
mPaint.setAlpha();
if(section == section_title.size()-){
canvas.drawBitmap(spot, width - spot_on.getWidth() - bitMapHeight/, height * / - spot.getHeight() / , mPaint);
}else {
canvas.drawBitmap(spot, thumb.getWidth()/ + section * perWidth + section * spot_on.getWidth(), height * / - spot.getHeight() / , mPaint);
}
} if(section == section_title.size()-) {
canvas.drawText(section_title.get(section), width - spot_on.getWidth()- bitMapHeight/ - textSize / , height * / - textMove, mTextPaint);
}else{
canvas.drawText(section_title.get(section), thumb.getWidth()/ + section * perWidth + section * spot_on.getWidth(), height * / - textMove, mTextPaint);
}
section++;
}
if(cur_sections == section_title.size()-){
canvas.drawBitmap(thumb, width - spot_on.getWidth() - bitMapHeight/ - thumb.getWidth() / ,
height * / - bitMapHeight, buttonPaint);
}else {
canvas.drawBitmap(thumb, thumb.getWidth()/ + cur_sections * perWidth + cur_sections * spot_on.getWidth() - thumb.getWidth()/ ,
height * / - bitMapHeight, buttonPaint);
}
} @Override
public boolean onTouchEvent(MotionEvent event) {
super.onTouchEvent(event);
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
thumb = BitmapFactory.decodeResource(getResources(), R.drawable.set_button_1);
downX = (int) event.getX();
downY = (int) event.getY();
responseTouch(downX, downY);
break;
case MotionEvent.ACTION_MOVE:
thumb = BitmapFactory.decodeResource(getResources(), R.drawable.set_button_1);
moveX = (int) event.getX();
moveY = (int) event.getY();
responseTouch(moveX, moveY);
break;
case MotionEvent.ACTION_UP:
thumb = BitmapFactory.decodeResource(getResources(), R.drawable.set_button_0);
upX = (int) event.getX();
upY = (int) event.getY();
responseTouch(upX, upY);
responseOnTouch.onTouchResponse(cur_sections);
break;
}
return true;
}
private void responseTouch(int x, int y){
if(x <= width-bitMapHeight/) {
cur_sections = (x + perWidth / ) / perWidth;
}else{
cur_sections = section_title.size()-;
}
invalidate();
} //设置监听
public void setResponseOnTouch(ResponseOnTouch response){
responseOnTouch = response;
} //设置进度
public void setProgress(int progress){
cur_sections = progress;
invalidate();
}
}</span>

Android 自定义带刻度的seekbar的更多相关文章

  1. 带你体验Android自定义圆形刻度罗盘 仪表盘 实现指针动态改变

    带你体验Android自定义圆形刻度罗盘 仪表盘 实现指针动态改变 转 https://blog.csdn.net/qq_30993595/article/details/78915115   近期有 ...

  2. Android自定义带标题边框的Layout

    今天工作中又碰到个小问题,项目需要用到像Java Swing的JPanel一样带标题边框的布局,Android里没有类似控件,想到这个也不难,自己画了一个,是继承LinearLayout的一个自定义布 ...

  3. Android -- 自定义带进度条的按钮

    1. 实现了一个带进度条的按钮,完成后显示提示信息,并设置按钮为不可再次被点击

  4. Android 自定义带回调的Dialog 及EditText相关

      import android.app.Activity; import android.content.Context; import android.text.Editable; import ...

  5. wpf自定义带刻度的柱状图控件

    效果图: 主要代码xaml: <UserControl x:Class="INSControls._01Conning.Steer.ConningSpeedBar" xmln ...

  6. Android实现自定义带文字和图片的Button

    Android实现自定义带文字和图片的Button 在Android开发中经常会需要用到带文字和图片的button,下面来讲解一下常用的实现办法. 一.用系统自带的Button实现 最简单的一种办法就 ...

  7. Android自定义Seekbar拖动条式样

    SeekBar拖动条可以由用户控制,进行拖动操作.比如,应用程序中用户需要对音量进行控制,就可以使用拖动条来实现. 1.SeekBar控件的使用 1.1SeekBar常用属性 SeekBar的常用属性 ...

  8. [转]Android UI:看看Google官方自定义带旋转动画的ImageView-----RotateImageView怎么写(附 图片淡入淡出效果)

    http://blog.csdn.net/yanzi1225627/article/details/22439119 众所周知,想要让ImageView旋转的话,可以用setRotation()让其围 ...

  9. 我的Android进阶之旅------>Android自定义View实现带数字的进度条(NumberProgressBar)

    今天在Github上面看到一个来自于 daimajia所写的关于Android自定义View实现带数字的进度条(NumberProgressBar)的精彩案例,在这里分享给大家一起来学习学习!同时感谢 ...

随机推荐

  1. php经典面试题与答案(转先锋教程网)

    php经典面试题与答案 时间:2016-02-29 16:06:23来源:网络 导读:php经典面试题与答案,包括腾讯php面试题.百度php面试题.新浪php面试题等   php面试题及答案 1,禁 ...

  2. java攻城狮之路(Android篇)--SQLite

    一.Junit    1.怎么使用        在AndroidManifest.xml文件中进行配置, 在manifest借点下配置instrumentation, 在application借点下 ...

  3. jQuery手机端上拉刷新下拉加载更多页面

    基于jQuery手机端上拉下拉刷新页面代码.这是一款类似QQ空间客户端或者微信下拉刷新页面特效代码.效果图如下: 在线预览   源码下载 实现的代码. html代码: <div id=" ...

  4. Java知多少(110)数据库之插入记录

    插入数据表记录有3种方案 一.使用Statement对象 实现插入数据表记录的SQL语句的语法是: insert into 表名(字段名1,字段名2,……)value (字段值1,字段值2,……) 例 ...

  5. WEB 安全之 SQL注入 < 三 > 提权

    SQL注入是一个比较“古老”的话题,虽然现在存在这种漏洞的站点比较少了,我们还是有必要了解一下它的危害,及其常用的手段,知己知彼方能百战不殆.进攻与防守相当于矛和盾的关系,我们如果能清楚了解 攻击的全 ...

  6. redis配置文件

    # redis 配置文件示例 # 当你需要为某个配置项指定内存大小的时候,必须要带上单位, # 通常的格式就是 1k 5gb 4m 等酱紫: # # 1k => bytes # 1kb => ...

  7. 基于HTML5实现的Heatmap热图3D应用

    Heatmap热图通过众多数据点信息,汇聚成直观可视化颜色效果,热图已广泛被应用于气象预报.医疗成像.机房温度监控等行业,甚至应用于竞技体育领域的数据分析. 已有众多文章分享了生成Heatmap热图原 ...

  8. Eclipse魔法堂:任务管理器

    一.前言        Eclipse的任务管理器为我们提供一个方便的入口查看工程代办事宜,并定位到对应的代码行继续之前的工作. 二.使用示例        示例1: /** * @Descripti ...

  9. [C#] 谈谈异步编程async await

    为什么需要异步,异步对可能起阻止作用的活动(例如,应用程序访问 Web 时)至关重要. 对 Web 资源的访问有时很慢或会延迟. 如果此类活动在同步过程中受阻,则整个应用程序必须等待. 在异步过程中, ...

  10. SpringMVC核心——视图渲染(包含视图解析)问题

    一.本来想说的是返回值处理问题,但在 SpringMVC 中,返回值处理问题的核心就是视图渲染.所以这里标题叫视图渲染问题. 本来想在上一篇文章中对视图解析进行说明的,但是通过源码发现,它应该算到视图 ...