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!

Charles
  • 50,943
  • 13
  • 104
  • 142
ZeZe
  • 3
  • 1

1 Answers1

0

You can create a custom button by setting the background of the button to a 9patch image file. You can also use a selector, and have different 9patch image files for different states of the button. That's a lot simpler than overriding onDraw().

Christine
  • 5,617
  • 4
  • 38
  • 61
  • Thank you for you answer, I will also try that, but my problem is not only related to buttons, but more to image scaling and custom views in general. – ZeZe Jun 19 '12 at 16:47