Android Tutorials
Working With Android Contacts
Accessing Data With Android Cursors
Creating Lists Using The Android ListActivity
Android 9 Patch Scaled PNG Image Guide
Working With Images In Android
Exploring Android LinearLayout And RelativeLayout
Writing A Basic Android Application
Installing The Android SDK In Eclipse

Working With Images In Android

Image attributes

The bitmap classes provide access to the image attributes as well as perform some image manipulation.

Dimensions (width/height)

package higherpass.TestImages;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.widget.TextView;

public class TestImages extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        TextView text = (TextView) findViewById(R.id.hello);
        Bitmap bMap = BitmapFactory.decodeResource(getResources(), R.drawable.test);
        text.setText(Integer.toString(bMap.getWidth()) + " x " + 
       		Integer.toString(bMap.getHeight()));
    }
}

Scaling an image

Done by creating a bitmap of the scaled size.

public class TestImages extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        ImageView image = (ImageView) findViewById(R.id.test_image);
        Bitmap bMap = BitmapFactory.decodeResource(getResources(), R.drawable.test);
        Bitmap bMapScaled = Bitmap.createScaledBitmap(bMap, 150, 100, true);
        image.setImageBitmap(bMapScaled);
    }
}

Load the original bitmap resource. Then use Bitmap.createScaledBitmap(originalBitmap, newX, newY, true) with the original bitmap resource, the new X dimension, new Y dimension, and true to enable filter respectively.

Rotating images

Use an instance of the Matrix class from the SDK to apply rotation properties to an image. The rotate properties work rotating the image clockwise.

package higherpass.TestImages;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.os.Bundle;
import android.widget.ImageView;

public class TestImages extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        ImageView image = (ImageView) findViewById(R.id.test_image);
        Bitmap bMap = BitmapFactory.decodeResource(getResources(), R.drawable.test);
        Matrix mat = new Matrix();
        mat.postRotate(90);
        Bitmap bMapRotate = Bitmap.createBitmap(bMap, 0, 0, bMap.getWidth(), bMap.getHeight(), mat, true);
        image.setImageBitmap(bMapRotate);
    }
}

Create a bitmap resource, bMap, containing the original bitmap. Create a new instance of the Matrix class. Use the postRotate() method of the Matrix instance to set the amount of rotation in degrees. Use the Bitmap.createBitmap() method to create a new rotated bitmap. This instance of createBitmap() expects the original bitmap, starting X, starting Y, ending X, ending Y, Matrix translation, true to enable filters as arguments.

Creating and Working With Bitmaps <<  1 2 3
New Content