1

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


}
4

2 に答える 2

4

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();
于 2012-06-07T20:17:10.877 に答える
2

プログラムで画像のサイズを変更してからXMLで使用できるとは思いません。XMLファイル内のすべてはほとんど静的であり、コンパイル時に読み取られるため、元に戻すことはできません。BitmapFactoryを使用してから、両方の画像をJavaで設定します。

于 2012-06-07T20:08:50.183 に答える