使用drawBitmap缩放位图

时间:2013-12-27 06:42:17

标签: android bitmap scale

基本上我有640 x 480图像,我想使用 drawBitmap 进行缩放和绘制。为简单起见,我只想说我有854 x 480安卓屏幕。这是我的代码

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
    );
  }
}

结果与我想要的不同。有什么建议吗?

1 个答案:

答案 0 :(得分:6)

使用此

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

canvas.drawBitmap(bitmap, x, y, paint);