How to set size of the image?

2020-07-18 02:00发布

I have splash.png and want that this image take all place on the screen like fitXY for ImageView. splash.png has sizes 480x767.

What does I must to change in my code?

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

标签: android
3条回答
成全新的幸福
2楼-- · 2020-07-18 02:40

Scale the bitmap. This is what you're looking for:
android.graphics.Bitmap.createScaledBitmap

查看更多
ゆ 、 Hurt°
3楼-- · 2020-07-18 02:51

This is how i usually do mine with a scale method.

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;

}
查看更多
家丑人穷心不美
4楼-- · 2020-07-18 02:59

As a result, I used 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>
查看更多
登录 后发表回答