安卓自定义按钮的实现
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 来讨论。
代码如下:
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 来讨论。
相关文章推荐
- 安卓自定义带图片按钮的实现
- 安卓自定义View----且看如何巧妙地实现一个类似于电视遥控板的环形按钮效果(上)
- 安卓非自定义标题栏实现“返回”按钮
- 安卓自定义shape并实现带有阴影的按钮背景效果
- 安卓自定义View实现钟表
- 安卓UI学习(2)--能自定义点击事件,且带点击动画的按钮
- 自定义listview,实现Item侧滑显示删除、置顶按钮
- 【IOS】自定义UIAlertView样式,实现可替换背景和按钮
- android之自定义ViewGroup和自动换行的布局的实现(支持按钮间隔)
- Windows Phone 7 自定义按钮的实现
- MFC自定义按钮的实现
- 安卓选项卡的实现方法(TabActivity),自定义TabHost容器
- 安卓中自定义view控件代替radiogroup实现颜色渐变效果的写法
- Android中ImageButton自定义按钮的按下效果的代码实现方法,附网上2种经典解决方法。 .
- UIPageControl实现自定义按钮
- 【IOS】自定义UIAlertView样式,实现可替换背景和按钮
- 安卓自定义View实现图片上传进度显示(仿QQ)
- FLEX4中的Panel如何实现带自定义图标和按钮
- 自定义标签实现按钮权限
- Android自定义按钮实现长按功能