androidcanvascustom-widgets

Bitmap.createScaledBitmap filtering not working as expected


I'm having problems using Bitmap.createScaledBitmap in a custom widget's canvas in Android. The widget is supposed to display a scaled non anti-aliased version of the given resource.

There's the following code in the widgets class:

protected void onDraw(Canvas canvas) { super.onDraw(canvas); Bitmap bmp = BitmapFactory.decodeResource(this.getResources(), R.drawable.btn); bmp = Bitmap.createScaledBitmap(bmp, bmp.getWidth()*3, bmp.getHeight()*3, false); canvas.drawBitmap(bmp, 0, 0, null); }

The "false" parameter on createScaledBitmap is supposed to turn off anti-aliasing filtering. The result is scaled, but smooth. Changing the value to "true" makes no difference.

Is there another way to achieve the result I want?


Solution

  • After much fiddling, it seems that the problem is getting bitmaps via getResources(). They will always get filtered, no matter which method of drawing is used.

    My workaround was to put all my images in the /assets folder and pull them via getAssets(). (Still not sure how bad of a practice this is...)