
想整个双向的进度条,就是可以选取播放范围的。
像这样:
然而官方控件里只有单向的。不要慌,我们自己画一个。
绘制一个进度条主要是三方面。1.样式,2.尺寸,3. *** 作监听。
完整代码来一遍:
注释基本上就把原理说明了一下。
package util;import androID.content.Context;import androID.graphics.Canvas;import androID.graphics.color;import androID.graphics.Paint;import androID.graphics.drawable.Drawable;import androID.support.v4.content.ContextCompat;import androID.util.AttributeSet;import androID.util.Log;import androID.vIEw.MotionEvent;import androID.vIEw.VIEw;import com.example.qzd.utildemo.R;import java.math.BigDecimal;/** * 双向滑块的进度条(区域选择) */public class SeekRangebar extends VIEw { private Context _context; private static final int CliCK_ON_LOW = 1; //手指在前滑块上滑动 private static final int CliCK_ON_HIGH = 2; //手指在后滑块上滑动 private static final int CliCK_IN_LOW_AREA = 3; //手指点击离前滑块近 private static final int CliCK_IN_HIGH_AREA = 4; //手指点击离后滑块近 private static final int CliCK_OUT_AREA = 5; //手指点击在vIEw外 private static final int CliCK_INVAILD = 0; private static final int[] STATE_norMAL = {}; private static final int[] STATE_pressed = {androID.R.attr.state_pressed,androID.R.attr.state_window_focused,}; private static int mThumbmargintop = 0; //滑动块顶部离vIEw顶部的距离 private static int mTextVIEwmargintop = 0; //当前滑块文字距离vIEw顶部距离 private Drawable hasScrollbarBg; //滑动条滑动后背景图 private Drawable notScrollbarBg; //滑动条未滑动背景图 private Drawable mThumbLow; //前滑块 private Drawable mThumbHigh; //后滑块 private int mScollbarWIDth; //控件宽度 = 滑动条宽度 + 滑动块宽度 private int mScollbarHeight; //控件高度 private int mThumbWIDth; //滑动块直径 private double mOffsetLow = 0; //前滑块中心坐标 private double mOffsetHigh = 0; //后滑块中心坐标 private int mdistance=0; //总刻度是固定距离 两边各去掉半个滑块距离 private int mFlag = CliCK_INVAILD; //手指按下的类型 private double defaultScreenLow = 0; //默认前滑块位置百分比 private double defaultScreenHigh = 100; //默认后滑块位置百分比 private OnSeekbarchangelistener mbarchangelistener; private boolean editable=false;//是否处于可编辑状态 private int miniGap=5;//AB的最小间隔 private double progressLow;//起点(百分比) private double progressHigh;//终点 public SeekRangebar(Context context) { this(context,null); } public SeekRangebar(Context context,AttributeSet attrs) { this(context,attrs,0); } public SeekRangebar(Context context,AttributeSet attrs,int defStyle) { super(context,defStyle); _context=context; //这里设置背景图及滑块图,自定义过进度条的同学应该很熟悉了 notScrollbarBg = ContextCompat.getDrawable(_context,R.mipmap.hp_wbf); hasScrollbarBg = ContextCompat.getDrawable(_context,R.mipmap.hp_ybf); mThumbLow = ContextCompat.getDrawable(_context,R.mipmap.hp_a); mThumbHigh = ContextCompat.getDrawable(_context,R.mipmap.hp_b); mThumbLow.setState(STATE_norMAL); mThumbHigh.setState(STATE_norMAL); //设置滑动条高度 mScollbarHeight = notScrollbarBg.getIntrinsicHeight(); //设置滑动块直径 mThumbWIDth = mThumbLow.getIntrinsicWIDth(); } /** * 测量vIEw尺寸(在onDraw()之前) * @param wIDthMeasureSpec * @param heightmeasureSpec */ protected voID onMeasure(int wIDthMeasureSpec,int heightmeasureSpec) { int wIDth = MeasureSpec.getSize(wIDthMeasureSpec); mScollbarWIDth = wIDth; if(mdistance==0) {//这里滑块中心坐标初始化的时候测量一下(根据mdistance是否赋值判断),并不需要不停地去测量。后面会根据进度计算滑块位置。 mOffsetLow = mThumbWIDth / 2; mOffsetHigh = wIDth - mThumbWIDth / 2; } mdistance = wIDth - mThumbWIDth; if(defaultScreenLow != 0) { mOffsetLow = formatInt(defaultScreenLow / 100 * (mdistance)) + mThumbWIDth / 2; } if(defaultScreenHigh != 100) { mOffsetHigh = formatInt(defaultScreenHigh / 100 * (mdistance)) + mThumbWIDth / 2; } setMeasuredDimension(wIDth,mThumbWIDth + mThumbmargintop + 2); } protected voID onLayout(boolean changed,int l,int t,int r,int b) { super.onLayout(changed,l,t,r,b); } /** * 绘制进度条 */ protected voID onDraw(Canvas canvas) { super.onDraw(canvas); //设置绘制样式 Paint text_Paint = new Paint(); text_Paint.setTextAlign(Paint.Align.CENTER); text_Paint.setcolor(color.RED); text_Paint.setTextSize(20); int top = mThumbmargintop + mThumbWIDth / 2 - mScollbarHeight / 2; int bottom = top + mScollbarHeight; //绘制是否可 *** 作状态的下的不同样式,仅可编辑状态下显示进度条 if(editable) { //白色滑动条,两个滑块各两边部分 notScrollbarBg.setBounds(mThumbWIDth / 2,top,mScollbarWIDth - mThumbWIDth / 2,bottom); notScrollbarBg.draw(canvas); //红色滑动条,两个滑块中间部分 hasScrollbarBg.setBounds((int) mOffsetLow,(int) mOffsetHigh,bottom); hasScrollbarBg.draw(canvas); } //前滑块 mThumbLow.setBounds((int) (mOffsetLow - mThumbWIDth / 2),mThumbmargintop,(int) (mOffsetLow + mThumbWIDth / 2),mThumbWIDth + mThumbmargintop); mThumbLow.draw(canvas); //后滑块 mThumbHigh.setBounds((int) (mOffsetHigh - mThumbWIDth / 2),(int) (mOffsetHigh + mThumbWIDth / 2),mThumbWIDth + mThumbmargintop); mThumbHigh.draw(canvas); //当前滑块刻度 progressLow = formatInt((mOffsetLow - mThumbWIDth / 2) * 100 / mdistance); progressHigh = formatInt((mOffsetHigh - mThumbWIDth / 2) * 100 / mdistance); canvas.drawText((int) progressLow + "",(int) mOffsetLow - 2 - 2,mTextVIEwmargintop,text_Paint); canvas.drawText((int) progressHigh + "",(int) mOffsetHigh - 2,text_Paint); if (mbarchangelistener != null) { mbarchangelistener.onProgressChanged(this,progressLow,progressHigh); } } //手势监听 @OverrIDe public boolean ontouchEvent(MotionEvent e) { if(!editable) { return false; } if (e.getAction() == MotionEvent.ACTION_DOWN) { mFlag = getAreaFlag(e); if (mFlag == CliCK_ON_LOW) { mThumbLow.setState(STATE_pressed); } else if (mFlag == CliCK_ON_HIGH) { mThumbHigh.setState(STATE_pressed); } else if (mFlag == CliCK_IN_LOW_AREA) { mThumbLow.setState(STATE_pressed); mThumbHigh.setState(STATE_norMAL); //如果点击0-mThumbWIDth/2坐标 if (e.getX() < 0 || e.getX() <= mThumbWIDth / 2) { mOffsetLow = mThumbWIDth / 2; } else if (e.getX() > mScollbarWIDth - mThumbWIDth / 2) { mOffsetLow = mThumbWIDth / 2 + mdistance; } else { mOffsetLow = formatInt(e.getX()); } } else if (mFlag == CliCK_IN_HIGH_AREA) { mThumbHigh.setState(STATE_pressed); mThumbLow.setState(STATE_norMAL); if (e.getX() >= mScollbarWIDth - mThumbWIDth / 2) { mOffsetHigh = mdistance + mThumbWIDth / 2; } else { mOffsetHigh = formatInt(e.getX()); } } //更新滑块 invalIDate(); } else if (e.getAction() == MotionEvent.ACTION_MOVE) { if (mFlag == CliCK_ON_LOW) { if (e.getX() < 0 || e.getX() <= mThumbWIDth / 2) { mOffsetLow = mThumbWIDth / 2; } else if (e.getX() >= mScollbarWIDth - mThumbWIDth / 2) { mOffsetLow = mThumbWIDth / 2 + mdistance; mOffsetHigh = mOffsetLow; } else { mOffsetLow = formatInt(e.getX()); if (mOffsetHigh - mOffsetLow <= 0) { mOffsetHigh = (mOffsetLow <= mdistance + mThumbWIDth / 2) ? (mOffsetLow) : (mdistance + mThumbWIDth / 2); } } } else if (mFlag == CliCK_ON_HIGH) { if (e.getX() < mThumbWIDth / 2) { mOffsetHigh = mThumbWIDth / 2; mOffsetLow = mThumbWIDth / 2; } else if (e.getX() > mScollbarWIDth - mThumbWIDth / 2) { mOffsetHigh = mThumbWIDth / 2 + mdistance; } else { mOffsetHigh = formatInt(e.getX()); if (mOffsetHigh - mOffsetLow <= 0) { mOffsetLow = (mOffsetHigh >= mThumbWIDth / 2) ? (mOffsetHigh) : mThumbWIDth / 2; } } } //更新滑块,每次滑块有动作都要执行此函数触发onDraw方法绘制新图片 invalIDate(); } else if (e.getAction() == MotionEvent.ACTION_UP) { Log.d("LOGCAT","ACTION UP:"+progressHigh+"-"+progressLow); mThumbLow.setState(STATE_norMAL); mThumbHigh.setState(STATE_norMAL); if(miniGap>0 && progressHigh<progressLow+miniGap){ progressHigh=progressLow+miniGap; this.defaultScreenHigh = progressHigh; mOffsetHigh = formatInt(progressHigh / 100 * (mdistance)) + mThumbWIDth / 2; invalIDate(); } } return true; } /** * 设置是否可编辑状态,非可编辑状态将不能对AB点进行 *** 作 * @param _b */ public voID setEditable(boolean _b){ editable=_b; invalIDate(); } /** * 获取当前手指位置 */ public int getAreaFlag(MotionEvent e) { int top = mThumbmargintop; int bottom = mThumbWIDth + mThumbmargintop; if (e.getY() >= top && e.getY() <= bottom && e.getX() >= (mOffsetLow - mThumbWIDth / 2) && e.getX() <= mOffsetLow + mThumbWIDth / 2) { return CliCK_ON_LOW; } else if (e.getY() >= top && e.getY() <= bottom && e.getX() >= (mOffsetHigh - mThumbWIDth / 2) && e.getX() <= (mOffsetHigh + mThumbWIDth / 2)) { return CliCK_ON_HIGH; } else if (e.getY() >= top && e.getY() <= bottom && ((e.getX() >= 0 && e.getX() < (mOffsetLow - mThumbWIDth / 2)) || ((e.getX() > (mOffsetLow + mThumbWIDth / 2)) && e.getX() <= ((double) mOffsetHigh + mOffsetLow) / 2))) { return CliCK_IN_LOW_AREA; } else if (e.getY() >= top && e.getY() <= bottom && (((e.getX() > ((double) mOffsetHigh + mOffsetLow) / 2) && e.getX() < (mOffsetHigh - mThumbWIDth / 2)) || (e.getX() > (mOffsetHigh + mThumbWIDth / 2) && e.getX() <= mScollbarWIDth))) { return CliCK_IN_HIGH_AREA; } else if (!(e.getX() >= 0 && e.getX() <= mScollbarWIDth && e.getY() >= top && e.getY() <= bottom)) { return CliCK_OUT_AREA; } else { return CliCK_INVAILD; } } /** * 设置前滑块位置 * @param progressLow */ public voID setProgressLow(double progressLow) { this.defaultScreenLow = progressLow; mOffsetLow = formatInt(progressLow / 100 * (mdistance)) + mThumbWIDth / 2; invalIDate(); } /** * 设置后滑块位置 * @param progressHigh */ public voID setProgressHigh(double progressHigh) { this.defaultScreenHigh = progressHigh; mOffsetHigh = formatInt(progressHigh / 100 * (mdistance)) + mThumbWIDth / 2; invalIDate(); } /** * 设置滑动监听 * @param mListener */ public voID setonSeekbarchangelistener(OnSeekbarchangelistener mListener) { this.mbarchangelistener = mListener; } /** * 滑动监听,改变输入框的值 */ public interface OnSeekbarchangelistener { //滑动时 public voID onProgressChanged(SeekRangebar seekbar,double progressLow,double progressHigh); } /** * 设置滑动结果为整数 */ private int formatInt(double value) { BigDecimal bd = new BigDecimal(value); BigDecimal bd1 = bd.setScale(0,BigDecimal.ROUND_HALF_UP); return bd1.intValue(); }}然后就可以在程序中使用了。
布局中
<util.SeekRangebar androID:ID="@+ID/doubleSeekbar" androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:layout_centerVertical="true"/>
调用
private SeekRangebar doubleSeekbar;//双向进度条doubleSeekbar = (SeekRangebar) findVIEwByID(R.ID.doubleSeekbar);//监听进度范围变化doubleSeekbar.setonSeekbarchangelistener(new SeekRangebar.OnSeekbarchangelistener() { @OverrIDe public voID onProgressChanged(SeekRangebar seekbar,double progressHigh) { Log.d("LOGCAT","低:" + progressLow + "高:" + progressHigh); }});相关GitHub项目地址:https://github.com/codeqian/android-class-lib
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。
以上是内存溢出为你收集整理的Android自定义双向进度条的实现代码全部内容,希望文章能够帮你解决Android自定义双向进度条的实现代码所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)