Android image rotation

Source: Internet
Author: User

 

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 );
}
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.