【问题标题】:Need to resize image for imageview需要为 imageview 调整图像大小
【发布时间】:2012-06-07 20:04:37
【问题描述】:

我有一个 8 mp 的图像,但它无法在 ImageView 上显示,因为它的文件太大。如何使用 BitmapFactory 重新调整大小,然后将此新图像作为 XML 中 ImageView 的源传递?

我的 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" >

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <Button
        android:id="@+id/select"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Select" />

    <Button
        android:id="@+id/info"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Info" />

</LinearLayout>

<ImageView
    android:id="@+id/test_image_view"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />


</LinearLayout>

我的活动:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ImageView view;

    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inSampleSize=8; //also tried 32, 64, 16
    Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher, options);
    Drawable d = new BitmapDrawable(bmp);

    view = (ImageView) findViewById(R.id.test_image_view);
    view.setImageDrawable(d);

    setContentView(R.layout.main);


}

【问题讨论】:

    标签: android image imageview bitmapfactory


    【解决方案1】:

    你不能把它传入 XML

    为了什么?加载它,当你需要它时。 在您的情况下,最好使用 BitmapFactory.Options:

    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inSampleSize=16; //image will be 16x smaller than an original. Note, that it's better for perfomanse to use powers of 2 (2,4,8,16,32..).
    Bitmap bmp=BitmapFactory.decodeSomething(..., options) //use any decode method instead of this
    Drawable d=new BitmapDrawable(bmp);
    

    然后将其设置为您的ImageView

    ImageView iv; //initialize it first, of course. For example, using findViewById.
    iv.setImageDrawable(d);
    

    请注意,您应该在使用后回收位图(在 ImageView 不再可见之后):

    bmp.recycle();
    

    【讨论】:

    • 它仍然崩溃,我正在发布我的代码,你能快速看一下吗?
    • @Aneem 首先 - 你没有在 decodeResource 方法中设置你的选项(它应该是最后一个参数)。第二 - 也许值 16 不是你要找的(你也可以尝试 32、64 等)
    • @Aneem 哦,我明白了,对不起 :) 主要问题是你应该在super.onCreate() 之后写你的setContentView。否则你的 UI 将无法工作。
    • 哇,它现在可以工作了!非常感谢 biovamp,你太棒了!
    【解决方案2】:

    我不相信您可以以编程方式调整图像大小,然后在 XML 中使用它。 XML 文件中的所有内容几乎都是静态的,并且在编译时读取,因此没有回头路。 您将使用 BitmapFactory,然后在 java 中设置图像。

    【讨论】:

    • 那么请选择它作为答案:)
    • 附言。我已经多次遇到字符串问题,这就是我得出的结论。
    • 我会的,但我现在不能,它说我可以在 3 分钟内接受答案
    猜你喜欢
    • 2013-02-02
    • 1970-01-01
    • 2016-08-04
    • 2015-01-01
    • 1970-01-01
    • 2011-09-11
    • 1970-01-01
    • 1970-01-01
    • 2014-03-24
    相关资源
    最近更新 更多