The Android implementation Windows 8 Tile Style button
As follows:
I'm streamlining and sorting out a simple class based on the code I've circulated online, and I'm naming it for the moment: Windows8tileimageview, the Windows 8 Tile Style button, Windows8tileimageview is actually inherited from the standard Android ImageView, click on the Windows8tileimageview with shrinkage, side shrinkage and other more interesting effects. Now put all the code for this class Windows8tileimageview:
Package Windows8.tile;import Android.annotation.suppresslint;import Android.content.context;import Android.graphics.camera;import Android.graphics.canvas;import Android.graphics.matrix;import Android.graphics.paint;import Android.graphics.paintflagsdrawfilter;import Android.graphics.drawable.bitmapdrawable;import Android.graphics.drawable.drawable;import Android.os.Handler; Import Android.os.message;import Android.util.attributeset;import Android.view.motionevent;import Android.widget.imageview;public class Windows8tileimageview extends ImageView {public static final int Rotate_handler_ Message_start = 1;public static final int rotate_handler_message_turning = 2;public static final int Rotate_handler_messag e_turned = 3;public static final int rotate_handler_message_reverse = 6;public static final int Scale_handler_message_star t = 1;public static final int scale_handler_message_turning = 2;public static final int scale_handler_message_turned = 3;p ublic static final int Scale_handler_messaGe_reverse = 6;private Boolean Isantialias = true;private Boolean scaleonly = false;private Boolean issizechanged = false; Private Boolean isshowanimation = true;private int rotatedegree = 10;private Boolean isFirst = true;private float Minscale = 0.95f;private int Vwidth;private int Vheight;private Boolean isanimationfinish = True, Isactionmove = False,isscale = f Alse;private Camera Camera;boolean Xbigy = false;float Rolatex = 0;float Rolatey = 0;onviewclick onclick = null;public Win Dows8tileimageview (Context context) {super (context); camera = new camera ();} Public Windows8tileimageview (context context, AttributeSet Attrs) {Super (context, attrs); camera = new camera (); public void Setanimationonoff (Boolean oo) {isshowanimation = oo;} public void Setonclickintent (Onviewclick onclick) {this.onclick = onclick;} @SuppressLint ("drawallocation") @Overrideprotected void OnDraw (canvas canvas) {Super.ondraw (canvas), if (IsFirst) { IsFirst = False;init ();} Canvas.setdrawfilter (New Paintflagsdrawfilter (0, Paint.anti_alias_flag| Paint.filter_bitmap_flag));} public void init () {vwidth = GetWidth ()-Getpaddingleft ()-getpaddingright (); vheight = GetHeight ()-getpaddingtop ()-G Etpaddingbottom ();D rawable drawable = getdrawable (); bitmapdrawable BD = (bitmapdrawable) drawable;bd.setantialias (Isantialias);} @Overridepublic boolean ontouchevent (Motionevent event) {super.ontouchevent (event); if (!isshowanimation) return true; Switch (event.getaction () & Motionevent.action_mask) {case MotionEvent.ACTION_DOWN:float X = Event.getx (); float Y = E Vent.gety (); Rolatex = VWIDTH/2-X; Rolatey = VHEIGHT/2-Y; Xbigy = Math.Abs (Rolatex) > Math.Abs (rolatey)? True:false;isscale = x > Vwidth/3 && x < Vwidth * 2/3 && y > vheight/3&& Y < V Height * 2/3;isactionmove = false;if (Isscale) {if (isanimationfinish &&!issizechanged) {issizechanged = True;s Cale_handler.sendemptymessage (Scale_handler_message_start);}} else {if (scaleonly) {Scale_handler.sendeMptymessage (Scale_handler_message_start);} else {rotate_handler.sendemptymessage (Rotate_handler_message_start);}} Break;case MotionEvent.ACTION_MOVE:float x = Event.getx (); Float y = event.gety (); if (x > Vwidth | | y > Vheight | | x < 0 | | Y < 0) {Isactionmove = true;} else {isactionmove = false;} Break;case MotionEvent.ACTION_UP:if (Isscale) {if (issizechanged) scale_handler.sendemptymessage (scale_handler_ Message_reverse);} else {rotate_handler.sendemptymessage (rotate_handler_message_reverse);} break;} return true;} Public interface Onviewclick {public void OnClick (); @SuppressLint ("Handlerleak") private Handler Rotate_handler = new Handler () {private Matrix matrix = new Matrix ();p rivate F Loat count = 0;//Private Boolean clickguolv = false; @Overridepublic void Handlemessage (Message msg) {super.handlemessage ( msg); Matrix.set (Getimagematrix ()); switch (msg.what) {Case rotate_handler_message_start:count = 0;beginrotate (Matrix , (Xbigy count:0), (Xbigy 0:count)); Rotate_handler.senDemptymessage (rotate_handler_message_turning); Break;case rotate_handler_message_turning:beginrotate (Matrix, ( Xbigy? count:0), (Xbigy 0:count)) count++;if (Count < Getdegree ()) {Rotate_handler.sendemptymessage (rotate_handler_mess age_turning);} else {isanimationfinish = true;} Break;case rotate_handler_message_turned:beginrotate (Matrix, (Xbigy count:0), (Xbigy 0:count)); if (Count > 0) { Rotate_handler.sendemptymessage (rotate_handler_message_turned);} else {isanimationfinish = true;if (!isactionmove && onclick! = null) {Onclick.onclick ();}} Count--;count--;break;case Rotate_handler_message_reverse:count = Getdegree () beginrotate (Matrix, XbigY count:0), (Xbigy 0:count)); Rotate_handler.sendemptymessage (rotate_handler_message_turned); break;}}; Private synchronized void beginrotate (Matrix matrix, float rotatex,float rotatey) {//Bitmap BM = Getimagebitmap (); int SCA LeX = (int) (Vwidth * 0.5f), int scaleY = (int) (vheight * 0.5f); Camera.save (); Camera.rotatex (RoLatey > 0? Rotatey:-rotatey) Camera.rotatey (Rolatex < 0 Rotatex:-rotatex); Camera.getmatrix (matrix); Camera.restore ();// Control center Point if (Rolatex > 0 && Rotatex! = 0) {matrix.pretranslate (-vwidth,-scaley); Matrix.posttranslate (Vwidth, ScaleY);} else if (Rolatey > 0 && Rotatey! = 0) {matrix.pretranslate (-scalex,-vheight); Matrix.posttranslate (ScaleX, VHe ight);} else if (Rolatex < 0 && Rotatex! = 0) {matrix.pretranslate ( -0,-scaley); matrix.posttranslate (0, ScaleY);} else if (Rolatey < 0 && Rotatey! = 0) {matrix.pretranslate (-scalex, -0); Matrix.posttranslate (ScaleX, 0);} Setimagematrix (matrix);} Private Handler Scale_handler = new Handler () {private Matrix Matrix = new Matrix ();p rivate float S;int count = 0; @Overrid epublic void Handlemessage (Message msg) {super.handlemessage (msg); Matrix.set (Getimagematrix ()); switch (msg.what) { Case Scale_handler_message_start:if (!isanimationfinish) {return;} else {isanimationfinish = False;issizechanged = True; count = 0;s = (float) math.sqrt (math.sqrt (Minscale)); Beginscale (Matrix, s); Scale_handler.sendemptymessage (Scale_ handler_message_turning);} Break;case Scale_handler_message_turning:beginscale (Matrix, s); if (Count < 4) {Scale_handler.sendemptymessage ( scale_handler_message_turning);} else {isanimationfinish = true;if (!issizechanged &&!isactionmove && onclick! = null) {Onclick.onclick () ;}} Count++;break;case Scale_handler_message_reverse:if (!isanimationfinish) {scale_handler.sendemptymessage (Scale_ Handler_message_reverse);} else {isanimationfinish = False;count = 0;s = (float) math.sqrt (math.sqrt (1.0f/minscale)); Beginscale (Matrix, s); Scale_h Andler.sendemptymessage (scale_handler_message_turning); issizechanged = false;} break;}}; Private synchronized void Beginscale (matrix matrix, float scale) {int scaleX = (int) (vwidth * 0.5f); int scaleY = (int) (V Height * 0.5f); Matrix.postscale (scale, scale, ScaleX, ScaleY); Setimagematrix (matrix);} public int Getdegree () {return rotatEdegree;} public void Setdegree (int degree) {rotatedegree = degree;} public float Getscale () {return minscale;} public void Setscale (float scale) {Minscale = scale;}}
Windows8tileimageview is simple to use, similar to Android's ImageView, and uses it directly as a view. For example, you can use this in a layout file:
<?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:gravity=" center "Andro id:orientation= "vertical" > <linearlayout android:layout_width= "wrap_content" android:layout_height = "Wrap_content" android:orientation= "vertical" > <linearlayout android:layout_width= "Wrap_c Ontent "android:layout_height=" wrap_content "android:orientation=" Horizontal "> <li Nearlayout android:layout_width= "wrap_content" android:layout_height= "Wrap_content" android:orientation= "vertical" > <windows8.tile.windows8tileimageview Andro Id:id= "@+id/c_joke" android:layout_width= "wrap_content" android:layout_height= "Wrap_ Content "Android: layout_margin= "2DP" android:scaletype= "Matrix" android:src= "@drawable/left_top"/&G T <windows8.tile.windows8tileimageview android:id= "@+id/c_idea" Android:layout_widt H= "Wrap_content" android:layout_height= "wrap_content" android:layout_margin= "2DP" Android:scaletype= "Matrix" android:src= "@drawable/left_bottom"/> </l inearlayout> <windows8.tile.windows8tileimageview android:id= "@+id/c_constellation" Android:layout_width= "Wrap_content" android:layout_height= "Wrap_content" Android:la Yout_margin= "2DP" android:scaletype= "Matrix" android:src= "@drawable/right"/> < /linearlayout> <windows8.tile.windows8tileimageview android:id= "@+id/c_recommend" Andro Id:layOut_width= "Wrap_content" android:layout_height= "wrap_content" android:layout_margin= "2DP" Android:scaletype= "Matrix" android:src= "@drawable/bottom"/> </LinearLayout></LinearLayout>
Description: The ANDROID:SRC loaded drawable resource picture in this layout file is selected by itself, and can be set according to its own situation when used.
You can increase the click events for the control Windows8tileimageview, such as adding:
Package Windows8.tile;import Android.support.v7.app.actionbaractivity;import Android.widget.toast;import Android.os.bundle;public class Mainactivity extends actionbaractivity {@Overrideprotected void OnCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); Windows8tileimageview joke = (Windows8tileimageview) Findviewbyid (R.id.c_joke);//How to add a click event for a tile. Joke.setonclickintent (New Windows8tileimageview.onviewclick () {@Overridepublic void OnClick () {Toast.maketext ( Getapplicationcontext (), "Tile Click event", Toast.length_long). Show ();}});}}
Android implementation Windows 8 Tile Style button