ImageView and BitmapFactory

Description

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">
/*w w w  .ja va 2  s. 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  v a 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));

    }

}
ImageView and BitmapFactory




















Home »
  Android »
    Android UI »




UI Basics
Action Bar
Animation
Button
Canvas
CheckBox
Clock Date Picker
Dialog
EditText
Event
Fragment
Gesture
GridView
ImageView
Layout
ListView
Map
Menu
Model
OpenGL
ProgressBar
RadioButton
Spinner
Tab
TextView
Thread
Toast
Video
View
WebView