Android Faux 360 Desktop villain

Source: Internet
Author: User

First, customize the Floatswindowview to show the animated villain.

Import Android.annotation.suppresslint;import Android.content.context;import Android.content.intent;import Android.graphics.drawable.animationdrawable;import Android.util.attributeset;import Android.util.DisplayMetrics; Import Android.view.gravity;import Android.view.motionevent;import Android.view.view;import Android.view.viewtreeobserver.onpredrawlistener;import Android.view.windowmanager;import android.widget.Toast;@ Suppresslint ("clickableviewaccessibility") public class Floatswindowview extends View {private Context mcontext = null; Private WindowManager mwindowmgr = null;private windowmanager.layoutparams mwindowmgrparams = null;private animationdrawable manimationdrawable = null;private int iposx = 0;private int iposy = 0;private int ILastPosX = 0;private int ilastposy = 0;private Boolean bmoved = False;public Floatswindowview (context context) {This (context, NULL, 0);} Public Floatswindowview (context context, AttributeSet Attrs) {This (context, attrs, 0);} @SuppressWarnings ("Deprecation ") public Floatswindowview (context context, AttributeSet attrs, int defstyle) {Super (context, attrs, defstyle); mcontext = Context;mwindowmgr = (windowmanager) getcontext (). Getapplicationcontext (). Getsystemservice ("window"); Mwindowmgrparams = new Windowmanager.layoutparams (); InitParams (); manimationdrawable = new animationdrawable (); for ( int i = 0; I < 4; i++) {int id = getresources (). Getidentifier ("a" + I, "drawable", Mcontext.getpackagename ()); Manimationdrawable.addframe (Getresources (). getdrawable (ID), 100);} Manimationdrawable.setoneshot (false); this.setbackgrounddrawable (manimationdrawable); Onpredrawlistener listener = New Onpredrawlistener () {@Overridepublic Boolean onpredraw () {Manimationdrawable.start (); return true;}}; This.getviewtreeobserver (). Addonpredrawlistener (listener);} private void InitParams () {displaymetrics dm = getresources (). Getdisplaymetrics (); mwindowmgrparams.x = Dm.widthpixels- 136;mwindowmgrparams.y = 300;mwindowmgrparams.width = 136;mwindowmgrparams.height = 136;} @Overridepublic Boolean ontouchevent (Motionevent event) {switch (event.getaction ()) {case Motionevent.action_down: IPOSX = (int) event.getx (); iposy = (int) event.gety (); bmoved = False;break;case MotionEvent.ACTION_MOVE:bMoved = True;ila  STPOSX = (int) event.getx (); ilastposy = (int) event.gety (); Updatepostion (Ilastposx-iposx, ilastposy-iposy); break;case MotionEvent.ACTION_UP:if (!bmoved) {Intent it = new Intent (Mcontext, Mainactivity.class); mcontext.startactivity (it);} Break;default:break;} if (event.getaction () = = Motionevent.action_move) {getParent (). Requestdisallowintercepttouchevent (True); if (event.getaction () = = motionevent.action_up) {if ((Iposx = = ilastposx) && (iposy = ilastposy)) {Singleclick (); return true;}} return true;} private void Updatepostion (int x, int y) {mwindowmgrparams.type = 2003;mwindowmgrparams.format = 1; Mwindowmgrparams.flags = 40;mwindowmgrparams.gravity = Gravity.left | Gravity.top;mwindowmgrparams.x + = X;mwindowmgrparams.y + Y;mwindowmgr.updateviewLayout (this, mwindowmgrparams);} public void Singleclick () {Toast.maketext (Mcontext, "clicked Ali, wow clicks", toast.length_short). Show ();}}

  

Then add a custom villain to the activity, you can handle the villain's click Logic, sliding logic.

Import Android.os.bundle;import Android.util.displaymetrics;import Android.view.gravity;import Android.view.windowmanager;import Android.app.activity;import Android.content.context;public class MainActivity Extends Activity {private static WindowManager Mwindowmgr = null;private windowmanager.layoutparams mwindowmgrparams = nu Ll;private static Floatswindowview Mfloatswindowview = null, @Overrideprotected void OnCreate (Bundle savedinstancestate {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main);} @Overrideprotected void OnStop () {super.onstop ();} @Overridepublic void Onwindowfocuschanged (Boolean hasfocus) {getwindowlayout ();} private void InitParams () {displaymetrics dm = getresources (). Getdisplaymetrics (); mwindowmgrparams.x = Dm.widthpixels- 136;mwindowmgrparams.y = 300;mwindowmgrparams.width = 136;mwindowmgrparams.height = 136;} private void Getwindowlayout () {if (Mfloatswindowview = = null) {Mwindowmgr = (WindowManager) getbasecontext (). Getsystemservice (Context.wiNdow_service) Mwindowmgrparams = new Windowmanager.layoutparams ();/* 2003 is hovering over all interfaces (4.0+ system, under the drop down menu, While in 2.3, above the pull-down menu) */mwindowmgrparams.type = 2003;mwindowmgrparams.format = 1;/* * Code is actually wmparams.flags |= flag_not_ FOCUSABLE; * 40 is the origin of the Wmparams default attribute (+) + flag_not_focusable (8) */mwindowmgrparams.flags = 40;mwindowmgrparams.gravity = Gravity.left | Gravity.top;initparams (); mfloatswindowview = new Floatswindowview (this); Mwindowmgr.addview (Mfloatswindowview, mwindowmgrparams);}}}

  

Android Faux 360 Desktop villain

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.