Android UI How to - ImageView and BitmapFactory








We can use BitmapFactory to load image for ImageView.

Example

<?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">
/*from  w w  w .  ja v a 2s. co m*/
    <ImageView
        android:id="@+id/image3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>

The following Java code uses BitmapFactory to load image.

package com.java2s.app;
/* w  w w.  j a va 2s  .  c o m*/
import android.app.Activity;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.widget.ImageView;

public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ImageView imgView = (ImageView)findViewById(R.id.image3);


        imgView.setImageBitmap(BitmapFactory.decodeResource(this.getResources(), R.drawable.ic_launcher));

    }

}
null