Customize a control that implements a selected range of values, with a scale of 100 equal to equal. Mimic the controls in the used Car app to filter the selected interval.
Implementation results:
First, divide the control into four parts, position markers, ticks, rulers, and cursors.
One, measure the scale of the sub-control, Inherit Onmeasure (). Size of the child space to determine the size of the control.
@Override protected synchronized void onmeasure (int widthmeasurespec, int heightmeasurespec) { Measurechildren (WIDTHMEASURESPEC,HEIGHTMEASURESPEC); Measure child control super.onmeasure (Widthmeasurespec, heightmeasurespec); int mwidth=measurespec.getsize (WIDTHMEASURESPEC); Propaddingleftandright=mthumbleft.getmeasuredwidth ()/2; Mleftlimit=propaddingleftandright; Mrightlimit=mwidth-propaddingleftandright; Height of position marker + ruler height + height of ruler + cursor height setmeasureddimension (mwidth,mthumbplaceheight+rule_height_px+ Mprogressbarheight+mthumbleft.getmeasuredheight ()); }
Second, lay out the controls.
Since the cursor needs to listen to his sliding events, a cursor control thumbview is defined here.
@Override protected void OnLayout (Boolean changed, int l, int t, int r, int b) { int heightsum=0; Heightsum+=mthumbplaceheight; heightsum+=rule_height_px; Heightsum+=mprogressbarheight; Mpartwidth= (Mrightlimit-mleftlimit)/(float) mmaxvalue; Calculate a share of the width must be used float mthumbleft.setlimit (mleftlimit,mrightlimit); Set the range that can be moved mthumbleft.layout (0,heightsum,mthumbleft.getmeasuredwidth (), b-10); Sets the location of the parent layout mthumbright.setlimit (mleftlimit,mrightlimit); Mthumbright.layout (0,heightsum,mthumbleft.getmeasuredwidth (), b-10); Onlayoutprepared (); Method called after layout call, such as setting thumb limit }
Three, draw the ruler, the ruler's scale, the ruler's position marks the control.
Draw a ruler, where Mprobaseline is the top position of the ruler. The range of the ruler is actually two different rectangular boxes.
/** * Draw Ruler * @param canvas * /protected void Drawprogressbar (canvas canvas) { //Paint background paint paint =new Paint (); Paint.setantialias (true); Paint.setcolor (Getresources (). GetColor (R.color.grey)); Rect rect=new rect (mleftlimit,mprobaseline,mrightlimit,mprobaseline+mprogressbarheight); Canvas.drawrect (rect,paint); Draw Progress Paint.setcolor (Getresources (). GetColor (R.color.blue)); Rect=new Rect (Mthumbleft.getcenterx (), Mprobaseline,mthumbright.getcenterx (), mprobaseline+mprogressbarheight); Canvas.drawrect (Rect,paint); }
Draw the scale, where the ruler is divided into 100 equal parts, and then the first 5 parts and 5 parts do not draw, the middle part of every two copies of a line, every 10 parts draw a long-term.
protected void Drawrule (canvas canvas) {Paint paint=new paint (); Paint.setstrokewidth (1); Paint.setcolor (Getresources (). GetColor (R.color.grey)); Paint.settextsize (20); Paint.settextalign (Paint.Align.CENTER); Paint.setantialias (TRUE); Traversing two parts at a time, the plotted position is in odd position for (int i=5;i<=mmaxvalue;i+=2) {if (i<part_item| | I>mmaxvalue-part_item) {continue; } float Degx=mleftlimit+i*mpartwidth; int degy; if ((i-part_item)% (part_item*2) ==0) {degy=mprobaseline-densityutil.dip2px (GetContext (), longline_height); Canvas.drawtext (degs[(i-5)/10]+unitstr,degx,degy,paint); Draw Text}else{degy=mprobaseline-densityutil.dip2px (GetContext (), shortline_height); } canvas.drawline (Degx,mprobaseline,degx,degy,paint); } }Draws a markup control that determines whether a marker value needs to be drawn based on the Ismoving property of the cursor Thumbview. Ismoving true to draw, false not to draw
/** * Draw Thumb Position value (TAG) */ protected void Drawrodplacevalue (Canvas canvas,thumbview thumbview) { int Centerx=thumbview.getcenterx (); Paint paint=new paint (); Bitmapdrawable bd= (bitmapdrawable) mthumbplacedrawable; Canvas.drawbitmap (Bd.getbitmap (), Centerx-mthumbplacedrawable.getintrinsicwidth ()/2,0,paint); Paint.setcolor (color.white); Paint.settextalign (Paint.Align.CENTER); Paint.settextsize (+); Canvas.drawtext (Genearethumbvalue (Thumbview) + "", Centerx,mthumbdrawable.getintrinsicheight ()/2,paint); }
Four, related calculations
To calculate the progress based on the position of the cursor on the scale.
private int Genearethumbvalue (Thumbview view) { //todo here just calculates the value of 100 and needs to convert itself to the value on the scale int provalue=mmaxvalue* ( View.getcenterx ()-mleftlimit)/(mrightlimit-mleftlimit); return provalue; }
Five, cursor control Thumbview
1,mleftlimit, Mrightlimit is the position where the cursor can float.
2, when the touch event is move, change its position.
Package Com.jayce.uidefine;import Android.content.context;import Android.graphics.rect;import Android.util.attributeset;import android.view.motionevent;import android.widget.imageview;/** * @author Jayce * @date 2015/3/12 */public class Thumbview extends ImageView {private Rangeseekbar rangeseekbar; private int mdownx=0; private int mwidth; private int mleftlimit=0; private int mrightlimit=integer.max_value; Private rect rect; private int Mcenterx; The center position of the cursor is private boolean mismoving; Whether the cursor is moving private onthumblistener listener; Public Thumbview (Context context) {This (context, NULL); } public Thumbview (context context, AttributeSet Attrs) {This (context, attrs, 0); } public Thumbview (context context, AttributeSet attrs, int defstyleattr) {Super (context, attrs, defstyleattr); } public void Setrangeseekbar (Rangeseekbar rangeseekbar) {this.rangeseekbar = Rangeseekbar; } public void Setlimit (int mLEftlimit,int mrightlimit) {this.mleftlimit = Mleftlimit; This.mrightlimit=mrightlimit; } public int Getcenterx () {return mcenterx; }/** * Sets the center position, does not exceed the left and right limit, refreshes the entire control, and callbacks Onthumbchange () * @param centerx */public void Setcenterx (int ce Nterx) {int left=centerx-mwidth/2,right=centerx+mwidth/2; if (centerx<mleftlimit) {LEFT=MLEFTLIMIT-MWIDTH/2; RIGHT=MLEFTLIMIT+MWIDTH/2; } if (centerx>mrightlimit) {LEFT=MRIGHTLIMIT-MWIDTH/2; RIGHT=MRIGHTLIMIT+MWIDTH/2; } This.mcenterx = (left+right)/2; if (left!=rect.left| | Right!=rect.right) {rect.union (left,rect.top,right,rect.bottom); Layout (left, rect.top, right, rect.bottom); Invalidate (rect); Rangeseekbar.invalidate (); if (listener!=null) {Listener.onthumbchange (100* (left+right)/2-mleftlimit)/(Mrightlimit-mleftlimit)); } }} public Boolean ismoving () {return mismoving; } public void Setonthumblistener (Onthumblistener listener) {This.listener = listener; } @Override protected void onmeasure (int widthmeasurespec, int heightmeasurespec) {super.onmeasure (widthmeas Urespec, Heightmeasurespec); Mwidth=getmeasuredwidth (); } @Override protected void OnLayout (Boolean changed, int left, int top, int. right, int bottom) {Super.onlayo UT (changed, left, top, right, bottom); Rect=new Rect (Left,top,right,bottom); } @Override public boolean ontouchevent (Motionevent event) {switch (event.getaction ()) {case Mot IonEvent.ACTION_DOWN:mDownX = (int) event.getx (); Mismoving=false; Break Case MotionEvent.ACTION_MOVE:int nowx = (int) event.getx (); int left = Rect.left + Nowx-mdownx; int right = Rect.right + Nowx-mdoWnx; Mismoving=true; Setcenterx ((left+right)/2); Break Case Motionevent.action_up:mismoving=false; Rangeseekbar.invalidate (); Break } return true; } public interface onthumblistener{public void onthumbchange (int i); }}
Rangeseekbarview a control that selects a range value