Points:
With the swipe of the finger update position
DrawText, how to calculate the starting position, so that the STR center
1.callsliderendview.java
PackageNet.mobctrl.callendview;ImportAndroid.annotation.SuppressLint;ImportAndroid.content.Context;ImportAndroid.content.res.TypedArray;ImportAndroid.graphics.Canvas;ImportAndroid.graphics.Color;ImportAndroid.graphics.Paint;ImportAndroid.graphics.Paint.Style;ImportAndroid.graphics.RectF;ImportAndroid.util.AttributeSet;ImportAndroid.view.MotionEvent;ImportAndroid.view.View;/** * @date February 2, 2015 PM 11:02:00 * @author Zheng haibo * @Description: Sliding off Custom controls * @We b http://www.mobctrl.net * /@SuppressLint({"Drawallocation","Clickableviewaccessibility"}) Public class callsliderendview extends View { Public interface sliderendlistener { Public void Onsliderend(); }PrivateSliderendlistener Sliderendlistener; Public void Setsliderendlistener(Sliderendlistener Sliderendlistener) { This. Sliderendlistener = Sliderendlistener; }Private intHeightPrivate intWidthPrivate intCircleoffset =0;Private intPrevX =0;Private intMaxoffset;PrivateString Slidertext;Private floatTextSize;Private intProgressbackgroundcolor;Private intBackgroundColor;Private intRedreginwidth; Public Callsliderendview(Context context) {Super(context); Init (Context,NULL); } Public Callsliderendview(context context, AttributeSet attrs) {Super(context, attrs); Init (context, attrs); } Public Callsliderendview(context context, AttributeSet attrs,intDEFSTYLEATTR) {Super(Context, attrs, defstyleattr); Init (context, attrs); }Private void Init(context context, AttributeSet attrs) {if(NULL= = Attrs) {return; } TypedArray TypedArray = Context.obtainstyledattributes (Attrs, R.styleable.callsliderendview); TextSize = Typedarray.getdimensionpixelsize (R.styleable.callsliderendview_textsize, +); Slidertext = typedarray.getstring (R.styleable.callsliderendview_text); Progressbackgroundcolor = Typedarray.getcolor (R.styleable.callsliderendview_progressbackgroundcolor, Color.green); BackgroundColor = Typedarray.getcolor (R.styleable.callsliderendview_backgroundcolor,0x0fffffff); Typedarray.recycle (); }@Override protected void OnDraw(Canvas canvas) {Super. OnDraw (canvas);//TODOHeight = getheight (); width = getwidth ();//Draw backgroundPaint paint =NewPaint (); Paint.setstyle (Style.fill); Paint.setantialias (true); Paint.setcolor (BackgroundColor); Drawbackground (canvas, paint);//Drawcirclebutton (canvas, paint);Drawroundbutton (canvas, paint); }//Draw background Private void Drawbackground(Canvas canvas, paint paint) {Canvas.drawroundrect (NewRECTF (0,0, width, height), height/2, Height/2, paint); }//Draw the view of the hang-off button @Deprecated Private void Drawcirclebutton(Canvas canvas, paint paint) {intCirclemargin = height/Ten; Paint.setcolor (color.red); Canvas.drawcircle (Height/2+ Circleoffset, Height/2, Height/2-Circlemargin, paint); }//Draw the view of the hang-off button Private void Drawroundbutton(Canvas canvas, paint paint) {redreginwidth = width/2;//Draw Progress backgroundPaint.setcolor (Progressbackgroundcolor); Canvas.drawroundrect (NewRECTF (Circleoffset,0, Circleoffset + redreginwidth, height), height/2, Height/2, paint);//Display text Slidertext in the middlePaint.settextsize (textSize); Paint.setcolor (Color.White);intYcenterpos = (int) ((Canvas.getheight ()/2)-((Paint.descent () + Paint. Ascent ())/2));//calculation in Y intStartX = Circleoffset + (Redreginwidth-(int) Paint.measuretext (Slidertext,0, Slidertext.length ()))/2; Canvas.drawtext (Slidertext, StartX, Ycenterpos, paint); }@Override Public Boolean ontouchevent(Motionevent event) {Switch(Event.getaction ()) { CaseMotionEvent.ACTION_DOWN:actionDown (event); Break; CaseMotionEvent.ACTION_MOVE:actionMove (event); Break; CaseMotionEvent.ACTION_UP:actionUp (event); Break; }return true; }Private void Actionup(Motionevent event) {if( This. circleoffset! = Maxoffset) { This. Circleoffset =0; } postinvalidate (); }Private void Actionmove(Motionevent event) {intTempoffset = (int) (Event.getx ()- This. PrevX); This. Maxoffset = Width-redreginwidth;if(Tempoffset >= Maxoffset && This. Circleoffset = = Maxoffset) {return; } This. Circleoffset = Tempoffset;if( This. Circleoffset > Maxoffset) {//Have you slipped to the edge This. Circleoffset = Maxoffset;if(Sliderendlistener! =NULL) {sliderendlistener.onsliderend (); } }if( This. Circleoffset <=0) { This. Circleoffset =0; } postinvalidate (); }Private void Actiondown(Motionevent e) { This. PrevX = (int) E.getx (); }}
2. Use
Layout Main.xml
<?xml version= "1.0" encoding= "Utf-8"?><relativelayout xmlns:android="Http://schemas.android.com/apk/res/android" Xmlns:app="Http://schemas.android.com/apk/res/net.mobctrl.callendview"android:id= "@+id/rl_root" android:layout_width="Match_parent"android:layout_height="Match_parent" Android:background="@android: Color/background_dark"android:orientation= "Horizontal" > <Net.mobctrl.callendview.CallSliderEndView Android:id="@+id/csev_slider_view" Android:layout_width="Match_parent" Android:layout_height="60DP" android:layout_centerinparent="true" Android:layout_margin="30DP" App:backgroundcolor="#ffffff" App:progressbackgroundcolor="#ffff1221" App:text="Slide Hang Off" app:textsize="24SP"/></relativelayout>
Mainactivity.java
PackageNet.mobctrl.callendview;ImportNet.mobctrl.callendview.CallSliderEndView.SliderEndListener;Importandroid.app.Activity;ImportAndroid.os.Bundle;ImportAndroid.widget.Toast;/** * * @author Zheng haibo * @web http://www.mobctrl.net * */ Public class mainactivity extends Activity { PrivateCallsliderendview Callsliderendview;@Override protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate); Setcontentview (R.layout.main); Callsliderendview = (Callsliderendview) Findviewbyid (R.id.csev_slider_view); Callsliderendview.setsliderendlistener (NewSliderendlistener () {@Override Public void Onsliderend() {System.out.println ("Debug:onsliderend ..."); Toast.maketext (Getapplicationcontext (),"Slider to End", Toast.length_short). Show (); } }); }}
3. Configuration of the attribute attr (/res/value/attr.xml)
<?xml version= "1.0" encoding= "Utf-8"?><resources> <declare-styleable name="Callsliderendview"> <attr name="backgroundcolor" format="Color" /> <attr name="Progressbackgroundcolor" format="Color" /> <attr name="textSize" format="Dimension" /> <attr name="text" format="string" /> </declare-styleable></Resources>
4. Effects
Users can swipe with their fingers
Project GitHub Address: GitHub
Android Development Alliance QQ Group:272209595
Not for commercial purposes without permission
Android: A simple implementation of sliding hang-up custom view