Android - 将位图拟合到屏幕

时间:2012-04-21 09:16:23

标签: android bitmap height width landscape

我有这个项目,其中我的位图大于屏幕大小。我想调整它以适应屏幕。我没有标题栏,而且我处于全屏模式。这是我的非工作代码:

public class ScopView extends View
{
    private Scop thescop;

    public ScopView(Context context, Scop newscop)
    {
        super(context);
        this.thescop = newscop;
    }

    @Override
    public void onDraw(Canvas canvas)
    {
        Bitmap scopeBitmap;
        BitmapFactory.Options bfOptions = new BitmapFactory.Options();
        bfOptions.inDither = false;
        bfOptions.inPurgeable = true;
        bfOptions.inInputShareable = true;
        bfOptions.inTempStorage = new byte[32 * 1024];

        scopeBitmap = BitmapFactory.decodeResource(getResources(),R.drawable.scope, bfOptions);
        scopeBitmap.createScaledBitmap(scopeBitmap, SniperActivity.Width, SniperActivity.Height, false);
        canvas.drawBitmap(scopeBitmap, SniperActivity.scopx, SniperActivity.scopy, null);
    }
}

在这里使用createScaledBitmap方法时,我将自己用作源,并使用一个活动中的一些变量来从屏幕首选项中检索窗口高度和宽度。

3 个答案:

答案 0 :(得分:11)

您可以使用以下代码调整位图大小。

int h = 320; // Height in pixels
int w = 480; // Width in pixels    
Bitmap scaled = Bitmap.createScaledBitmap(largeBitmap, h, w, true);

答案 1 :(得分:1)

此外,您可以使用以下代码段。

public Bitmap getResizedBitmap(Bitmap bm, int newHeight, int newWidth) {
    int width = bm.getWidth();
    int height = bm.getHeight();
    float scaleWidth = ((float) newWidth) / width;
    float scaleHeight = ((float) newHeight) / height;

    // Create a matrix for the manipulation
    Matrix matrix = new Matrix();

    // Resize the bit map
    matrix.postScale(scaleWidth, scaleHeight);

    // Recreate the new Bitmap
    Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, false);

    return resizedBitmap;
}

答案 2 :(得分:1)

此代码有助于您尝试此

int REQ_WIDTH = 0;
int REQ_HEIGHT = 0;
REQ_WIDTH =imageView.getWidth();
vREQ_HEIGHT =imageView.getHeight();
mImageView.setImageBitmap(Bitmap.createScaledBitmap(BitmapFactory.decodeFile(imageURI, options), REQ_HEIGHT, REQ_WIDTH, true));