Activity_main.xml file Configuration
<LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:tools= "Http://schemas.android.com/tools"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"Android:paddingbottom= "@dimen/activity_vertical_margin"Android:paddingleft= "@dimen/activity_horizontal_margin"Android:paddingright= "@dimen/activity_horizontal_margin"Android:paddingtop= "@dimen/activity_vertical_margin"android:orientation= "vertical"Tools:context=". Mainactivity " > <ImageViewAndroid:id= "@+id/iv_src"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content" /> <ImageViewAndroid:id= "@+id/iv_rotate"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content" /></LinearLayout>
Mainactivity.java Code
1 PackageCom.imagerotota.view;2 3 Importandroid.app.Activity;4 ImportAndroid.graphics.Bitmap;5 Importandroid.graphics.BitmapFactory;6 ImportAndroid.graphics.Canvas;7 ImportAndroid.graphics.Matrix;8 ImportAndroid.graphics.Paint;9 ImportAndroid.os.Bundle;Ten ImportAndroid.os.SystemClock; One ImportAndroid.widget.ImageView; A - Public classMainactivityextendsActivity { - the PrivateImageView ivsrc; - PrivateImageView ivratate; - - @Override + protected voidonCreate (Bundle savedinstancestate) { - Super. OnCreate (savedinstancestate); + Setcontentview (r.layout.activity_main); A //get the corresponding control in the picture atIVSRC =(ImageView) Findviewbyid (R.ID.IV_SRC); -Ivratate =(ImageView) Findviewbyid (r.id.iv_rotate); - //convert src.jpg pictures to bitmap - FinalBitmap Srcbitmap =Bitmapfactory.decoderesource (Getresources (), r.drawable.src); - //in the original bitmap can not modify the picture, can only copy the picture to modify - //Srcbitmap.setpixel (+, color.red);//This modification is not valid in //displays the acquired picture in the IVSRC control - Ivsrc.setimagebitmap (srcbitmap); to + //Create a new thread to rotate the picture in the process - NewThread () { the intdegrees = 0; * Public voidrun () { $ while(true){Panax NotoginsengDegrees + = 5; - //every time you rotate a picture, you re-copy the original, reset the brush, and so on, or you can change it after the copied image . the //Copy the original picture + FinalBitmap Copybitmap =Bitmap.createbitmap (Srcbitmap.getwidth (), A srcbitmap.getheight (), Srcbitmap.getconfig ()); the //Create a replenishment with canvas to Copybitmap as a template + FinalCanvas Canvas =NewCanvas (copybitmap); - //Create a brush $ FinalPaint paint =NewPaint (); $ //Create a Matrix object - FinalMatrix Matrix =NewMatrix (); - the //rotate a picture -Matrix.setrotate (degrees, Srcbitmap.getwidth ()/2, Srcbitmap.getheight ()/2);Wuyi Canvas.drawbitmap (Srcbitmap,matrix, paint); the //☆ Note that the activity page UI cannot be updated directly in the child thread -Runonuithread (NewRunnable () { Wu Public voidrun () { - //put the modified picture into the relevant ivratate control About Ivratate.setimagebitmap (copybitmap); $ } - }); - //let the thread sleep for a second and then continue execution -Systemclock.sleep (1000); A } + } the }.start (); the } the}
Show effect
Picture rotation in Android