Original, reprinted please indicate the source http://blog.csdn.net/manymore13/article/details/12219687
Package com. manymore13.scrollerdemo; import android. annotation. suppressLint; import android. content. context; import android. graphics. color; import android. graphics. drawable. drawable; import android. util. attributeSet; import android. util. displayMetrics; import android. util. log; import android. view. motionEvent; import android. view. view; import android. view. windowManager; import android. view. animation. accelerateInterpolator; import android. view. animation. bounceInterpolator; import android. view. animation. interpolator; import android. widget. frameLayout; import android. widget. imageView; import android. widget. relativeLayout; import android. widget. scroller; public class PullDoorView extends RelativeLayout {private Context mContext; private Scroller mScroller; private int mScreenWidth = 0; private int mscreenheweigh = 0; private int mLastDownY = 0; private int mCurryY; private int mDelY; private boolean mCloseFlag = false; private ImageView mImgView; public PullDoorView (Context context) {super (context); mContext = context; setupView ();} public PullDoorView (Context context, AttributeSet attrs) {super (context, attrs); mContext = context; setupView () ;}@ SuppressLint ("NewApi") private void setupView () {// you can set another InterpolatorInterpolator with bounce effect. polator = new BounceInterpolator (); mScroller = new Scroller (mContext, polator ); // obtain the screen resolution WindowManager wm = (WindowManager) (mContext. getSystemService (Context. WINDOW_SERVICE); DisplayMetrics dm = new DisplayMetrics (); wm. getdefadisplay display (). getMetrics (dm); mScreenHeigh = dm. heightPixels; mScreenWidth = dm. widthPixels; // you must set it to a transparent background. Otherwise, the underlying layout will be affected. setBackgroundColor (Color. argb (0, 0, 0, 0); mImgView = new ImageView (mContext); mImgView. setLayoutParams (new LayoutParams (LayoutParams. MATCH_PARENT, LayoutParams. MATCH_PARENT); mImgView. setScaleType (ImageView. scaleType. FIT_XY); // fill in the mImgView of the entire screen. setImageResource (R. drawable. bg1); // Default background addView (mImgView);} // set the public void setBgImage (int id) {mImgView. setImageResource (id);} // set the public void setBgImage (Drawable drawable) {mImgView. setImageDrawable (drawable);} // The public void startBounceAnim (int startY, int dy, int duration) of the door pushing animation {mScroller. startScroll (0, startY, 0, dy, duration); invalidate () ;}@ Overridepublic boolean onTouchEvent (MotionEvent event) {int action = event. getAction (); switch (action) {case MotionEvent. ACTION_DOWN: mLastDownY = (int) event. getY (); System. err. println ("ACTION_DOWN =" + mLastDownY); return true; case MotionEvent. ACTION_MOVE: mCurryY = (int) event. getY (); System. err. println ("ACTION_MOVE =" + mCurryY); mDelY = mCurryY-mLastDownY; // valid if (mDelY <0) {scrollTo (0,-mDelY);} System. err. println ("-------------" + mDelY); break; case MotionEvent. ACTION_UP: mCurryY = (int) event. getY (); mDelY = mCurryY-mLastDownY; if (mDelY <0) {if (Math. abs (mDelY)> mScreenHeigh/2) {// turn on the upward disappear animation startBounceAnim (this. getScrollY (), mScreenHeigh, 450); mCloseFlag = true;} else {// enable the downward bullet animation startBounceAnim (this. getScrollY (),-this. getScrollY (), 1000) ;}} break;} return super. onTouchEvent (event) ;}@ Overridepublic void computeScroll () {if (mScroller. computescroloffset () {scrollTo (mScroller. getCurrX (), mScroller. getCurrY (); Log. I ("scroller", "getCurrX () =" + mScroller. getCurrX () + "getCurrY () =" + mScroller. getCurrY () + "getFinalY () =" + mScroller. getFinalY (); // do not forget to update the interface postInvalidate ();} else {if (mCloseFlag) {this. setVisibility (View. GONE );}}}}
In addition, the scroler class is used for downward movement. You can also add BounceInterpolator when constructing the scroler class. In fact, scroroller is just a class that assists in viewing sliding. It helps the View to store sliding data. When the View slides, you can extract sliding data from scroroller, in fact, the actual sliding effect uses scrollTo to instantly reach the destination. View can be combined with scroroller and scroroller to achieve this door pushing effect.
As shown below, the parent ViewGroup of the entire project can use Framelayout
To get touch events
<Com. manymore13.scrollerdemo. pullDoorView android: id = "@ + id/myImage" android: layout_width = "match_parent" android: layout_height = "match_parent" android: background = "# ddd"> <Button android: id = "@ + id/btn_above" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: layout_centerInParent = "true" android: text = "Layer 2"/> <TextView android: id = "@ + id/TV _hint" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: layout_alignParentBottom = "true" android: layout_centerHorizontal = "true" android: layout_marginBottom = "10dp" android: text = "slide to the Homepage" android: textColor = "# ffffffff" android: textSize = "18sp"/> </com. manymore13.scrollerdemo. pullDoorView>