Android image rotation is an old topic, but you need to think about how to solve it easily and quickly without any bugs.
First, let's review the APIS provided by Android image rotation.
1. Matrix
2. Animation
To put it bluntly, the core of animation is also matrix. In addition, animation is not scalable and is not easy to use. We should all know it if we have used it.
Let's start the matrix route. The following shows the arrows used in the game and the animation is shifted back and forth.
The core code is as follows:
Package com. laahaa. View. extension;
Import com. laahaa. R;
Import com. laahaa. config. extensionlayoutconfig;
Import Android. content. context;
Import Android. Graphics. Bitmap;
Import Android. Graphics. matrix;
Import Android. Graphics. drawable. bitmapdrawable;
Import Android. View. gravity;
Import Android. widget. framelayout;
Import Android. widget. framelayout. layoutparams;
Import Android. widget. imageview;
/**
*
* <P> class name: The Middle arrow </P>
* <P> class instruction: </P>
* @ Author mercury create in 2012-2-7
*/
Public class centerarrow extends imageview {
Framelayout. layoutparams LP;
Private Bitmap bitmap;
Private matrix = new matrix ();
Public centerarrow (context ){
Super (context );
Lp = new framelayout. layoutparams (layoutparams. wrap_content, layoutparams. wrap_content, gravity. center );
}
/**
* The thread calls each interval.
* @ Param dir
*/
Public void ontick (int dir)
{
Switch (DIR ){
Case 0:
If (LP. bottommargin = (extensionlayoutconfig. centerarrowwidth + extensionlayoutconfig. centercirclewidth ))
LP. bottommargin + = extensionlayoutconfig. arrowoffset;
Else
LP. bottommargin-= extensionlayoutconfig. arrowoffset;
Break;
Case 1:
If (LP. leftmargin = (extensionlayoutconfig. centerarrowwidth + extensionlayoutconfig. centercirclewidth ))
LP. leftmargin + = extensionlayoutconfig. arrowoffset;
Else
LP. leftmargin-= extensionlayoutconfig. arrowoffset;
Break;
Case 2:
If (LP. bottommargin =-(extensionlayoutconfig. centerarrowwidth + extensionlayoutconfig. centercirclewidth ))
LP. bottommargin-= extensionlayoutconfig. arrowoffset;
Else
LP. bottommargin + = extensionlayoutconfig. arrowoffset;
Break;
Case 3:
If (LP. leftmargin =-(extensionlayoutconfig. centerarrowwidth + extensionlayoutconfig. centercirclewidth ))
LP. leftmargin-= extensionlayoutconfig. arrowoffset;
Else
LP. leftmargin + = extensionlayoutconfig. arrowoffset;
Break;
}
This. setlayoutparams (LP );
This. setimagebitmap (Bitmap );
}
Public void setrotate (int dir)
{
Int degrees = 0;
Switch (DIR ){
Case 0:
Degrees = 90;
LP. leftmargin = 0;
LP. rightmargin = 0;
LP. bottommargin = extensionlayoutconfig. centerarrowwidth + extensionlayoutconfig. centercirclewidth;
LP. topmargin = 0;
Break;
Case 1:
Degrees = 0;
LP. leftmargin = extensionlayoutconfig. centerarrowwidth + extensionlayoutconfig. centercirclewidth;
LP. rightmargin = 0;
LP. bottommargin = 0;
LP. topmargin = 0;
Break;
Case 2:
Degrees = 270;
LP. leftmargin = 0;
LP. rightmargin = 0;
LP. bottommargin =-(extensionlayoutconfig. centerarrowwidth + extensionlayoutconfig. centercirclewidth );
LP. topmargin = 0;
Break;
Case 3:
Degrees = 180;
LP. leftmargin =-(extensionlayoutconfig. centerarrowwidth + extensionlayoutconfig. centercirclewidth );
LP. rightmargin = 0;
LP. bottommargin = 0;
LP. topmargin = 0;
Break;
}
Bitmap = (bitmapdrawable) getresources (). getdrawable (R. drawable. Arrow). getbitmap ();
This. setlayoutparams (LP );
// Set the Image Rotation Angle
Matrix. setrotate (degrees );
// Rotate the image and generate a new bitmap object
Bitmap = bitmap. createbitmap (bitmap, 0, 0, bitmap. getwidth (), bitmap. getheight (), matrix, true );
// Re-display the rotated image in the imageview component
This. setimagebitmap (Bitmap );
}
}