Android: simple implementation of sliding and hanging up custom views

Source: Internet
Author: User
Tags drawtext getcolor

Android: simple implementation of sliding and hanging up custom views
Key points:

Position updated as the finger slides

How to calculate the start position and center str in drawText

1. CallSliderEndView. java

Package net. mobctrl. callendview; import android. annotation. suppressLint; import android. content. context; import android. content. res. typedArray; import android. graphics. canvas; import android. graphics. color; import android. graphics. paint; import android. graphics. paint. style; import android. graphics. rectF; import android. util. attributeSet; import android. view. motionEvent; import android. view. view;/*** @ date 20 15 years February 2 11:02:00 * @ author Zheng Haibo * @ Description: Sliding hanging custom controls * @ Web http://www.mobctrl.net */@ SuppressLint ({DrawAllocation, ClickableViewAccessibility }) public class CallSliderEndView extends View {public interface SliderEndListener {public void onSliderEnd ();} private SliderEndListener sliderEndListener; public void setSliderEndListener (SliderEndListener sliderEndListener) {this. slide REndListener = sliderEndListener;} private int height; private int width; private int circleOffset = 0; private int prevX = 0; private int maxOffset; private String sliderText; private float textSize; private int progressBackgroundColor; private int backgroundColor; private int redReginWidth; public CallSliderEndView (Context context) {super (context); init (context, null);} public CallSliderEndVi Ew (Context context, AttributeSet attrs) {super (context, attrs); init (context, attrs);} public CallSliderEndView (Context context, AttributeSet attrs, int defStyleAttr) {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. callSl IderEndView); textSize = typedArray. getDimensionPixelSize (R. styleable. callSliderEndView_textSize, 40); 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); // TODO height = getHeight (); width = getWidth (); // Paint background paint = new Paint (); paint. setStyle (Style. FILL); paint. setAntiAlias (true); paint. setColor (backgroundColor); drawBackground (canvas, paint); // drawCircleButton (canvas, paint); drawRoundButton (canvas, paint);} // draw the background private void drawBackground (Canvas Canvas, Paint paint) {canvas. drawRoundRect (new RectF (0, 0, width, height), height/2, height/2, paint );} // draw the View @ Deprecated private void drawCircleButton (Canvas canvas, Paint paint) of the hanging button {int circleMargin = height/10; paint. setColor (Color. RED); canvas. drawCircle (height/2 + circleOffset, height/2, height/2-circleMargin, paint);} // draw the View private void drawRoundButton (Canva S canvas, Paint paint) {redReginWidth = width/2; // paint progress background. setColor (progressBackgroundColor); canvas. drawRoundRect (new RectF (circleOffset, 0, circleOffset + redReginWidth, height), height/2, height/2, paint); // display the text sliderText in the middle of the painting. setTextSize (textSize); paint. setColor (Color. WHITE); int yCenterPos = (int) (canvas. getHeight ()/2)-(paint. descent () + paint. ascent ()/2); // count Calculate it in Y int startX = 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 () {case MotionEvent. ACTION_DOWN: actionDown (event); break; case MotionEvent. ACTION_MOVE: actionMove (event); break; case MotionEvent. 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) {int tempOffset = (int) (event. getX ()-this. prevX); this. maxOffset = width-redReginWidth; if (tempOffset> = maxOffset & this. circleOffset = maxOffset) {return;} this. circleOffset = tempOffset; if (this. circleOffset> maxOffset) {// whether it has been moved to the edge this. circleOffset = maxOffset; if (sliderEndList Ener! = 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


  
      
   
  

MainActivity. java

package net.mobctrl.callendview;import net.mobctrl.callendview.CallSliderEndView.SliderEndListener;import android.app.Activity;import android.os.Bundle;import android.widget.Toast;/** *  * @author Zheng Haibo * @web http://www.mobctrl.net * */public class MainActivity extends Activity {    private CallSliderEndView callSliderEndView;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        callSliderEndView = (CallSliderEndView) findViewById(R.id.csev_slider_view);        callSliderEndView.setSliderEndListener(new SliderEndListener() {            @Override            public void onSliderEnd() {                System.out.println(debug:onSliderEnd...);                Toast.makeText(getApplicationContext(), Slider To End,                        Toast.LENGTH_SHORT).show();            }        });    }}

3. configuration of attributes attr (/res/value/attr. xml)


  
      
                                       
   
  

4. Results

You can use your fingers to slide

Github address: GitHub

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.