Android Custom button and android custom control

Source: Internet
Author: User
Tags getcolor

Android Custom button and android custom control

Recently, I wrote a custom iPhone-like switch. You can download it if you need it. You can click auto-scroll to modify the speed as needed. Touch scrolling, custom size, and style modification supported. If you do not want to record the animation, you will upload two images for you to see.

Main Code:

Package com. example. switchbutton; import android. annotation. suppressLint; import android. content. context; import android. content. res. resources; import android. content. res. typedArray; import android. graphics. canvas; import android. graphics. paint; import android. graphics. rectF; import android. OS. handler; import android. util. attributeSet; import android. view. motionEvent; import android. view. view; import android. v Iew. viewConfiguration; public class SwitchButton extends View {public static final int DEFAULT_WIDTH = 100; public static final int DEFAULT_HEIGTH = 45; public static final int PER_POST_TIME = 20; public static final int CLICK = 0; public static final int LEFT = 1; public static final int RIGHT = 2; private int mSelectBg; private int mUnSelectBg; private int mBorderColor; private int mSelectCirlceColor; priv Ate int mUnSelectCircleColor; private boolean isChecked; private Paint mPaint; private int mWidth; private int mHeight; private int mClickTimeout; private int mTouchSlop; private float mAnimMove; private final float VELOCITY = 350; private float firstDownX; private float firstDownY; private float lastDownX; private int mCriclePostion; private int mStartCriclePos; private int mEndCirclePos; private boolean isSc Roll; private int status; private Handler mHander; private AnimRunnable mAnim; private OnCheckChangeListener mOnCheckChangeListener; public interface listener {void OnCheck (SwitchButton switchButton, boolean isChecked);} public SwitchButton (Context context Context, attributeSet attrs) {super (context, attrs); Resources res = getResources (); int defaselecselectbg = res. getColor (R. color. default_switch_butt On_select_bg); int defaultUnSelectBg = res. getColor (R. color. default_switch_button_unselect_bg); int defaultBorderColor = res. getColor (R. color. default_switch_button_border_color); int defaultSelectCircleColor = res. getColor (R. color. default_switch_button_select_color); int defaultUnSelectCircleColor = res. getColor (R. color. default_switch_button_unselect_color); TypedArray a = context. obtainStyledAttribute S (attrs, R. styleable. switchButton); mSelectBg =. getColor (R. styleable. switchButton_select_bg, defaultSelectBg); mUnSelectBg =. getColor (R. styleable. switchButton_unselect_bg, defaultUnSelectBg); mBorderColor =. getColor (R. styleable. switchButton_select_border_color, defaultBorderColor); mSelectCirlceColor =. getColor (R. styleable. switchButton_select_cricle_color, defaultSelectCircleColor); mUnSelectCir CleColor =. getColor (R. styleable. switchButton_unselect_cricle_color, defaultUnSelectCircleColor); isChecked =. getBoolean (R. styleable. switchButton_isChecked, false);. recycle (); initView (context);} private void initView (Context context) {mPaint = new Paint (Paint. ANTI_ALIAS_FLAG); final float density = getResources (). getDisplayMetrics (). density; mAnimMove = (int) (VELOCITY * density + 0.5f)/150; // from Dynamic scroll speed: mClickTimeout = ViewConfiguration. getPressedStateDuration () + ViewConfiguration. getTapTimeout (); // click the mTouchSlop = ViewConfiguration. get (context ). getScaledTouchSlop (); // touch the rolling Slop mHander = new Handler (); mAnim = new AnimRunnable () ;}@ SuppressLint ("DrawAllocation") @ Overrideprotected void draonw (Canvas canvas Canvas) {super. onDraw (canvas); mPaint. setColor (mBorderColor); mPaint. setStyle (Paint. style. STRO KE); RectF rect = new RectF (0, 0, mWidth, mHeight); canvas. drawRoundRect (rect, mHeight/2, mHeight/2, mPaint); mPaint. setColor (isChecked? MSelectBg: mUnSelectBg); RectF innerRect = new RectF (1, 1, mWidth-1, mHeight-1); canvas. drawRoundRect (innerRect, mHeight/2-1, mHeight/2-1, mPaint); mPaint. setColor (isChecked? MSelectCirlceColor: mUnSelectCircleColor); mPaint. setStyle (Paint. style. FILL); canvas. drawCircle (mCriclePostion, mHeight/2, mHeight/2-1, mPaint) ;}@ Overridepublic boolean onTouchEvent (MotionEvent event) {float x = event. getX (); float y = event. getY (); switch (event. getAction () {case MotionEvent. ACTION_DOWN: if (isScroll) {return false;} firstDownX = x; firstDownY = y; lastDownX = x; mCriclePostion = isCheck Ed? MEndCirclePos: mStartCriclePos; break; case MotionEvent. ACTION_MOVE: float delaX = x-lastDownX; setCriclePositon (delaX); lastDownX = x; invalidate (); break; case MotionEvent. ACTION_UP: float totalX = x-firstDownX; float totalY = y-firstDownY; float time = event. getEventTime ()-event. getDownTime (); if (totalX <mTouchSlop & totalY <mTouchSlop & time <mClickTimeout) {status = CLICK; startAutoScroll ();} e Lse {delaX = x-lastDownX; setCriclePositon (delaX); status = mCriclePostion <mWidth/2? LEFT: RIGHT; startAutoScroll ();} break;} return true;} // start auto-rolling private void startAutoScroll () {isScroll = true; mHander. postDelayed (mAnim, PER_POST_TIME);} class AnimRunnable implements Runnable {@ Overridepublic void run () {mHander. postDelayed (this, PER_POST_TIME); moveView () ;}// move private void moveView () {if (status = CLICK & isChecked) | status = LEFT) {mCriclePostion-= mAnimMove; if (mCriclePostion <m Height/2) {mCriclePostion = mHeight/2; stopView (false) ;}} else if (status = CLICK &&! IsChecked) | status = RIGHT) {mCriclePostion + = mAnimMove; if (mCriclePostion> mWidth-mHeight/2) {mCriclePostion = mWidth-mHeight/2; stopView (true) ;}} invalidate () ;}// stop moving Viewprivate void stopView (boolean endChecked) {mHander. removeCallbacks (mAnim); isScroll = false; isChecked = endChecked; // callback listener event if (mOnCheckChangeListener! = Null) {mOnCheckChangeListener. onCheck (SwitchButton. this, isChecked) ;}}// set the center position private synchronized void setCriclePositon (float delaX) {int pos = (int) (mCriclePostion + delaX ); if (pos <mHeight/2) {mCriclePostion = mHeight/2;} else if (pos> mWidth-mHeight/2) {mCriclePostion = mWidth-mHeight/2 ;} else {mCriclePostion = pos ;}@ Overrideprotected void onMeasure (int widthMeasureSpec, int heightMeas UreSpec) {int widthMode = MeasureSpec. getMode (widthMeasureSpec); if (widthMode = MeasureSpec. UNSPECIFIED | widthMode = MeasureSpec. AT_MOST) {mWidth = DEFAULT_WIDTH;} else {mWidth = MeasureSpec. getSize (widthMeasureSpec);} int heightMode = MeasureSpec. getMode (heightMeasureSpec); if (heightMode = MeasureSpec. UNSPECIFIED | heightMode = MeasureSpec. AT_MOST) {mHeight = DEFAULT_HEIGTH;} else {mHeight = Measur ESpec. getSize (heightMeasureSpec);} mStartCriclePos = mHeight/2; mEndCirclePos = mWidth-mHeight/2; mCriclePostion = isChecked? MEndCirclePos: mStartCriclePos; setMeasuredDimension (mWidth, mHeight);} public boolean isChecked () {return isChecked;} public void setChecked (boolean isChecked) {this. isChecked = isChecked; invalidate ();} public void setOnCheckChangeListener (OnCheckChangeListener onCheckChangeListener) {this. mOnCheckChangeListener = onCheckChangeListener ;}}

Attrs:

<?xml version="1.0" encoding="utf-8"?> <resources>     <declare-styleable name="SwitchButton">         <attr name="select_bg" format="color"/>         <attr name="unselect_bg" format="color"/>         <attr name="select_border_color" format="color"/>         <attr name="select_cricle_color" format="color"/>          <attr name="unselect_cricle_color" format="color"/>          <attr name="isChecked" format="boolean"></attr>    </declare-styleable> </resources>

: Http://download.csdn.net/detail/huangyanbin123/8173521

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.