仿zaker用手向上推動的效果(推動門效果)

來源:互聯網
上載者:User


原創,轉載請標明出處 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 mScreenHeigh = 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() {// 這個Interpolator你可以設定別的 我這裡選擇的是有彈跳效果的InterpolatorInterpolator polator = new BounceInterpolator();mScroller = new Scroller(mContext, polator);// 擷取螢幕解析度WindowManager wm = (WindowManager) (mContext.getSystemService(Context.WINDOW_SERVICE));DisplayMetrics dm = new DisplayMetrics();wm.getDefaultDisplay().getMetrics(dm);mScreenHeigh = dm.heightPixels;mScreenWidth = dm.widthPixels;// 這裡你一定要設定成透明背景,不然會影響你看到底層布局this.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);// 填充整個螢幕mImgView.setImageResource(R.drawable.bg1); // 預設背景addView(mImgView);}// 設定推動門背景public void setBgImage(int id) {mImgView.setImageResource(id);}// 設定推動門背景public void setBgImage(Drawable drawable) {mImgView.setImageDrawable(drawable);}// 推動門的動畫public void startBounceAnim(int startY, int dy, int duration) {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;// 只准上滑有效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) {// 向上滑動超過半個螢幕高的時候 開啟向上消失動畫startBounceAnim(this.getScrollY(), mScreenHeigh, 450);mCloseFlag = true;} else {// 向上滑動未超過半個螢幕高的時候 開啟向下彈動動畫startBounceAnim(this.getScrollY(), -this.getScrollY(), 1000);}}break;}return super.onTouchEvent(event);}@Overridepublic void computeScroll() {if (mScroller.computeScrollOffset()) {scrollTo(mScroller.getCurrX(), mScroller.getCurrY());Log.i("scroller", "getCurrX()= " + mScroller.getCurrX()+ "     getCurrY()=" + mScroller.getCurrY()+ "  getFinalY() =  " + mScroller.getFinalY());// 不要忘記更新介面postInvalidate();} else {if (mCloseFlag) {this.setVisibility(View.GONE);}}}}

另外向下彈動效果是用到Scroller類,而在構造Scroller類時加入BounceInterpolator,你也可以加入別的插值器。其實Scroller只是一個輔助View滑動的一個類,協助View儲存滑動資料的類,當view滑動時,你可以從scroller中取出滑動資料,而真實滑動運動效果其實是利用scrollTo瞬間達到目的地,View結合scroller以及scroller就可以實現這種推動門的效果。


像下面這樣整個項目父ViewGroup可以用Framelayout 

使其可以擷取touch事件

<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="第二層" />        <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="上滑可以進入首頁"            android:textColor="#ffffffff"            android:textSize="18sp" />    </com.manymore13.scrollerdemo.PullDoorView>


相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.