Android程式碼實現自定義Button

yangxi_001發表於2013-11-21

 繼承View,在裡面自己去實現onDraw(), onMeasure(), onClickListener()等方法。這種方式比較靈活,可以實現複雜的需求。

程式碼樣例如下:

public class CustomButton extends View{

private final static int WIDTH_PADDING = 8;

private final static int HEIGHT_PADDING = 10;

private final String label;

private final int imageResId;

private final Bitmap image;

//private final InternalListener listenerAdapter = new InternalListener();

public CustomButton(Context context, int resImage, String label)

       {

           super(context);

          this.label = label;

          this.imageResId = resImage;

           this.image = BitmapFactory.decodeResource(context.getResources(),

                  imageResId);

 

           setFocusable(true);

           setBackgroundColor(Color.WHITE);

           //setOnClickListener(listenerAdapter);

           setClickable(true);

       }

@Override

protected void onFocusChanged (boolean gainFocus, int direction, Rect previouslyFocusedRect){

this.setBackgroundColor(Color.GREEN);

}

@Override

protected void onDraw(Canvas canvas)

    {

         Paint textPaint = new Paint();

         textPaint.setColor(Color.BLACK);

         canvas.drawBitmap(image, WIDTH_PADDING / 2, HEIGHT_PADDING / 2, null);

          canvas.drawText(label, WIDTH_PADDING / 2, (HEIGHT_PADDING / 2) +

                  image.getHeight() + 8, textPaint);

     }

@Override

       protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)

        {

         setMeasuredDimension(measureWidth(widthMeasureSpec),

                 measureHeight(heightMeasureSpec));

         }

private int measureWidth(int measureSpec)

     {

        int preferred = image.getWidth() * 2;

         return getMeasurement(measureSpec, preferred);

     }

    

     private int measureHeight(int measureSpec)

     {

          int preferred = image.getHeight() * 2;

          return getMeasurement(measureSpec, preferred);

     }

     private int getMeasurement(int measureSpec, int preferred)

     {

              int specSize = MeasureSpec.getSize(measureSpec);

              int measurement = 0;

           

              switch(MeasureSpec.getMode(measureSpec))

             {

                  case MeasureSpec.EXACTLY:

                      // This means the width of this view has been given.

                      measurement = specSize;

                      break;

                 case MeasureSpec.AT_MOST:

                     // Take the minimum of the preferred size and what

                      // we were told to be.

                     measurement = Math.min(preferred, specSize);

                     break;

                default:

                     measurement = preferred;

                    break;

             }

      

             return measurement;

         }

    /* public void setOnClickListener(ClickListener newListener)

          {

              listenerAdapter.setListener(newListener);

          }*/

     public String getLabel()

          {

              return label;

          }

        

          /**

           * Returns the resource id of the image.

           */

          public int getImageResId()

          {

              return imageResId;

          }

         /* private class InternalListener implements View.OnClickListener

               {

                   private ClickListener listener = null;

             

                  

                   public void setListener(ClickListener newListener)

                   {

                       listener = newListener;

                   }

                 

                   @Override

                   public void onClick(View v)

                   {

                       if (listener != null)

                       {

                           listener.onClick(CustomImageButton.this);

                       }

                   }

               }*/

  

}

相關文章