1

我有一个 TextView 加载 HTML 内容(也有 img 标签)。我的问题是某些图像太大,因此应用程序崩溃。有什么办法可以让图片都一样大吗?

我的代码示例:

            String EntireStire = "<b>" + mItem.title + " </b> <br> <img src='"
                + mItem.photo + "' > " + " <br><small>" + mItem.description
                + " <br> " + mItem.content + "</small>";

        noteView.setText(Html.fromHtml(EntireStire, new MyImageGetter(),
                null));
        noteView.setMovementMethod(LinkMovementMethod.getInstance());
    }

    return rootView;
}

private class MyImageGetter implements Html.ImageGetter {

    @Override
    public Drawable getDrawable(String arg0) {
        Bitmap bitmap;
        try {
            bitmap = BitmapFactory.decodeStream(
                    (InputStream) new URL(arg0).getContent(), null, null);
            @SuppressWarnings("deprecation")
            Drawable drawable = new BitmapDrawable(bitmap);
            drawable.setBounds(0, 0, drawable.getIntrinsicWidth(),
                    drawable.getIntrinsicHeight());
            return drawable;
        } catch (Exception e) {
            Drawable d = getResources().getDrawable(R.drawable.ic_launcher);
            d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
            return d;
        }
    }
}
4

3 回答 3

2

请改用此代码,它会调整您的图像大小

private class MyImageGetter implements Html.ImageGetter {
    private final Context context;

    public MyImageGetter (Context context){
        this.context = context;
    }

    @Override
    public Drawable getDrawable(String source) {
        int path = context.getResources().getIdentifier(source, "drawable",
                BuildConfig.APPLICATION_ID);
            Drawable drawable = context.getResources().getDrawable(path);
            Display display = getWindowManager().getDefaultDisplay();
            Point size = new Point();
            display.getSize(size);
            int widthD = size.x;
            int heightD = size.y;
            drawable.setBounds(0, 0, widthD, (widthD* drawable.getIntrinsicHeight()/ drawable.getIntrinsicWidth()));
            return drawable;

    }
}
于 2017-04-06T20:56:04.197 回答
1

设置边界时使用上限宽度/高度将确保所有图像的大小相同。在这里,我稍微修改了您的MyImageGetter方法。

private class MyImageGetter implements Html.ImageGetter {

@Override
public Drawable getDrawable(String arg0) {
    Bitmap bitmap;
    try {
        bitmap = BitmapFactory.decodeStream(
                (InputStream) new URL(arg0).getContent(), null, null);
        @SuppressWarnings("deprecation")
        Drawable drawable = new BitmapDrawable(bitmap);
        float multiplier = (float)SOME_CAPPED_WIDTH / (float)drawable.getIntrinsicWidth();
        int width = (int)(drawable.getIntrinsicWidth() * multiplier);
        int height = (int)(drawable.getIntrinsicHeight() * multiplier);
        drawable.setBounds(0, 0, width, height);
        return drawable;
    } catch (Exception e) {
        Drawable d = getResources().getDrawable(R.drawable.ic_launcher);
        d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
        return d;
    }
}

}

为了支持多种屏幕尺寸,您可以使用DisplayMetrics类。

于 2014-01-04T05:43:22.023 回答
-1
Drawable d = getResources().getDrawable(R.drawable.ic_launcher);
        d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());

如果你想调整高度和宽度,那么你可以改变

Drawable d = getResources().getDrawable(R.drawable.ic_launcher);
        d.setBounds(0, 0, 100, 100);

或者如果你想压缩它,请执行以下操作

Bitmap bitmap = ((BitmapDrawable)drawable).getBitmap();
ByteArrayOutputStream out = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
drawable = new BitmapDrawable(_context.getResources(),bitmap);
于 2015-06-07T11:43:13.380 回答