In actual development, the Android self-contained controls sometimes cannot meet our needs. In this case, we need to rewrite the controls to implement the functions we want. For example, if I want to enable the Button to write text after being pressed or played, no native control can meet our needs. Here I choose to reload ImageButton and add text based on ImageButton.
Effect
Code for rewriting buttons
Package com. example. buttontest; import android. content. context; import android. content. res. typedArray; import android. graphics. canvas; import android. graphics. paint; import android. graphics. paint. align; import android. util. attributeSet; import android. util. log; import android. widget. imageButton;/*** custom ImageButton can set text on ImageButton */public class CustomImageButton extends ImageButton {private static final String TAG = "mimimagebutton_dzt "; private String mtext = "a"; private int mcolor = 0; private float mtextsize = 0f; private Paint mpatin; public CustomImageButton (Context context, AttributeSet attrs) {super (context, attrs); initAttrs (attrs);} private void initAttrs (AttributeSet attrs) {TypedArray array = getContext (). obtainStyledAttributes (attrs, R. styleable. customButtonAttrs); mtext = array. getString (R. styleable. customButtonAttrs_textValue); mcolor = array. getColor (R. styleable. customButtonAttrs_textColor, 230); mtextsize = array. getDimension (R. styleable. customButtonAttrs_textSize, 25366f); array. recycle (); // reclaim the resource mpatin = new Paint (); mpatin. setTextAlign (Align. CENTER); Log. d (TAG, "mtextsize =" + mtextsize);} public void setText (String text) {this. mtext = text;} public void setColor (int color) {this. mcolor = color;} public void setTextSize (float textsize) {this. mtextsize = textsize;} @ Overrideprotected void onDraw (Canvas canvas) {super. onDraw (canvas); mpatin. setColor (mcolor); mpatin. setTextSize (mtextsize); canvas. drawText (mtext, canvas. getWidth ()/2, (canvas. getHeight ()/2) + 6, mpatin );}}
Notes for Custom Controls
1. There are three constructors in the view class.
View (Context context)
Simple constructor to use when creating a view from code.
View (Context context, AttributeSet attrs)
Constructor that is called when inflating a view from XML.
View (Context context, AttributeSet attrs, int defStyleAttr)
Perform inflation from XML and apply a class-specific base style.
If you want to define custom controls in xml, you must add constructors with parameters.
Public CustomImageButton (Context context, AttributeSet attrs)
The constructor of the parent class must be called.
2. If you want to customize control attributes in xml
Attrs parameters need to be parsed
Private void initAttrs (AttributeSet attrs) {TypedArray array = getContext (). obtainStyledAttributes (attrs, R. styleable. customButtonAttrs); mtext = array. getString (R. styleable. customButtonAttrs_textValue); mcolor = array. getColor (R. styleable. customButtonAttrs_textColor, 230); mtextsize = array. getDimension (R. styleable. customButtonAttrs_textSize, 25366f); array. recycle (); // reclaim the resource mpatin = new Paint (); mpatin. setTextAlign (Align. CENTER); Log. d (TAG, "mtextsize =" + mtextsize );}
R. styleable. CustomButtonAttrs is a custom attribute.
Attrs. xml definition
Use of Custom Controls
Explain the meaning of the following sentence
xmlns:custom_btn="http://schemas.android.com/apk/res/com.example.buttontest"
Custom_btn is the prefix of the custom attribute, Which is used later when you reference the custom attribute.
Com. example. buttontest is the package name of the program to be created. No matter which package your custom control is placed, the native package name can only be used here.
Custom_btn: textValue = "@ string/rock" is the property of the custom control.
Style definitions used in the preceding xml
When defining the attributes of a custom control in a style, you must add a native package name such as com. example. buttontest: textColor.
Use in Activity
Package com. example. buttontest; import android. app. activity; import android. OS. bundle; import android. view. view; import android. view. view. onClickListener; public class MainActivity extends Activity {private int moldId =-1; mimimagebutton ImageButton1; using ImageButton2; using ImageButton3; using ImageButton4; using ImageButton5; using ImageButto N6; CustomImageButton ImageButton7; CustomImageButton ImageButton8; CustomImageButton ImageButton9; CustomImageButton ImageButton10; CustomImageButton ImageButton11; CustomImageButton ImageButton12; @ Overrideprotected void onCreate (Bundle savedInstanceState. onCreate (savedInstanceState); setContentView (R. layout. activity_main); ImageButton1 = (CustomImageButton) findViewById (R. id. button1); ImageButton 2 = (CustomImageButton) findViewById (R. id. button2); ImageButton3 = (CustomImageButton) findViewById (R. id. button3); ImageButton4 = (CustomImageButton) findViewById (R. id. button4); ImageButton5 = (CustomImageButton) findViewById (R. id. button5); ImageButton6 = (CustomImageButton) findViewById (R. id. button6); ImageButton7 = (CustomImageButton) findViewById (R. id. button7); ImageButton8 = (CustomImageButton) find ViewById (R. id. button8); ImageButton9 = (CustomImageButton) findViewById (R. id. button9); ImageButton10 = (CustomImageButton) findViewById (R. id. button10); ImageButton11 = (CustomImageButton) findViewById (R. id. button11); ImageButton12 = (CustomImageButton) findViewById (R. id. button12); ImageButton1.setOnClickListener (new buttonListener (); ImageButton2.setOnClickListener (new buttonListener (); ImageButton3.se TOnClickListener (new buttonListener (); listener (new buttonListener (); ImageButton5.setOnClickListener (new buttonListener (); listener (new buttonListener ()); imageButton8.setOnClickListener (new buttonListener (); ImageButton9.setOnClickListener (new buttonListener (); ImageButton10.setOnClickListener (new ButtonListener (); ImageButton11.setOnClickListener (new buttonListener (); ImageButton12.setOnClickListener (new buttonListener (); moldId = R. id. button3; selectBtnState (moldId);}/*** selected button status ** @ param newId */private void selectBtnState (int newId) {switch (newId) {case R. id. button1: ImageButton1.setBackgroundResource (R. drawable. eq_btn_sel); break; case R. id. button2: ImageButton2.setBackgroundResource (R. Drawable. eq_btn_sel); break; case R. id. button3: ImageButton3.setBackgroundResource (R. drawable. eq_btn_sel); break; case R. id. button4: ImageButton4.setBackgroundResource (R. drawable. eq_btn_sel); break; case R. id. button5: ImageButton5.setBackgroundResource (R. drawable. eq_btn_sel); break; case R. id. button6: ImageButton6.setBackgroundResource (R. drawable. eq_btn_sel); break; case R. id. button7: ImageButton7.setImageResour Ce (R. drawable. eq_btn_sel); break; case R. id. button8: ImageButton8.setImageResource (R. drawable. eq_btn_sel); break; case R. id. button9: ImageButton9.setImageResource (R. drawable. eq_btn_sel); break; case R. id. button10: ImageButton10.setImageResource (R. drawable. eq_btn_sel); break; case R. id. button11: ImageButton11.setImageResource (R. drawable. eq_btn_sel); break; case R. id. button12: ImageButton12.setImageResource (R. draw Able. eq_btn_sel); break; default: break;}/*** restore the status of the previous button ** @ param */private void regainBtnState (int oldId) {switch (oldId) {case R. id. button1: ImageButton1.setBackgroundResource (R. drawable. eq_btn_selector); break; case R. id. button2: ImageButton2.setBackgroundResource (R. drawable. eq_btn_selector); break; case R. id. button3: ImageButton3.setBackgroundResource (R. drawable. eq_btn_selector); break; case R. id. but Ton4: ImageButton4.setBackgroundResource (R. drawable. eq_btn_selector); break; case R. id. button5: ImageButton5.setBackgroundResource (R. drawable. eq_btn_selector); break; case R. id. button6: ImageButton6.setBackgroundResource (R. drawable. eq_btn_selector); break; case R. id. button7: ImageButton7.setImageResource (R. drawable. eq_btn_selector); break; case R. id. button8: ImageButton8.setImageResource (R. drawable. eq_btn_sele Ctor); break; case R. id. button9: ImageButton9.setImageResource (R. drawable. eq_btn_selector); break; case R. id. button10: ImageButton10.setImageResource (R. drawable. eq_btn_selector); break; case R. id. button11: ImageButton11.setImageResource (R. drawable. eq_btn_selector); break; case R. id. button12: ImageButton12.setImageResource (R. drawable. eq_btn_selector); break; default: break;} class buttonListener implements OnClic KListener {@ Overridepublic void onClick (View v) {// TODO Auto-generated method stubif (moldId! = V. getId () {switch (v. getId () {case R. id. button1: break; case R. id. button2: break; case R. id. button3: break; case R. id. button4: break; case R. id. button5: break; case R. id. button6: break; case R. id. button7: break; case R. id. button8: break; case R. id. button9: break; case R. id. button10: break; case R. id. button11: break;} regainBtnState (moldId); selectBtnState (v. getId (); moldId = v. getId ();}}}}
In actual use, there is a strange problem. There is no problem when you click the code button above on the resistor screen. If you are on the capacitive screen (multi-point touch is supported, the buttons in the second row may be selected at the same time. The difference between the two buttons is that one is set using setBackgroundResource, And the other uses setImageResource to correspond to android in xml: background, android: src: A background image and a foreground image. As for why multiple setImageResource images are selected, no root cause is found.
Another question about the UI experience is the best way to create objects in the onDraw () function. Otherwise, the following warning message is displayed: Because onDraw () is frequently called, ongoing creation and garbage collection will affect the UI display performance
For example:
protected void onDraw(Canvas canvas) {super.onDraw(canvas);Paint mpatin = new Paint();mpatin.setTextAlign(Align.CENTER);mpatin.setColor(mcolor);mpatin.setTextSize(mtextsize);canvas.drawText(mtext, canvas.getWidth() / 2,(canvas.getHeight() / 2) + 6, mpatin);}
Avoid object allocations during draw/layout operations (preallocate and reuse instead)
Sample Code
Http://download.csdn.net/detail/deng0zhaotai/7005597