其他分享
首页 > 其他分享> > Android中带文字的ImageButton的实现

Android中带文字的ImageButton的实现

作者:互联网

import android.content.Context;
import android.graphics.Canvas;
import   android.graphics.Paint;
import android.graphics.Typeface;
import   android.widget.ImageButton;  
public class Butt extends ImageButton
{
 private String text = null;  //要显示的文字
 private int color;//文字的颜色
 
 public Butt(Context context)
 {
  super(context);
 }
 
 public void setText(String text)
 {
  this.text = text;//设置文字
 this.invalidate();  //通知刷新
 }
 
 public void setColor(int color)
 {
  this.color = color;//设置文字颜色
 }
 
 protected void onDraw(Canvas canvas)
 {
  super.onDraw(canvas);
  
  if(this.text != null)
  {
   Paint paint=new Paint();
   paint.setTextAlign(Paint.Align.CENTER);
   paint.setAntiAlias(true);
   paint.setTypeface(Typeface.create("宋体",      Typeface.BOLD_ITALIC));
   paint.setColor(color);
   paint.setTextSize(this.getHeight()/6);
   canvas.drawText(this.text,this.getWidth()/2,this.getHeight()-(float)(paint.getTextSize()-2)/2,paint);//绘制文字
  }
 }
}

 

标签:中带,Paint,ImageButton,text,paint,import,Android,android,color
来源: https://blog.51cto.com/u_15298588/3034415