0

I was wandering if anyone could help me with a little image scaling problem that I have while creating custom view (customized button)? So, I created a custom view class with custom constructor and few methods:

public class GamesButton extends View {
public int imageID;
public Bitmap image;
public GamesButton(Context context, AttributeSet attrs) {
    super(context, attrs);
}
public GamesButton(Context context, int resImage) {
    super(context);
    this.imageID = resImage;
    this.image = BitmapFactory.decodeResource(context.getResources(),imageID);
    setFocusable(true);
    setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            // do something
        }
    });
    setClickable(true);
}
public void onDraw(Canvas canvas) {
    Paint textPaint = new Paint();
    textPaint.setColor(Color.BLACK);
    canvas.drawBitmap(image,0 , 0,null);

}
   .
   .
   .

I've also overridden onMeaure() and others, but my custom view size is not the issue here.

The problem is the Bitmap image that I use as a button or as a surface that you click on. I can't scale it so that you can see the whole image on screen.

Is there a way to deal with this?

Thank you!

4

1 回答 1

0

您可以通过将按钮的背景设置为 9patch 图像文件来创建自定义按钮。您还可以使用选择器,并为按钮的不同状态提供不同的 9patch 图像文件。这比覆盖 onDraw() 要简单得多。

于 2012-06-19T16:39:32.770 回答