An idea for Android custom controls-Getting Started

Source: Internet
Author: User

Transferred from: http://blog.sina.com.cn/s/blog_691051e10101a3by.html

  Many times there are no controls that we need to use, or the controls are not beautiful. For example, this slide switch, this is the version of Android after the control provided, the new version does not provide, this time we need to customize the control.   A 2 main class, Onchangedlistener,slipbutton slipbutton code as follows package com.appipv6.android.slipbutton;  Import Com.appipv.onoff.r;import Android.content.context;import Android.graphics.bitmap;import Android.graphics.bitmapfactory;import Android.graphics.canvas;import Android.graphics.matrix;import Android.graphics.paint;import Android.graphics.rect;import Android.util.attributeset;import Android.view.motionevent;import Android.view.view;import Android.view.view.ontouchlistener; public Class Slipbutton extends View implements ontouchlistener{//current button state private Boolean nowchoose=false;  //user is sliding private The Boolean onslip=false;//is pressed when the X, xprivate float downx,nowx;//is turned on and off, and the cursor rectprivate Rect btn_on,btn_off;  Private Boolean Ischglsnon =false;  private Onchangedlistener Chglsn;       private Bitmap bg_on,bg_off,slip_btn;  public Slipbutton (Context contEXT) {super (context);//TODO auto-generated constructor Stubinit ();}  public Slipbutton (Context context, AttributeSet Attrs) {Super (context, attrs);//TODO Auto-generated constructor Stubinit ();}  public Slipbutton (Context context, AttributeSet attrs, int defstyle) {Super (context, attrs, defstyle);//TODO Auto-generated constructor Stubinit ();}  private void init () {//TODO auto-generated method Stubbg_on=bitmapfactory.decoderesource (Getresources (), R.drawable.on); Bg_off=bitmapfactory.decoderesource (Getresources (), r.drawable.off); slip_btn= Bitmapfactory.decoderesource (Getresources (), r.drawable.my_btn); int tmp=bg_off.getwidth ()/2;btn_On=new Rect (tmp, 0 , Slip_btn.getwidth () +tmp, Slip_btn.getheight ()), Btn_off=new Rect (Bg_off.getwidth ()-tmp-slip_btn.getwidth (), 0, Bg_ Off.getwidth ()-tmp, Slip_btn.getheight ()); Setontouchlistener (this);} @Overrideprotected void OnDraw (canvas canvas) {//TODO auto-generated method Stubsuper.ondraw (canvas); Matrix matrix=new Matrix (); Paint Paint=new Paint(); float x;if (Onslip) {if (Nowx>=bg_on.getwidth ()) {x=bg_on.getwidth ()-slip_btn.getwidth ()/2;} Else{x=nowx-slip_btn.getwidth ()/2;}} Else{if (nowchoose) {x=btn_on.left;} Else{x=btn_off.left;}} if (nowx< (Bg_on.getwidth ()/2)) {Canvas.drawbitmap (Bg_off, Matrix, paint);} Else{canvas.drawbitmap (bg_on, Matrix, paint);} if (x<0) {x=0;} else if (X>bg_on.getwidth ()-slip_btn.getwidth ()) {x=bg_on.getwidth ()-slip_btn.getwidth ();} Canvas.drawbitmap (slip_btn, X, 0,paint);}  public boolean OnTouch (View V, motionevent event) {//TODO auto-generated method Stubswitch (Event.getaction ()) { Case Motionevent.action_move:nowx=event.getx (); Break;case MotionEvent.ACTION_DOWN:if (Event.getx () >bg_ On.getwidth () | | Event.gety () >bg_on.getheight ()) {return false;} Onslip=true;downx=event.getx (); Nowx=downx;break;case Motionevent.action_up:onslip=false;boolean lastChoose= Nowchoose;if (Event.getx () >= (Bg_on.getwidth ()/2)) {nowchoose=true;} Else{nowchoose=false;} if (ischglsnon&& (Lastchoose!=nowchoose)) {Chglsn.onchanGed (nowchoose);} Break;default:break;} Invalidate (); return true;} public void Setonchangelistener (Onchangedlistener l) {ischglsnon=true; Chglsn=l;}  public Boolean isnowchoose () {return nowchoose;}  public void Setnowchoose (Boolean nowchoose) {this.nowchoose = Nowchoose;nowx=btn_on.left;invalidate ();} The  }  onchangedlistener code is as follows public interface onchangedlistener{abstract void OnChanged (Boolean CheckState);}    main activity code is as follows  package Com.appipv.onoff; import Com.appipv6.android.slipbutton.onchangedlistener;import Com.appipv6.android.slipbutton.slipbutton; import Android.app.activity;import Android.os.bundle;import Android.widget.toast; public class OnOffActivity extends Activity{private Slipbutton slipButton1; @Overridepublic void OnCreate (Bundle savedinstancestate) {super.oncreate ( Savedinstancestate); Setcontentview (R.layout.main); Slipbutton Slipbutton = (Slipbutton) Findviewbyid (R.id.slipbutton1); Slipbutton.setnowchoose (true); SlipButton1 = ( Slipbutton) Findviewbyid (r.id.slipbutton2);  slipbutton.setonchangelistener (new Onchangedlistener () { public void OnChanged (Boolean checkstate) {//TODO auto-generated method stubstring s;if (Slipbutton1.isnowchoose ()) {s= " Button2 opened the ... ";} else{s= "Button2 closed ...";} if (checkstate) Toast.maketext (onoffactivity.this, "Button1 open ..." +s,toast.length_short). Show (); Elsetoast.maketext (Onoffactivity.this, "Button1 closed ..." +s,toast.length_short). Show ();}});}} The layout file code is as follows <?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/ Apk/res/android "    android:layout_width=" Fill_parent "    android: layout_height= "fill_parent"     android:orientation= "vertical"       android:background= "@drawable/back"     >     < Com.appipv6.android.slipbutton.slipbutton        android:id= "@+id/ SlipButton1 "         android:layout_width= "Wrap_content"          android:layout_height= "49DP"/>  <com.appipv6.android.slipbutton.slipbutton      android:id= "@+id/slipbutton2"      android:layout_width= "Wrap_ Content "     android:layout_height=" Fill_parent "/> </linearlayout>   

An idea for Android custom controls-Getting started

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.