package ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
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, 0);
}
/**
* Measurement
*/
@SuppressLint("DrawAllocation")
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
if (getChildCount() != 2) {
try {
// Custom Exception
throw new Exception() {
@Override
public void printStackTrace() {
("There can only be one View in MyView");
();
}
};
} catch (Exception e) {
();
}
}
leftView = getChildAt(0);
// Set the height and width of leftview
(widthMeasureSpec, heightMeasureSpec);
rightView = getChildAt(1);
// Set the height and width of the rightview
(widthMeasureSpec, heightMeasureSpec);
(widthMeasureSpec, heightMeasureSpec);
}
/**
* Layout
*/
@SuppressLint("NewApi")
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
(l + "," + t + "," + r + "," + b);
if (leftView != null & rightView != null) {
// Set the position of leftview, on the right side of the screen (initially invisible)
(-r, 0, 0, b);
// Set the position of the rightView, in the screen
(l, t, r, b);
}
}
@Override
protected void onDraw(Canvas canvas) {
(canvas);
}
@SuppressLint("NewApi")
@Override
public boolean onTouchEvent(MotionEvent event) {
final float X = ();
float Y = ();
switch (()) {
case MotionEvent.ACTION_MOVE:
("X:" + X);
if (X < 100) {
scrollTo(0, 0);
} else if (X > () - 100) {// When the user slides to 100 from the right edge, the page closes
new Thread(new Runnable() {// Create a new thread, slide to close
@Override
public void run() {
for (int i = 0;; i++) {
try {
(10);// RightView moves right 3 every 10ms to ensure smooth sliding
} catch (InterruptedException e) {
();
}
int len = (int) (X + 3 * i);
// ("len:" + len);
Message message = new Message();// Non-UI threads in Android do not allow direct operation of controls, and messages can be sent to the handler class of the main thread
if (len >= ()) {
= 1;
(message);// Send a message
// Close View
break;
} else {
= 0; // Send message Automatically swipe
(message);
}
}
}
}).start();
} else {
scrollTo((int) -X, 0);
// Calculate transparency information
float alpha = (float) (1.0 - (float) (1.0 / 400) * X);
// ("alpha:" + al);
// Set transparency
(alpha);
}
break;
}
// Set true, consume event events, not to be passed out
return true;
}
@SuppressLint("HandlerLeak")
Handler handler = new Handler() {
@SuppressLint("NewApi")
@Override
public void handleMessage(Message msg) {
if ( == 0) {
scrollBy(-3, 0);// viewgroup swipes right 3
} else if ( == 1) {
(getContext(), "Close", 50).show();
setVisibility();// Set viewgroup invisible (hidden)
}
}
};
}