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 provide us with a very convenient operation of the mobile phone. Prior to this design. Perhaps the easy way to think of is to set up shortcuts. The result of the shortcut is to load the interface (which can sometimes be a heavy interface).

Once on this road. The direction of the quick operation may be fading away.

Assistivetouch's design is really great. Android is also worth having this awesome feature, and here's how to do it on Android.


idea Finishing:

Seeing this feature at a 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, maybe you think of dialog. Only the dialog in Android can not be displayed on the desktop of the system. And you might say that it's not a dialog theme for activity? Yes, this is actually a pop-up form on the top of the system desktop. But. We're going to have to drag and drop the controls again, which may not be easy for Android.

However,Android agrees that we are adding a view to the WindowManager. The form 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. Here's how it's going to come true.

It is just another point to note that our Easytouchview is created based on a common context. Suppose Easytouchview is based on a context creation of a short life cycle like activity. Then the easytouchview will disappear very quickly as the activity is suspended or destroyed.


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 parameters 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 form.        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 ("frequent use");            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 a little bit of attention. As you can see from the code above, we started Easytouchview by service. The general Easytouch will provide a lock screen function. To use the one-click Lock screen 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 are able to start a our own activity in the service. In this activity, you start the interface for the Setup Device Manager.


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

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.