This example describes the bitmap usage in Android. Share to everyone for your reference, specific as follows:
Generally in the Android program to put the picture file in the Res/drawable directory can be used by the r.drawable.id, but in the memory card picture how to reference it? The following features are implemented to introduce the use of bitmap.
The procedure is as follows:
Import Java.io.File;
Import android.app.Activity;
Import Android.graphics.Bitmap;
Import Android.graphics.BitmapFactory;
Import Android.os.Bundle;
Import Android.view.View;
Import Android.view.View.OnClickListener;
Import Android.widget.Button;
Import Android.widget.ImageView;
Import Android.widget.TextView;
public class A10activity extends activity {private Button B;
Private ImageView IV;
Private TextView TV;
Private String filename= "sdcard/picture/red leaf. jpg"; Private String filename= "sdcard/picture/red leaf. jpg"; This is wrong, and the path is not the beginning of the//device/** called when the ' activity was first created.
* * @Override public void onCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);
b= (Button) Findviewbyid (R.id.button); B.setonclicklistener (New Onclicklistener () {@Override public void OnClick (View v) {//TODO auto-generated Method St
UB iv= (ImageView) Findviewbyid (R.id.imageview);
tv= (TextView) Findviewbyid (R.id.textview); FileF=new File (fileName); First to determine if the picture file exists if (f.exists ()) {//if present, bitmap the picture into the ImageView display/*bitmapfactory (Android.graphics.BitmapFactory) Is the object provided by the Android API, the object * 's DecodeFile () method converts a picture file in a mobile phone to a bitmap object.
* * Bitmap bm=bitmapfactory.decodefile (fileName);
Iv.setimagebitmap (BM);
Tv.settext (FileName);
} else{Tv.settext ("file does not exist");
}
}
});
}
}
Bitmapfactory also provides other methods, such as Decoderesource (), which converts a res/drawable image file into a bitmap object, Decodestream () Method can convert InputStream into bitmap objects.
The following example uses the Matrix.setrotate () method to implement the ImageView rotation. The principle is to put ImageView into the bitmap, and then use the Bitmap.createbitmap () method to create a new bitmap object, in which the setrotate () of the Matrix object is created. method to dynamically rotate the newly created bitmap. The rotated bitmap object is then created in a newly constructed bitmap and placed in the original ImageView.
The program looks like this:
Import android.app.Activity;
Import Android.graphics.Bitmap;
Import Android.graphics.BitmapFactory;
Import Android.graphics.Matrix;
Import android.graphics.drawable.BitmapDrawable;
Import Android.os.Bundle;
Import Android.view.View;
Import Android.view.View.OnClickListener;
Import Android.widget.Button;
Import Android.widget.ImageView;
Import Android.widget.TextView;
public class A11activity extends activity {private ImageView IV;
Private TextView TV;
Private Button left,right;
private int times;
private int angle; /** called the activity is a.
* * @Override public void onCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);
Times=1;
angle=1;
iv= (ImageView) Findviewbyid (R.ID.IV);
tv= (TextView) Findviewbyid (r.id.tv);
left= (Button) Findviewbyid (r.id.left);
Left.settext ("to the Left");
right= (Button) Findviewbyid (r.id.right);
Right.settext ("Turn Right"); Final Bitmap Bmp=bitmapfactoRy.decoderesource (Getresources (), r.drawable.a);
Introduce yourself to a picture a.png final int width=bmp.getwidth ();
Final int height=bmp.getheight ();
Iv.setimagebitmap (BMP); Left.setonclicklistener (New Onclicklistener () {@Override public void OnClick (View v) {//TODO auto-generated method
Stub angle--;
if (angle<-20) {//Set up rotate 20 degrees angle=-20;
int width01=width*times;
int height01=height*times;
float width02= (float) (width01/width);
float height02= (float) (width02/height);
Matrix m=new Matrix ();
M.postscale (width02, height02);
M.setrotate (5*angle);
Bitmap bmp01=bitmap.createbitmap (BMP, 0, 0, width, height, m, true);
Bitmapdrawable bd=new bitmapdrawable (BMP01);
Iv.setimagedrawable (BD);
Tv.settext (integer.tostring (5*angle));
}
}); Right.setonclicklistener (New Onclicklistener () {@Override public void OnClick (View v) {//TODO auto-generated Metho
D stub angle++;
if (angle>20) {angle=20;
int width01=width*times; int Height01=heiGht*times;
float width02= (float) (width01/width);
float height02= (float) (width02/height);
Matrix m=new Matrix ();
M.postscale (width02, height02);
M.setrotate (5*angle);
Bitmap bmp01=bitmap.createbitmap (BMP, 0, 0, width, height, m, true);
Bitmapdrawable bd=new bitmapdrawable (BMP01);
Iv.setimagedrawable (BD);
Tv.settext (integer.tostring (5*angle));
}
});
}
}
Res/layout/main.xml is as follows:
<?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:o" rientation= "vertical" >
<textview
android:id= "@+id/tv" android:layout_width= "Fill_parent"
android:layout_height= "wrap_content"
android:text= "@string/hello"/>
<button
android: Id= "@+id/left"
android:layout_width= "fill_parent" android:layout_height= "wrap_content"
/>
<button
android:id= "@+id/right"
android:layout_width= "fill_parent"
android:layout_ height= "Wrap_content"
/>
<imageview
android:id= "@+id/iv"
android:layout_width= Parent "
android:layout_height=" wrap_content "
/>
</LinearLayout>
For more information on Android-related content readers can view the site: "Android Development Introduction and Advanced Course" and "Android graphics and image processing skills summary"
I hope this article will help you with your Android programming.