您的位置:首页 > 其它

安卓自定义按钮的实现

2012-11-11 22:19 316 查看
如图,在按钮上实现带有个性图片的按钮,可通过复写按钮类来实现。



代码如下:

package moyan.standopen;

import android.annotation.SuppressLint;

import android.content.Context;

import android.content.res.Resources;

import android.graphics.Bitmap;

import android.graphics.BitmapFactory;

import android.graphics.Canvas;

import android.graphics.Color;

import android.graphics.Paint;

import android.graphics.Rect;

import android.graphics.RectF;

import android.util.AttributeSet;

import android.view.MotionEvent;

import android.widget.Button;

@SuppressLint("ParserError")

public class mybutton extends Button{

private Paint margainPaint;

private Paint linePaint;

private int paperColor;

private float margain;

private Bitmap bmp;

public mybutton(Context context) {

super(context);

// TODO Auto-generated constructor stub

}

public mybutton(Context context, AttributeSet attrs, int defStyle) {

super(context, attrs, defStyle);

// TODO Auto-generated constructor stub

init();

}

public mybutton(Context context, AttributeSet attrs) {

super(context, attrs);

// TODO Auto-generated constructor stub

init();

}

@Override

public boolean onTouchEvent(MotionEvent event) {

// TODO Auto-generated method stub

//处理按钮按下后的效果

if(event.getAction()==MotionEvent.ACTION_DOWN)

{

linePaint=new Paint(Paint.ANTI_ALIAS_FLAG);

linePaint.setColor(Color.GRAY);

}

if(event.getAction()==MotionEvent.ACTION_UP)

{

linePaint=new Paint(Paint.ANTI_ALIAS_FLAG);

linePaint.setColor(Color.WHITE);

}

invalidate();

return super.onTouchEvent(event);

}

public void init()

{

Resources mtRes=getResources();

bmp=BitmapFactory.decodeResource(mtRes, R.drawable.head_01);

margainPaint=new Paint(Paint.ANTI_ALIAS_FLAG);

margainPaint.setColor(Color.BLACK);

linePaint=new Paint(Paint.ANTI_ALIAS_FLAG);

linePaint.setColor(Color.WHITE);

paperColor=Color.WHITE;

}

@Override

protected void onDraw(Canvas canvas) {

// TODO Auto-generated method stub

//canvas.drawColor(paperColor);

//canvas.drawRect(10, 0, getMeasuredHeight(), getMeasuredHeight(), linePaint);

linePaint.setAntiAlias(true);

RectF oval3 = new RectF(0, 0,getMeasuredWidth(), getMeasuredHeight());

canvas.drawRoundRect(oval3, 5, 5, linePaint);//画按钮的形状为圆角矩形

canvas.drawText(">",getMeasuredWidth()-30,getMeasuredHeight()/2,margainPaint);

Rect rect=new Rect(2,2,getMeasuredHeight()-2,getMeasuredHeight()-2);

canvas.drawBitmap(bmp,null, rect, linePaint);//画图片

canvas.save();

canvas.translate(30, 0);

super.onDraw(canvas);

canvas.restore();

}

@Override

public boolean isPressed() {

// TODO Auto-generated method stub

return super.isPressed();

}

public void SetBmp(int bitmap)

{

//在程序中调用,用来设置每个按钮的图片

Resources mtRes=getResources();

bmp=BitmapFactory.decodeResource(mtRes,bitmap);

invalidate();

}

}

在布局中的调用:

<moyan.standopen.mybutton

android:id="@+id/first"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="@string/love"

android:layout_marginLeft="50dip"

android:layout_marginRight="50dip"

android:textSize="12pt"

android:gravity="center"

android:paddingRight="30dip"

android:layout_above="@id/center"

android:layout_marginBottom="20dip"

/>

最后在程序中定义mybutton的对象,通过调用SetBmp()函数来设置图片。

如有不懂或有疑问可以加群 140239644 来讨论。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: