android实现年龄段选择器
程序员文章站
2022-05-16 12:28:13
本文实例为大家分享了android实现年龄段选择器的具体代码,供大家参考,具体内容如下
效果就是滑动圆形按钮选择时间,废话不多说,先上工具类
imp...
本文实例为大家分享了android实现年龄段选择器的具体代码,供大家参考,具体内容如下
效果就是滑动圆形按钮选择时间,废话不多说,先上工具类
import android.view.view; import android.content.context; import android.graphics.bitmap; import android.graphics.bitmapfactory; import android.graphics.canvas; import android.graphics.paint; import android.util.attributeset; import android.view.motionevent; import com.zhiziyun.dmptest.bot.r; /** * created by administrator on 2018/7/27. */ public class rangeseekbar extends view { private float linewidth = 5.0f; private float textsize = 25.0f; private int inrangecolor = 0xff247ab7; private int outrangecolor = 0xff777777; private int textcolor = 0xff247ab7; private int textmarginbottom = 10; private int lowercenterx; private int uppercenterx; private int bmpwidth; private int bmpheight; private bitmap lowerbmp; private bitmap upperbmp; private paint inrangepaint; private paint outrangepaint; private paint bmppaint; private paint textpaint; private boolean islowermoving = false; private boolean isuppermoving = false; private onrangechangedlistener onrangechangedlistener; private int paddingleft = 50; private int paddingright = 50; private int paddingtop = 50; private int paddingbottom = 10; private int lineheight; private int linelength = 400; private int linestart = paddingleft; private int lineend = linelength + paddingleft; private float smallvalue = 13.0f;//最小值 private float bigvalue = 60.0f;//最大值 private float smallrange = smallvalue; private float bigrange = bigvalue; private int textheight; public rangeseekbar(context context) { super(context); init(); } public rangeseekbar(context context, attributeset attrs) { super(context, attrs); init(); } public rangeseekbar(context context, attributeset attrs, int defstyle) { super(context, attrs, defstyle); init(); } private void init() { lowerbmp = bitmapfactory.decoderesource(getresources(), r.drawable.circular);//圆形按钮图标,自己设置 upperbmp = bitmapfactory.decoderesource(getresources(), r.drawable.circular);//圆形按钮图标,自己设置 bmpwidth = upperbmp.getwidth(); bmpheight = upperbmp.getheight(); lowercenterx = linestart; uppercenterx = lineend; lineheight = getheight() - paddingbottom - lowerbmp.getheight() / 2; textheight = lineheight + lowerbmp.getheight() / 2 + 10; } private void initpaint() { // 绘制范围内的线条 inrangepaint = new paint(); inrangepaint.setantialias(true); inrangepaint.setstrokewidth(linewidth); inrangepaint.setcolor(inrangecolor); // 绘制范围外的线条 outrangepaint = new paint(); outrangepaint.setantialias(true); outrangepaint.setstrokewidth(linewidth); outrangepaint.setcolor(outrangecolor); // 画图片滑块 bmppaint = new paint(); // 画范围文字 textpaint = new paint(); textpaint.setcolor(textcolor); textpaint.settextsize(textsize); textpaint.setantialias(true); textpaint.setstrokewidth(linewidth); } private int measurewidth(int measurespec) { int result = 0; int specmode = measurespec.getmode(measurespec); int specsize = measurespec.getsize(measurespec); if (specmode == measurespec.exactly) { result = specsize; } else { result = paddingleft + paddingright + bmpwidth * 2; if (specmode == measurespec.at_most) { result = math.min(result, specsize); } } return result; } private int measureheight(int measureheight) { int result = 0; int specmode = measurespec.getmode(measureheight); int specsize = measurespec.getsize(measureheight); if (specmode == measurespec.exactly) { result = bmpheight * 2; } else { result = bmpheight + paddingtop; if (specmode == measurespec.at_most) { result = math.min(result, specsize); } } return result; } @override protected void onmeasure(int widthmeasurespec, int heightmeasurespec) { widthmeasurespec = measurewidth(widthmeasurespec); heightmeasurespec = measureheight(heightmeasurespec); setmeasureddimension(widthmeasurespec, heightmeasurespec); } @override protected void ondraw(canvas canvas) { super.ondraw(canvas); bmpwidth = upperbmp.getwidth(); bmpheight = upperbmp.getheight(); lineheight = getheight() - paddingbottom - lowerbmp.getheight() / 2; textheight = lineheight - bmpheight / 2 - textmarginbottom; // 画线 paint linepaint = new paint(); linepaint.setantialias(true); linepaint.setstrokewidth(linewidth); // 绘制处于图片滑块之间线段 linepaint.setcolor(inrangecolor); canvas.drawline(lowercenterx, lineheight, uppercenterx, lineheight, linepaint); // 绘制处于图片滑块两端的线段 linepaint.setcolor(outrangecolor); canvas.drawline(linestart, lineheight, lowercenterx, lineheight, linepaint); canvas.drawline(uppercenterx, lineheight, lineend, lineheight, linepaint); // 画图片滑块 paint bmppaint = new paint(); canvas.drawbitmap(lowerbmp, lowercenterx - bmpwidth / 2, lineheight - bmpheight / 2, bmppaint); canvas.drawbitmap(lowerbmp, uppercenterx - bmpwidth / 2, lineheight - bmpheight / 2, bmppaint); // 画范围文字 paint textpaint = new paint(); textpaint.setcolor(textcolor); textpaint.settextsize(textsize); textpaint.setantialias(true); textpaint.setstrokewidth(linewidth); canvas.drawtext(string.format("%.0f岁", smallrange), lowercenterx - bmpwidth / 2, textheight, textpaint); canvas.drawtext(string.format("%.0f岁", bigrange), uppercenterx - bmpwidth / 2, textheight, textpaint); } @override public boolean ontouchevent(motionevent event) { super.ontouchevent(event); float xpos = event.getx(); switch (event.getaction()) { case motionevent.action_down: // 如果按下的位置在垂直方向没有与图片接触,则不会滑动滑块 float ypos = event.gety(); if (math.abs(ypos - lineheight) > bmpheight / 2) { return false; } // 表示当前按下的滑块是左边的滑块 if (math.abs(xpos - lowercenterx) < bmpwidth / 2) { islowermoving = true; } // //表示当前按下的滑块是右边的滑块 if (math.abs(xpos - uppercenterx) < bmpwidth / 2) { isuppermoving = true; } // 单击左边滑块的左边线条时,左边滑块滑动到对应的位置 if (xpos >= linestart && xpos <= lowercenterx - bmpwidth / 2) { lowercenterx = (int) xpos; updaterange(); postinvalidate(); } // 单击右边滑块的右边线条时, 右边滑块滑动到对应的位置 if (xpos <= lineend && xpos >= uppercenterx + bmpwidth / 2) { uppercenterx = (int) xpos; updaterange(); postinvalidate(); } break; case motionevent.action_move: // 滑动左边滑块时 if (islowermoving) { if (xpos >= linestart && xpos < uppercenterx - bmpwidth) { lowercenterx = (int) xpos; updaterange(); postinvalidate(); } } // 滑动右边滑块时 if (isuppermoving) { if (xpos > lowercenterx + bmpwidth && xpos < lineend) { uppercenterx = (int) xpos; updaterange(); postinvalidate(); } } break; case motionevent.action_up: // 修改滑块的滑动状态为不再滑动 islowermoving = false; isuppermoving = false; break; default: break; } return true; } // 计算指定滑块对应的范围值 private float computrange(float range) { return (range - linestart) * (bigvalue - smallvalue) / linelength + smallvalue; } // 滑动滑块的过程中,更新滑块上方的范围标识 private void updaterange() { smallrange = computrange(lowercenterx); bigrange = computrange(uppercenterx); if (null != onrangechangedlistener) { onrangechangedlistener.onrangechanged(smallrange, bigrange); } } // 注册滑块范围值改变事件的监听 public void setonrangechangedlistener( onrangechangedlistener onrangechangedlistener) { this.onrangechangedlistener = onrangechangedlistener; } // 公共接口,用户回调接口范围值的改变 public interface onrangechangedlistener { public void onrangechanged(float lowerrange, float upperrange); } }
在xml中
<com.zhiziyun.dmptest.bot.util.rangeseekbar android:id="@+id/rangeseekbar" android:layout_width="match_parent" android:layout_height="wrap_content" />
最后在代码中调用
rangeseekbar = (rangeseekbar) findviewbyid(r.id.rangeseekbar); rangeseekbar.setonrangechangedlistener(new rangeseekbar.onrangechangedlistener() { @override public void onrangechanged(float lowerrange, float upperrange) { tv_age.settext((int) lowerrange + "~" + (int) upperrange); } });
写完收工。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
上一篇: 盘点:欧洲5大最便宜最美旅游目的
下一篇: 沙发客解读为何印度贱民命运与生俱来