DEMO:呼吸灯按钮、smoothbutton、使用一组图片设置按钮背景、setLevel
1.attrs属性: 1 2 3 4 5 6 7 8 9 10 11 12 <?xmlversion= "1.0" encoding= "utf-8" ?> <resources> <stringname= "app_name" >EX_templete</string> <stringname= "action_settings" >Settings</string> <stringname= "hello_world" >Helloworld!</string> <declare-styleablename= "SmoothButton" > <attrname= "transitionDrawable" format= "reference" /> <attrname= "transitionDrawableLength" format= "integer" /> <attrname= "transitionTextColorUp" format= "color" /> <attrname= "transitionTextColorDown" format= "color" /> </declare-styleable> </resources> 2.layout设置: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 <RelativeLayoutxmlns:android= "http://schemas.android.com/apk/res/android" xmlns:misc= "http://schemas.android.com/apk/res/com.example.ex_templete" xmlns:tools= "http://schemas.android.com/tools" android:layout_width= "match_parent" android:layout_height= "match_parent" android:paddingBottom= "@dimen/activity_vertical_margin" android:paddingLeft= "@dimen/activity_horizontal_margin" android:paddingRight= "@dimen/activity_horizontal_margin" android:paddingTop= "@dimen/activity_vertical_margin" tools:context= ".MainActivity" > <TextView android:id= "@+id/textView1" android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:text= "@string/hello_world" /> <com.example.ex_templete.SmoothButton android:layout_width= "fill_parent" android:layout_height= "wrap_content" android:background= "@drawable/red_button" android:text= "smoothbutton1" misc:transitionDrawable= "@drawable/red_transition_list" misc:transitionDrawableLength= "8" misc:transitionTextColorDown= "#aaa" misc:transitionTextColorUp= "#eee" /> </RelativeLayout> 3.自定义控件SmoothButton类: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 import android.content.Context; import android.content.res.TypedArray; import android.graphics.Color; import android.graphics.drawable.Drawable; import android.graphics.drawable.LevelListDrawable; import android.os.Handler; import android.os.Message; import android.util.AttributeSet; import android.view.KeyEvent; import android.view.MotionEvent; import android.widget.Button; public class SmoothButton extends Button { private static final long DELAY= 25 ; private int transitionDrawableLength; private int level; private int []colors; private boolean wasPressed; private Drawablebackground; public SmoothButton(Contextcontext,AttributeSetattrs) { super (context,attrs); TypedArraya=context.obtainStyledAttributes(attrs,R.styleable.SmoothButton); LevelListDrawabletransitionDrawable=(LevelListDrawable)a.getDrawable(R.styleable.SmoothButton_transitionDrawable); transitionDrawableLength=a.getInt(R.styleable.SmoothButton_transitionDrawableLength, 0 ); int useTextColors= 0 ; int c0= 0 ; if (a.hasValue(R.styleable.SmoothButton_transitionTextColorUp)) { c0=a.getColor(R.styleable.SmoothButton_transitionTextColorUp, 0 ); useTextColors++; } int c1= 0 ; if (useTextColors== 1 &&a.hasValue(R.styleable.SmoothButton_transitionTextColorDown)) { c1=a.getColor(R.styleable.SmoothButton_transitionTextColorDown, 0 ); useTextColors++; } a.recycle(); if (transitionDrawable== null ) { throw new RuntimeException( "transitionDrawablemustbedefinedinXML(withvalidxmlns)" ); } if (transitionDrawableLength== 0 ) { throw new RuntimeException( "transitionDrawableLengthmustbedefinedinXML(withvalidxmlns)" ); } if (useTextColors== 2 ) { setTextColor(c0); int a0=Color.alpha(c0); int r0=Color.red(c0); int g0=Color.green(c0); int b0=Color.blue(c0); int a1=Color.alpha(c1); int r1=Color.red(c1); int g1=Color.green(c1); int b1=Color.blue(c1); colors= new int [transitionDrawableLength]; for ( int i= 0 ;i<transitionDrawableLength;i++) { int ai=a0+i*(a1-a0)/transitionDrawableLength; int ri=r0+i*(r1-r0)/transitionDrawableLength; int gi=g0+i*(g1-g0)/transitionDrawableLength; int bi=b0+i*(b1-b0)/transitionDrawableLength; colors[i]=Color.argb(ai,ri,gi,bi); } } int []viewAttrs= {android.R.attr.background}; a=context.obtainStyledAttributes(attrs,viewAttrs); boolean hasa.hasValue( 0 ); a.recycle(); if (!hasBackground) { int l=getPaddingLeft(); int t=getPaddingTop(); int r=getPaddingRight(); int b=getPaddingBottom(); setBackgroundDrawable(transitionDrawable); setPadding(l,t,r,b); } background=getBackground(); level= 0 ; background.setLevel(level); //通过此方法设置一组图片为背景************************************** } @Override public boolean onKeyDown( int keyCode,KeyEventevent) { boolean rc= super .onKeyDown(keyCode,event); if ((keyCode==KeyEvent.KEYCODE_ENTER||keyCode==KeyEvent.KEYCODE_DPAD_CENTER)&&event.getRepeatCount()== 0 &&level+ 1 <transitionDrawableLength) { handler.removeMessages(- 1 ); handler.sendEmptyMessageDelayed( 1 ,DELAY); } return rc; } @Override public boolean onKeyUp( int keyCode,KeyEventevent) { boolean rc= super .onKeyUp(keyCode,event); if ((keyCode==KeyEvent.KEYCODE_ENTER||keyCode==KeyEvent.KEYCODE_DPAD_CENTER)&&event.getRepeatCount()== 0 &&level> 0 ) { handler.removeMessages( 1 ); handler.sendEmptyMessageDelayed(- 1 ,DELAY); } return rc; } @Override public boolean onTouchEvent(MotionEventevent) { wasPressed=isPressed(); boolean rc= super .onTouchEvent(event); post(checkPressed); return rc; } RunnablecheckPressed= new Runnable() { public void run() { if (!wasPressed&&isPressed()) { if (level+ 1 <transitionDrawableLength) { handler.removeMessages(- 1 ); handler.sendEmptyMessageDelayed( 1 ,DELAY); } } else if (wasPressed&&!isPressed()) { if (level> 0 ) { handler.removeMessages( 1 ); handler.sendEmptyMessageDelayed(- 1 ,DELAY); } } } }; private Handlerhandler= new Handler() { @Override public void handleMessage(Messagemsg) { int what=msg.what; level+=what; if (level>= 0 &&level<transitionDrawableLength) { background.setLevel(level); if (colors!= null ) { setTextColor(colors[level]); } sendEmptyMessageDelayed(what,DELAY); } else { level=Math.max( 0 ,level); level=Math.min(transitionDrawableLength- 1 ,level); } } }; } 4.MainActivity 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 import android.os.Bundle; import android.app.Activity; import android.view.Menu; public class MainActivity extends Activity { @Override protected void onCreate(BundlesavedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.activity_main); } @Override public boolean onCreateOptionsMenu(Menumenu) { getMenuInflater().inflate(R.menu.main,menu); return true ; } } 本文转自 glblong 51CTO博客,原文链接:http://blog.51cto.com/glblong/1228577,如需转载请自行联系原作者