android 自定义button,Android代码实现自定义Button

本文介绍如何通过继承View并自定义onDraw()、onMeasure()和onClickListener方法,创建一个可满足复杂需求的自定义按钮。实例展示了如何设置焦点变化效果、绘制文字和图片,并处理点击事件。
摘要由CSDN通过智能技术生成

继承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);

}

}

}*/

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值