Android HTML.ImageGetter图片已调整大小

时间:2013-08-13 23:26:59

标签: java android html android-drawable

我有一个加载HTML内容的TextView(也有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;
        }
    }
}

3 个答案:

答案 0 :(得分: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;

    }
}

答案 1 :(得分: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类。

答案 2 :(得分:0)

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