首页 > 代码库 > Android特效开发(仿zaker用手向上推动的效果(推动门效果))
Android特效开发(仿zaker用手向上推动的效果(推动门效果))
Android特效开发(仿zaker用手向上推动的效果(推动门效果))
[1].[代码] [Java]代码 跳至 [1]
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 | 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你可以设置别的 我这里选择的是有弹跳效果的Interpolator Interpolator 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(); } @Override public 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); } @Override public 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); } } } } |
Android特效开发(仿zaker用手向上推动的效果(推动门效果))
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。