如何设置图像的大小?

4

我有一个splash.png图片,希望让它像ImageViewfitXY一样占据整个屏幕。该图片的尺寸为480x767。

我需要在代码中进行哪些更改?

public class BitmapConfigView extends LinearLayout {
private Bitmap mImage;
private Paint mPaint = new Paint();

public BitmapConfigView(Context context) {
    super(context);

    BitmapFactory.Options opts = new BitmapFactory.Options();
    opts.inPreferredConfig = Bitmap.Config.ARGB_8888;
    opts.inScaled = false;
    mImage = BitmapFactory.decodeResource( getResources(), R.drawable.splash, opts);

    mPaint.setDither(true);
    setWillNotDraw(false);
    setOrientation(VERTICAL);
    setGravity(Gravity.BOTTOM);
}

@Override
protected void onDraw(Canvas canvas) {
    canvas.drawBitmap(mImage, 0.0f, 0.0f, mPaint);
}
}

你尝试了哪些方法?你遇到了什么问题? - Pēteris Caune
我正在尝试以不同的方式决定链接。这个问题中的代码给了我优秀的图像,但是大小不正确。 - Sviatoslav
3个回答

4

在这种情况下,bMapScaled = Bitmap.createScaledBitmap(mImage, 320, 480, false); 我得到了图像渐变的扭曲 :( - Sviatoslav
尝试使用 filter = true(第四个参数)。 - Adam Stelmaszczyk
"filter = true" 使得图片上的文本显得非常清晰,但渐变效果则不太好。如果你有时间,我可以发送一个轻量级项目 .zip 文件,其中包含图片和活动。 - Sviatoslav

1
因此,我使用了WebView:
public class TestwebViewimageActivity extends Activity {
private WebView wv;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    wv = (WebView) findViewById(R.id.webview);
    wv.setWebViewClient(new HelloWebViewClient());  
    wv.setBackgroundColor(0x00000000);
    wv.getSettings().setBuiltInZoomControls(false);
    wv.setVerticalScrollBarEnabled(false);
    wv.setHorizontalScrollBarEnabled(false);
    wv.loadUrl("file:///android_asset/6.html");

}

private class HelloWebViewClient extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        view.loadUrl(url);
        return true;
    }
}
}

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">

   <WebView
   android:id="@+id/webview"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"/>
</LinearLayout>

6.html

<html>
<head>
<style type="text/css">

body {
margin: 0;
    background-color: black;
}

html, body, #my-image {
    width: 100%;
    height: 100%;
}

#my-image {
    background-size: cover;
    background-image: url(splash.png);
    background-position: center center;
    background-repeat: no-repeat;
}

</style>
</head>
<body>

<div id="my-image"></div>

</body>
</html>

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
没问题,但是android.graphics.Bitmap.createScaledBitmap可以为你完成这个任务 :) - Adam Stelmaszczyk

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接