3

Basically I have 640 x 480 image and I want to scale and draw it using drawBitmap. For simplicity lets just say I have 854 x 480 android screen. Here is my code

MainActivity.java

public class MainActivity extends Activity
{
  @Override
  public void onCreate(Bundle savedInstanceState)
  {
    super.onCreate(savedInstanceState);
    setContentView(new MainLayout(this));
  }
}
class MainLayout extends RelativeLayout
{
  public MainLayout(Context context)
  {
    super(context);
    setWillNotDraw(false);
  }
  @Override
  public void onDraw(Canvas canvas)
  {
    super.onDraw(canvas);
    Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.background);
    bitmap = Bitmap.createScaledBitmap(bitmap, 854, 480, true);
    canvas.drawBitmap
    (
      bitmap,
      null,
      new Rect(0, 0, 854, 480),
      null
    );
  }
}

The result is different than what I want. Any suggestion?

Sandesh
  • 1,190
  • 3
  • 23
  • 41
user3135962
  • 57
  • 1
  • 8
  • Just a note. If you want **simply a square result**, whether you need to scale up or down, use this incredibly handy tip ... http://stackoverflow.com/a/17733530/294884 – Fattie Jun 02 '14 at 13:42

1 Answers1

7

use this

Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setFilterBitmap(true);
paint.setDither(true);

canvas.drawBitmap(bitmap, x, y, paint);
Shyam
  • 6,376
  • 1
  • 24
  • 38
  • Really good answer, I had bad bitmaps after scaling by matrix without paint.setDither(true), but now everything is perfect. – Yev Kanivets Nov 20 '14 at 09:21