Android uses the suspension button to make the page turn effect _android

Source: Internet
Author: User
Tags prev

Today to share their own with the suspension button click to achieve a page effect example.

First of all, a button to achieve suspension, it is necessary to use the system's top-level window-related windowmanager,windowmanager.layoutparams. Then add the permissions in the Androidmanifest.xml:

<uses-permission android:name= "Android.permission.SYSTEM_ALERT_WINDOW"/>

Then, we'll set up the related properties for Windowmanager,windowmanager.layoutparams:

Private WindowManager wm=null;
Private Windowmanager.layoutparams wmparams=null;
private void Initfloatview () {
  //Get WindowManager
  wm= (WindowManager) Getapplicationcontext (). Getsystemservice ("window");
  Set Layoutparams (global variable) correlation parameter
   wmparams = new Windowmanager.layoutparams ();
  Wmparams.type=layoutparams.type_phone;  Sets the window type
  wmparams.format=pixelformat.rgba_8888;  Format the picture, the effect is background transparent
   //Set window flag
  Wmparams.flags=layoutparams.flag_not_touch_modal 
           | layoutparams.flag_not_focusable;
  In the upper left corner of the screen as the origin, set the X, y initial value
   wmparams.x=0;
  wmparams.y=0;
  Set the suspension window long width data
   wmparams.width=50;
  wmparams.height=50;
}

The view created by the WindowManager AddView method can achieve the suspension window effect! So we need to create 2 hover buttons for the screen.

/** * Create left hover button */private void Createleftfloatview () {leftbtn=new ImageView (t
    His);
    Leftbtn.setimageresource (R.drawable.prev);
    Leftbtn.setalpha (0);
    Leftbtn.setonclicklistener (New View.onclicklistener () {public void OnClick (View arg0) {//previous}}); Adjust the suspension window wmparams.gravity=gravity.left|
    gravity.center_vertical;
  Display Myfloatview image Wm.addview (leftbtn, wmparams);
    /** * Create right suspension button/private void Createrightfloatview () {rightbtn=new ImageView (this);
    Rightbtn.setimageresource (R.drawable.next);
    Rightbtn.setalpha (0);
    Rightbtn.setonclicklistener (New View.onclicklistener () {public void OnClick (View arg0) {//Next}}); Adjust the suspension window wmparams.gravity=gravity.right|
    gravity.center_vertical;
  Display Myfloatview image Wm.addview (rightbtn, wmparams); }

I set the image's alpha value to 0 because I don't want the hover button to show up at the beginning; I want to implement the gradient display and gradient hide of the hover button by touching the screen. So we're going to work on the gradient effect of the picture:

//ImageView alpha value private int malpha = 0;
  Private Boolean ishide; /** * Picture gradient display processing/private Handler Mhandler = new Handler () {public void Handlemessage (message msg) {if (M         
    Sg.what==1 && malpha<255) {//system.out.println ("---" +malpha);
    Malpha + 50;
       if (malpha>255) malpha=255;
       Leftbtn.setalpha (Malpha);
       Leftbtn.invalidate ();
       Rightbtn.setalpha (Malpha);
    Rightbtn.invalidate ();
    if (!ishide && malpha<255) mhandler.sendemptymessagedelayed (1, 100);
    }else if (msg.what==0 && malpha>0) {//system.out.println ("---" +malpha);
    Malpha-= 10;
    if (malpha<0) malpha=0;
    Leftbtn.setalpha (Malpha);
    Leftbtn.invalidate ();
    Rightbtn.setalpha (Malpha);
    Rightbtn.invalidate ();
    if (ishide && malpha>0) mhandler.sendemptymessagedelayed (0, 100); 
}      
  }
  };

We use 2 different methods to control the display of the suspension button, hide:

private void Showfloatview () {
  ishide = false;
  Mhandler.sendemptymessage (1);
}
private void Hidefloatview () {
new Thread () {public
  void run () {
  try {
        thread.sleep (1500);
        Ishide = true;
        Mhandler.sendemptymessage (0);
     } catch (Exception e) {
        ;
     }}}
. Start ();
}

In order to not allow the suspension button to display, immediately began to hide. I use a thread, pause for 1.5 seconds, and then start the gradient hide.
Next, I'll rewrite the ontouchevent touchscreen event for the activity, as follows:

 @Override Public
  Boolean ontouchevent (Motionevent event) {
    switch (event.getaction ()) {
      case Motionevent.action_move: Case
    Motionevent.action_down:
    //system.out.println ("========action_down");
    Showfloatview ();      
    break;
    Case MOTIONEVENT.ACTION_UP:
    //system.out.println ("========action_up");
    Hidefloatview ();        
    break;
  return true;
  }

Finally, destroy the suspension button when the activity is destroyed, or the suspension button will be suspended there. Therefore, we will rewrite the OnDestroy () method of the activity and call the WindowManager Removeview () method to remove the suspension button.

 @Override public
  void OnDestroy () {
    Super.ondestroy ();
    Destroying the suspension window
    Wm.removeview (LEFTBTN) when the program exits (activity destruction);
    Wm.removeview (RIGHTBTN);
  }

Here is the complete code for the program:

Package com.liux.pageflipper;
Import android.app.Activity;
Import Android.graphics.PixelFormat;
Import Android.os.Bundle;
Import Android.os.Handler;
Import Android.os.Message;
Import android.view.Gravity;
Import android.view.MotionEvent;
Import Android.view.View;
Import Android.view.WindowManager;
Import Android.view.WindowManager.LayoutParams;
Import Android.widget.ImageView;
Import Android.widget.ViewFlipper; /** * Suspension button to achieve the flip effect * <a href= "http://my.oschina.net/arthor" target= "_blank" rel= "nofollow" > @author </a> liux H Ttp://my.oschina.net/liux * @date 2012-2-10 PM 2:48:52 * * public class Pageflipperactivity extends activity{private Wi
  Ndowmanager Wm=null;
  Private Windowmanager.layoutparams Wmparams=null;
  Private ImageView Leftbtn=null;
  Private ImageView Rightbtn=null;
  ImageView's alpha value is private int malpha = 0;
  Private Boolean ishide;
  Private Viewflipper viewflipper = null; @Override public void OnCreate (Bundle savedinstancestate) {super.oncreatE (savedinstancestate);
    Setcontentview (R.layout.main);
    Viewflipper = (viewflipper) This.findviewbyid (r.id.myviewflipper);
  Initialize the suspension button Initfloatview (); /** * Initialize the suspension button */private void Initfloatview () {//Get WindowManager wm= (WindowManager) getapplicationconte
    XT (). Getsystemservice ("window");
    Set Layoutparams (global variable) correlation parameter Wmparams = new Windowmanager.layoutparams ();  Wmparams.type=layoutparams.type_phone;  Sets the window type wmparams.format=pixelformat.rgba_8888; Format the picture, the effect is background transparent//Set window flag Wmparams.flags=layoutparams.flag_not_touch_modal |
    layoutparams.flag_not_focusable;
    In the upper left corner of the screen as the origin, set the X, y initial value wmparams.x=0;
    wmparams.y=0;
    Set the suspension window long width data wmparams.width=50;
    wmparams.height=50;
    Create a suspension button Createleftfloatview ();
  Createrightfloatview ();
    /** * Create left hover button/private void Createleftfloatview () {leftbtn=new ImageView (this);
    Leftbtn.setimageresource (R.drawable.prev); LefTbtn.setalpha (0); Leftbtn.setonclicklistener (New View.onclicklistener () {public void OnClick (View arg0) {//previous Viewflippe
    R.setinanimation (Pageflipperactivity.this, r.anim.in_leftright);
    Viewflipper.setoutanimation (Pageflipperactivity.this, r.anim.out_leftright);
    Viewflipper.showprevious ();
    }
  }); Adjust the suspension window wmparams.gravity=gravity.left|
     gravity.center_vertical;
  Display Myfloatview image Wm.addview (leftbtn, wmparams);
    /** * Create right suspension button/private void Createrightfloatview () {rightbtn=new ImageView (this);
    Rightbtn.setimageresource (R.drawable.next);
     Rightbtn.setalpha (0); Rightbtn.setonclicklistener (New View.onclicklistener () {public void OnClick (View arg0) {//Next viewflipper.
    Setinanimation (Pageflipperactivity.this, r.anim.in_rightleft);
    Viewflipper.setoutanimation (Pageflipperactivity.this, r.anim.out_rightleft);
    Viewflipper.shownext ();
    }
  }); Adjust the suspension window Wmparams.gravity=gravity.rigHt|
     gravity.center_vertical;
  Display Myfloatview image Wm.addview (rightbtn, wmparams);
        /** * Picture gradient display processing/private Handler Mhandler = new Handler () {public void Handlemessage (msg) {         
    if (msg.what==1 && malpha<255) {//system.out.println ("---" +malpha);
    Malpha + 50;
    if (malpha>255) malpha=255;
    Leftbtn.setalpha (Malpha);
    Leftbtn.invalidate ();
    Rightbtn.setalpha (Malpha);
    Rightbtn.invalidate ();
    if (!ishide && malpha<255) mhandler.sendemptymessagedelayed (1, 100);
    }else if (msg.what==0 && malpha>0) {//system.out.println ("---" +malpha);
    Malpha-= 10;
    if (malpha<0) malpha=0;
    Leftbtn.setalpha (Malpha);
    Leftbtn.invalidate ();
    Rightbtn.setalpha (Malpha);
    Rightbtn.invalidate ();
    if (ishide && malpha>0) mhandler.sendemptymessagedelayed (0, 100);
  }      
  }
  };
    private void Showfloatview () {ishide = false; MhandleR.sendemptymessage (1);
         private void Hidefloatview () {new Thread () {public void run () {try {thread.sleep (1500)};
         Ishide = true;
      Mhandler.sendemptymessage (0);
      catch (Exception e) {;
  }}.start (); @Override public boolean ontouchevent (Motionevent event) {switch (event.getaction ()) {case MOTIONEVENT.A
    Ction_move:case Motionevent.action_down://system.out.println ("========action_down");      
    Showfloatview ();
    Break
    Case MOTIONEVENT.ACTION_UP://system.out.println ("========action_up");        
    Hidefloatview ();
  Break
  return true;
    @Override public void OnDestroy () {Super.ondestroy ();
    Destroying the suspension window Wm.removeview (LEFTBTN) when the program exits (activity destruction);
  Wm.removeview (RIGHTBTN); }
}

The above content is small to share the Android with the suspension button to achieve the page effect, I hope you like.

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.