Package com.example.checkboxtest;
Import Android.annotation.SuppressLint;
Import Android.content.Context;
Import Android.graphics.Canvas;
Import Android.graphics.Color;
Import Android.os.Handler;
Import Android.os.Message;
Import Android.util.AttributeSet;
Import android.view.MotionEvent;
Import Android.view.View;
Import Android.view.ViewGroup;
Import Android.widget.Toast;
public class MyView extends ViewGroup {
View Leftview = null;
View Rightview = null;
Public MyView (context context, AttributeSet Attrs) {
Super (context, attrs);
View view = new View (context, attrs);
View.setbackgroundcolor (Color.Black);
This.addview (view, 0);
}
/**
* Measuring
*/
@SuppressLint ("Drawallocation")
@Override
protected void onmeasure (int widthmeasurespec, int heightmeasurespec) {
if (Getchildcount ()!= 2) {
try {
Custom exception
throw new Exception () {
@Override
public void Printstacktrace () {
System.err.println ("Only one view exists in MyView");
Super.printstacktrace ();
}
};
catch (Exception e) {
E.printstacktrace ();
}
}
Leftview = Getchildat (0);
Set the height and width of the Leftview
Leftview.measure (Widthmeasurespec, Heightmeasurespec);
Rightview = Getchildat (1);
Set the height and width of the Rightview
Rightview.measure (Widthmeasurespec, Heightmeasurespec);
Super.onmeasure (Widthmeasurespec, Heightmeasurespec);
}
/**
* Layout
*/
@ Suppresslint ("Newapi")
@Override
protected void OnLayout (Boolean Changed, int l, int t, int r, int b) {
System.out.println (l +), "+ t +", " + R + "," + B);
if (leftview!= null & Rightview!= null) {
& nbsp; //Set Leftview position, on the right side of the screen (initially invisible)
Leftview.layout (-r, 0, 0, b);
//Set the location of the Rightview in the screen
rightview.layout (L, T, R, b);
}
}
@Override
protected void OnDraw (Canvas Canvas) {
Super.ondraw (canvas);
}
@SuppressLint ("Newapi")
@Override
public boolean ontouchevent (Motionevent event) {
Final float X = Event.getx ();
float Y = event.gety ();
Switch (event.getaction ()) {
Case Motionevent.action_move:
System.out.println ("x:" + x);
if (X < 100) {
Scrollto (0, 0);
else if (X > Rightview.getmeasuredwidth ()-100) {//When the user slides to the right edge 100, the page closes
New Thread (new Runnable () {//new threads, sliding off
@Override
public void Run () {
for (int i = 0;; i++) {
try {
Thread.Sleep (3)//Rightview every 10ms right to ensure smooth sliding
catch (Interruptedexception e) {
E.printstacktrace ();
}
int len = (int) (X + 3 * i);
System.out.println ("len:" + len);
Messages = new message ();//Android non-UI thread does not allow direct manipulation of controls, you can send them to the handler class of the main thread
if (Len >= rightview.getmeasuredwidth ()) {
Message.what = 1;
Handler.sendmessage (message);//Send messages
Close View
Break
} else {
Message.what = 0; Send Message Auto Slide
Handler.sendmessage (message);
}
}
}
). Start ();
} else {
Scrollto ((int)-X, 0);
Calculate Transparency Information
float alpha = (float) (1.0-(float) (1.0/400) * X);
System.out.println ("Alpha:" + al);
Set transparency
Leftview.setalpha (Alpha);
}
Break
}
Set true to consume event events that are not passed outward
return true;
}
@SuppressLint ("Handlerleak")
Handler Handler = new Handler () {
@SuppressLint ("Newapi")
@Override
public void Handlemessage (msg) {
if (Msg.what = = 0) {
Scrollby ( -3, 0);//ViewGroup slide 3 to the right
else if (Msg.what = 1) {
Toast.maketext (GetContext (), "close",). Show ();
Setvisibility (View.gone);/set ViewGroup not visible (hidden)
}
}
};
}
To display the underlying activity interface, you need to use the fragment Add method