Android 推門效果

來源:互聯網
上載者:User

標籤:

public class MainActivity extends RelativeLayout{        private Scroller scroller;    private Context context;    private int width;    private int height;    private ImageView imageView;        private int mLastDownY = 0;            private int mCurryY;        private int mDelY;        private boolean mCloseFlag = false;        public Main_skip(Context context, AttributeSet attrs) {        super(context, attrs);        this.context = context;        initView();    }        private void initView(){        // 這個Interpolator你可以設定別的 我這裡選擇的是有彈跳效果的Interpolator         BounceInterpolator interpolator = new BounceInterpolator();        scroller = new Scroller(context,interpolator);                        WindowManager manager = (WindowManager) context                .getSystemService(Context.WINDOW_SERVICE);        DisplayMetrics metrics = new DisplayMetrics();        manager.getDefaultDisplay().getMetrics(metrics);        width = metrics.widthPixels;        height = metrics.heightPixels;                // 這裡你一定要設定成透明背景,不然會影響你看到底層布局          this.setBackgroundColor(Color.argb(0, 0, 0, 0));        imageView = new ImageView(context);        LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT                , LayoutParams.MATCH_PARENT);        imageView.setLayoutParams(params);        imageView.setScaleType(ScaleType.FIT_XY);        imageView.setImageResource(R.drawable.a);        addView(imageView);                    }    public void setBGimg(int resID){        imageView.setImageResource(resID);    }        public void setBGimg(Drawable drawable){        imageView.setImageDrawable(drawable);    }        // 推動門的動畫      public void setAnimation(int startY, int dy, int duration){        scroller.startScroll(0, startY, 0, dy, duration);        invalidate();    }            @Override    public boolean onTouchEvent(MotionEvent event) {        switch (event.getAction()) {        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)>height/2){                    setAnimation(this.getScrollY(), height, 450);                    mCloseFlag = true;                }else{                    setAnimation(this.getScrollY(), -this.getScrollY(), 1000);                }            }            break;        default:            break;        }        return super.onTouchEvent(event);    }            @Override    public void computeScroll() {        if(scroller.computeScrollOffset()){            scrollTo(scroller.getCurrX(), scroller.getCurrY());            // 不要忘記更新介面              postInvalidate();          }else {              if (mCloseFlag) {                  this.setVisibility(View.GONE);              }         }        super.computeScroll();    }}

 

Android 推門效果

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.