Android Custom Button Example (overriding ImageButton Control Implementation Picture button) _android

Source: Internet
Author: User

The ImageButton class is rewritten because the project has more of this type of picture buttons.

Copy Code code as follows:

Package me.henji.widget;

Import Android.content.Context;
Import Android.graphics.ColorMatrix;
Import Android.graphics.ColorMatrixColorFilter;
Import Android.util.AttributeSet;
Import android.view.MotionEvent;
Import Android.view.View;
Import Android.view.View.OnFocusChangeListener;
Import Android.view.View.OnTouchListener;
Import Android.widget.ImageButton;

/**
* Custom Picture button (ImageButton), press the color change
* @author Leo
* @created 2013-3-15
*/
public class Cmbutton extends ImageButton implements Ontouchlistener, Onfocuschangelistener {

Public Cmbutton {
Super (context);
This.setontouchlistener (this);
This.setonfocuschangelistener (this);
}

Public Cmbutton (context context, AttributeSet Attrs) {
This is the context, Attrs, Android. R.attr.imagebuttonstyle);
This.setontouchlistener (this);
This.setonfocuschangelistener (this);
}

Public Cmbutton (context context, AttributeSet attrs, int defstyle) {
Super (context, attrs, Defstyle);
Setfocusable (TRUE);
This.setontouchlistener (this);
This.setonfocuschangelistener (this);
}

  @Override
 public void Onfocuschange (View V, boolean hasfocus) {
  //Gray effect
   colormatrix cm = new ColorMatrix ();
  cm.setsaturation (0);
  if (hasfocus) {
    ((ImageButton) v). Getdrawable (). Setcolorfilter (New Colormatrixcolorfilter (cm));
  } else {
    ((ImageButton) v). Getdrawable (). Clearcolorfilter ();
 &NBSP}
 }

@Override
public boolean Ontouch (View V, motionevent event) {
Grey effect
ColorMatrix cm = new ColorMatrix ();
Cm.setsaturation (0);
if (event.getaction () = = Motionevent.action_down) {
((ImageButton) v). Getdrawable (). Setcolorfilter (new Colormatrixcolorfilter (cm));
else if (event.getaction () = = motionevent.action_up) {
((ImageButton) v). Getdrawable (). Clearcolorfilter ();
}
return false;
}
}

Layout file

Copy Code code as follows:

<me.henji.widget.cmbutton
Android:id= "@+id/btn_login"
Android:layout_width= "Match_parent"
android:layout_height= "Wrap_content"
Android:background= "#00000000"
android:src= "@drawable/button_login"
android:text= "@string/login_login"/>

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.