Picture View (ImageView)
The ImageView class belongs to Android. The Widget package is also inherited from the Android.widget.View class, which derives subclasses such as ImageButton and Zoombutton, and is used primarily for processing images. You can set a picture resource either through the Setimagebitmap method or the Setimageresource (int) method, or by using the Android:src property.
ImageView class method
ImageView Example
Complete project: http://download.csdn.net/detail/sweetloveft/9424612
The following programs mainly learn the usage of ImageView, Bitmap, and Bitmapfactory, in which the ImageView method needs to be performed in the main thread, not in the worker thread, or it will cause a crash, bitmapfactory The role of image decoding, Bitmap equivalent to the decoded bitmap handle, ImageView equivalent to the rendering container.
1.mainactivity.java
Package Com.sweetlover.activity;import Com.sweetlover.imageview.r;import Android.app.activity;import Android.graphics.bitmap;import Android.graphics.bitmapfactory;import Android.graphics.matrix;import Android.os.bundle;import Android.view.keyevent;import Android.widget.imageview;import Android.widget.TextView; Import Android.widget.toast;public class Mainactivity extends Activity {private Bitmap Bitmap = null;private TextView text View = null;private ImageView ImageView = null, @Overrideprotected void OnCreate (Bundle savedinstancestate) {//TODO auto-g Enerated method Stubsuper.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main); TextView = (TextView ) Findviewbyid (r.id.textview1); ImageView = (ImageView) Findviewbyid (r.id.imageview1) Textview.settext ("Press 1 to enlarge \ t Press 2 to zoom out \ N Press 3 left, \ t Press 4 to turn right "); bitmap = Bitmapfactory.decoderesource (Getresources (), R.drawable.miku);} @Overridepublic boolean onKeyDown (int keycode, keyevent event) {//TODO auto-generated method stubstring Text;switch (KEYC ODE) {Case KeyEvent.KEYCODE_1:text = "enlarging picture"; Break;case KeyEvent.KEYCODE_2:text = "shrinking picture"; Break;case Keyevent.keycode _3:text = "turning left picture"; Break;case KeyEvent.KEYCODE_4:text = "right picture"; break;default:text = "Invalid button"; break; Toast.maketext (this, text, Toast.length_short). Show (); return Super.onkeydown (KeyCode, event);} @Overridepublic boolean onKeyUp (int keycode, keyevent event) {//TODO auto-generated method Stubswitch (keycode) {case Key Event.KEYCODE_1:ZoomImageView (2.0f); Break;case KeyEvent.KEYCODE_2:ZoomImageView (0.5f); break;case KeyEvent.KEYCODE_3:RotateImageView ( -10); Break;case KeyEvent.KEYCODE_4:RotateImageView (ten); break;} Return Super.onkeyup (KeyCode, event);} public void Zoomimageview (float rate) {Bitmap bmp; Matrix Matrix;int width, Height;matrix = new Matrix (); width = bitmap.getwidth (); height = bitmap.getheight (); Matrix.postscale (rate, rate); bmp = Bitmap.createbitmap (Bitmap, 0, 0, width, height, matrix, true); Imageview.setimagebitmap (BMP);} public void Rotateimageview (int degree) {Bitmap Bmp Matrix Matrix;int width, Height;matrix = new Matrix (); width = bitmap.getwidth (); height = bitmap.getheight (); Matrix.postscale (1f, 1f); matrix.setrotate (degree); bmp = Bitmap.createbitmap (Bitmap, 0, 0, width, height, matrix, true); Imageview.setimagebitmap (BMP);}}
2.activity_main.xml
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android " android:layout_width=" match_parent " android:layout_height=" match_parent " android:padding= "30DP" android:orientation= "vertical" > <textview android:id= "@+id/textview1" android: Layout_width= "Wrap_content" android:layout_height= "wrap_content" android:layout_gravity= "center" Android:textappearance= "? Android:attr/textappearancemedium"/> <imageview android:id= "@+id/ ImageView1 " android:layout_width=" wrap_content " android:layout_height=" Wrap_content " android: layout_gravity= "center" android:contentdescription= "@string/app_name" android:src= "@drawable/miku"/ > </LinearLayout>
3.androidmanifest.xml
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android " android:layout_width=" match_parent " android:layout_height=" match_parent " android:padding= "30DP" android:orientation= "vertical" > <textview android:id= "@+id/textview1" android: Layout_width= "Wrap_content" android:layout_height= "wrap_content" android:layout_gravity= "center" Android:textappearance= "? Android:attr/textappearancemedium"/> <imageview android:id= "@+id/ ImageView1 " android:layout_width=" wrap_content " android:layout_height=" Wrap_content " android: layout_gravity= "center" android:contentdescription= "@string/app_name" android:src= "@drawable/miku"/ > </LinearLayout>
Read "Android from getting Started to mastering" (20)--Image view