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?

1
  • Just a note. If you want simply a square result, whether you need to scale up or down, use this incredibly handy tip ... stackoverflow.com/a/17733530/294884 Commented Jun 2, 2014 at 13:42

1 Answer 1

7

use this

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

canvas.drawBitmap(bitmap, x, y, paint);
Sign up to request clarification or add additional context in comments.

1 Comment

Really good answer, I had bad bitmaps after scaling by matrix without paint.setDither(true), but now everything is perfect.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.