Android imitation iOS Assistivetouch control Easytouch implementation

Source: Internet
Author: User

Overview:

I've heard a news story before that Ipone's Assistivetouch was designed to be used by people with disabilities. This feature is most frequently used in Asia (China).

Although the reliability of the news is not known, it is no doubt that it does give us a great convenience in operating the mobile phone. Before this design, it may be easier to think of a shortcut, and the result of the shortcut is to load the interface (sometimes it may be a heavy interface). Once on this path, the direction of the quick operation may be fading away.

Assistivetouch's design is really great. Android is also worth having this awesome feature, let me briefly explain how to achieve this on Android.


idea Finishing:

At one glance, we may be confused about how to add controls to the top of the system desktop in Android. Yes, this is a difficult point. From the size, you may think of dialog, but Android dialog can not be displayed on the desktop of the system. And you might say that it's not a dialog theme for activity? Yes, it does solve the pop-up window above the system desktop. However, it may not be easy for Android to drag and drop the controls at random.

However,Android allows us to add view on WindowManager. The window mechanism in Android is based on the WindowManager implementation. WindowManager's role is to add a view to the screen or remove the view from the screen. It shows the lowest level of the view.

Well, that's true. Windowmanger is the key to implementation. Let's implement it below.

However, it is important to note that our Easytouchview is created based on a common context, if Easytouchview is based on a context created for a short life cycle like activity, Then the easytouchview will quickly disappear as the activity pauses or destroys.


implementation process:

Easytouchview:

Package Com.bumblebee.remindeasy.widgets;import Java.util.timer;import Java.util.timertask;import Com.bumblebee.remindeasy.r;import Android.content.context;import Android.graphics.color;import Android.graphics.drawable.bitmapdrawable;import Android.os.handler;import Android.os.message;import Android.view.gravity;import Android.view.layoutinflater;import Android.view.motionevent;import Android.view.View; Import Android.view.windowmanager;import Android.view.windowmanager.layoutparams;import Android.widget.Button; Import Android.widget.imageview;import Android.widget.popupwindow;import Android.widget.toast;public class    Easytouchview extends View {private Context mcontext;    Private WindowManager Mwmanager;    Private Windowmanager.layoutparams Mwmparams;    Private View Mtouchview;    Private ImageView Miconimageview = null;    Private Popupwindow Mpopuwin;    Private Servicelistener Mserlisrener;    Private View msettingtable;    private int mtag = 0;    private int midx; Privateint midy;    private int moldoffsetx;    private int moldoffsety;    Private Toast Mtoast;    Private Timer Mtimer = null;    Private TimerTask mtask = null;        Public Easytouchview (context context, Servicelistener Listener) {super (context);        Mcontext = context;    Mserlisrener = listener;                } public void Inittouchviewevent () {initeasytouchviewevent ();    Initsettingtableview (); } private void Initeasytouchviewevent () {//Set load view WindowManager parameter Mwmanager = (WindowManager) mCo        Ntext.getsystemservice (Context.window_service);        Midx = Mwmanager.getdefaultdisplay (). GetWidth ()/2-25;        Midy = Mwmanager.getdefaultdisplay (). GetHeight ()/2-44;        Mtouchview = Layoutinflater.from (Mcontext). Inflate (R.layout.easy_touch_view, NULL);        Miconimageview = (ImageView) Mtouchview.findviewbyid (R.id.easy_touch_view_imageview);                Mtouchview.setbackgroundcolor (color.transparent); Mtouchview.setonTouchlistener (Mtouchlistener);        WindowManager wm = Mwmanager;        Windowmanager.layoutparams wmparams = new Windowmanager.layoutparams ();        Mwmparams = Wmparams; Wmparams.type = 2003;        The 2002 here represents a system-level window, you can also try 2003. Wmparams.flags = 40;        Set Desktop controllable wmparams.width = 100;        Wmparams.height = 100; Wmparams.format =-3;    Transparent Wm.addview (Mtouchview, wmparams); private void Initsettingtableview () {msettingtable = Layoutinflater.from (Mcontext). Inflate (R.layout.show_        Setting_table, NULL);        Button Commonusebutton = (button) Msettingtable.findviewbyid (R.id.show_setting_table_item_common_use_button);        Button Screenlockbutton = (button) Msettingtable.findviewbyid (R.id.show_setting_table_item_screen_lock_button);                Button Notificationbutton = (button) Msettingtable.findviewbyid (R.id.show_setting_table_item_notification_button); Button Phonebutton = (button) Msettingtable.findviewbyid (R.id.show_settiNg_table_item_phone_button);        Button Pagebutton = (button) Msettingtable.findviewbyid (R.id.show_setting_table_item_page_button);                Button Camerabutton = (button) Msettingtable.findviewbyid (R.id.show_setting_table_item_camera_button);        Button Backbutton = (button) Msettingtable.findviewbyid (R.id.show_setting_table_item_back_button);        Button Homebutton = (button) Msettingtable.findviewbyid (R.id.show_setting_table_item_home_button);                Button Exittouchbutton = (button) Msettingtable.findviewbyid (R.id.show_setting_table_item_exit_touch_button);        Commonusebutton.setonclicklistener (Mclicklistener);        Screenlockbutton.setonclicklistener (Mclicklistener);                Notificationbutton.setonclicklistener (Mclicklistener);        Phonebutton.setonclicklistener (Mclicklistener);        Pagebutton.setonclicklistener (Mclicklistener);                Camerabutton.setonclicklistener (Mclicklistener); Backbutton.setonclicklistener (mclicklisteNER);        Homebutton.setonclicklistener (Mclicklistener);    Exittouchbutton.setonclicklistener (Mclicklistener); } private Onclicklistener Mclicklistener = new Onclicklistener () {@Override public void OnClick (View v) {switch (V.getid ()) {case R.id.show_setting_table_item_common_use_button:hidesetti                Ngtable ("common");            Break                Case R.id.show_setting_table_item_screen_lock_button:hidesettingtable ("lock screen");            Break                Case r.id.show_setting_table_item_notification_button:hidesettingtable ("notice");                            Break                Case R.id.show_setting_table_item_phone_button:hidesettingtable ("telephone");            Break                Case R.id.show_setting_table_item_page_button:hidesettingtable ("1");            Break Case R.ID.SHOW_SETTING_TABLE_ITEM_CAMERA_BUTTON:HIDESETTINGTABLE ("Camera");                            Break                Case R.id.show_setting_table_item_back_button:hidesettingtable ("return");            Break                Case R.id.show_setting_table_item_home_button:hidesettingtable ("homepage");            Break                Case R.id.show_setting_table_item_exit_touch_button:quittouchview ();            Break        }        }    };                private void Quittouchview () {hidesettingtable ("exit");        Mwmanager.removeview (Mtouchview);                Mserlisrener.oncloseservice (TRUE);    Cleartimerthead ();        } private Ontouchlistener Mtouchlistener = new Ontouchlistener () {float lastx, lasty;        int PARAMX, Paramy;            public boolean OnTouch (View V, motionevent event) {Final int action = Event.getaction ();            float x = event.getrawx ();            Float y = Event.getrawy (); if (Mtag = = 0) {Moldoffsetx = MWMparams.x; Offset moldoffsety = mwmparams.y; OFFSET} switch (action) {case MotionEvent.ACTION_DOWN:motionActionDownE                Vent (x, y);                            Break                Case MotionEvent.ACTION_MOVE:motionActionMoveEvent (x, y);                            Break                Case MotionEvent.ACTION_UP:motionActionUpEvent (x, y);            Break            Default:break;        } return true;            } private void Motionactiondownevent (float x, float y) {lastx = x;            Lasty = y;            PARAMX = mwmparams.x;        Paramy = Mwmparams.y;            } private void Motionactionmoveevent (float x, float y) {int dx = (int) (X-LASTX);            int dy = (int) (y-lasty);            mwmparams.x = paramx + dx;            Mwmparams.y = Paramy + dy;          Mtag = 1;              Updated Hover window position mwmanager.updateviewlayout (Mtouchview, mwmparams);            } private void Motionactionupevent (float x, float y) {int newoffsetx = mwmparams.x;            int newoffsety = MWMPARAMS.Y; if (Moldoffsetx = = Newoffsetx && moldoffsety = = newoffsety) {Mpopuwin = new Popupwindow (MSETTINGTA                BLE, layoutparams.wrap_content, layoutparams.wrap_content); Mpopuwin.settouchinterceptor (New Ontouchlistener () {public boolean OnTouch (View V, motionevent event) {if (event.getaction () = = Motionevent.action_outside) {hidesettingtabl                            E ();                        return true;                    } return false;                                }                });                Mpopuwin.setbackgrounddrawable (New bitmapdrawable ());                Mpopuwin.settouchable (TRUE); mpopuwin.seTfocusable (TRUE);                Mpopuwin.setoutsidetouchable (TRUE);                                Mpopuwin.setcontentview (msettingtable); if (Math.Abs (MOLDOFFSETX) > Midx) {if (Moldoffsetx > 0) {moldoffsetx = m                    IdX;                    } else {moldoffsetx =-midx; }} if (Math.Abs (moldoffsety) > Midy) {if (moldoffset                    Y > 0) {moldoffsety = Midy;                    } else {moldoffsety =-midy;                }} mpopuwin.setanimationstyle (R.style.animationpreview);                Mpopuwin.setfocusable (TRUE);                Mpopuwin.update ();                                Mpopuwin.showatlocation (Mtouchview, Gravity.center,-moldoffsetx,-moldoffsety); if (Mtimer = = null) {CatchsettIngtabledismiss ();            }} else {mtag = 0;        }        }    };        private void Catchsettingtabledismiss () {Mtimer = new Timer (); Mtask = new TimerTask () {@Override public void run () {if (Mpopuwin = = N ull | |                !mpopuwin.isshowing ()) {handler.sendemptymessage (0x0);                } else {handler.sendemptymessage (0x1);                }            }        };    Mtimer.schedule (mtask, 0, 100);            } private void Cleartimerthead () {if (mtask! = null) {Mtask.cancel ();        Mtask = null;            } if (Mtimer! = null) {Mtimer.cancel ();        Mtimer = null; }} Handler Handler = new Handler () {public void Handlemessage (Message msg) {if (Msg.what = = 0x0 ) {miconimageview.setbackgrounddrawable (Getresources (). Getdrawable (r.drAwable.touch_ic)); } else if (msg.what = = 0x1) {miconimageview.setbackgrounddrawable (Getresources (). Getdrawable (r.drawable.tr            Ansparent));    }        };        }; public void Showtoast (context context, String text) {if (mtoast = = null) {mtoast = Toast.maketext (cont        Ext, text, toast.length_short);            } else {mtoast.settext (text);        Mtoast.setduration (Toast.length_short);    } mtoast.show ();        } private void Hidesettingtable (String content) {hidesettingtable ();    Showtoast (mcontext, content);        } private void Hidesettingtable () {if (null! = Mpopuwin) {Mpopuwin.dismiss ();    }} public interface Servicelistener {public void Oncloseservice (Boolean isclose); }}

Auxiliaryservice:

public class Auxiliaryservice extends Service implements Servicelistener {    private Intent mintent;    @Override public    IBinder onbind (Intent Intent) {        return null;    }    public void OnCreate () {        super.oncreate ();        New Easytouchview (this, this). Inittouchviewevent ();    }    @Override public    int Onstartcommand (Intent Intent, int flags, int startid) {        mintent = Intent;        Return Super.onstartcommand (Intent, flags, Startid);    }    @Override public    void Oncloseservice (Boolean isclose) {        stopservice (mintent);}    }


Here's one thing to watch out for. As you can see from the code above, we started Easytouchview by service. The general Easytouch will provide a lock screen function. To use one-click lock screen you need to activate the Device Manager, it is necessary to jump to some of the system interface, and these interfaces can not be started based on service, need to be based on activity to do processing. The service start-up process is flashing and disappears.

Here we can start our own activity in the service, and then start the Device Manager interface in this activity.


The code is as follows:

public class Auxiliaryactivity extends Activity {@Override protected void onCreate (Bundle savedinstancestate) {        Super.oncreate (savedinstancestate);        Requestwindowfeature (Window.feature_no_title);    Lockscreen ();        } private void Lockscreen () {Devicepolicymanager mdevicepolicymanager;                ComponentName Mcomponentname;        Mdevicepolicymanager = (Devicepolicymanager) getsystemservice (Context.device_policy_service);        Mcomponentname = new ComponentName (this, lockreceiver.class);            Determine if there is permission if (mdevicepolicymanager.isadminactive (Mcomponentname)) {Mdevicepolicymanager.locknow ();        Finish ();        } else {Activemanager (mcomponentname); }}/** * Activate Device Manager to get permissions */private void Activemanager (ComponentName componentname) {Intent inte        NT = new Intent (devicepolicymanager.action_add_device_admin); Intent.putextra (Devicepolicymanager.extra_device_admin,ComponentName);        Intent.putextra (Devicepolicymanager.extra_add_explanation, "One key lock the screen");        StartActivity (Intent);    Finish (); }}


:


TouchView


Showtableview


Code Download:

http://download.csdn.net/detail/u013761665/8894583

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced. http://blog.csdn.net/lemon_tree12138

Android imitation iOS Assistivetouch control Easytouch implementation

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.