实例介绍
【实例简介】
【实例截图】
【核心代码】
public class CurtainView extends RelativeLayout implements OnTouchListener{ private static String TAG = "CurtainView"; private Context mContext; /** Scroller 拖动类 */ private Scroller mScroller; /** 屏幕高度 */ private int mScreenHeigh = 0; /** 屏幕宽度 */ private int mScreenWidth = 0; /** 点击时候Y的坐标*/ private int downY = 0; /** 拖动时候Y的坐标*/ private int moveY = 0; /** 拖动时候Y的方向距离*/ private int scrollY = 0; /** 松开时候Y的坐标*/ private int upY = 0; /** 广告幕布的高度*/ private int curtainHeigh = 0; /** 是否 打开*/ private boolean isOpen = false; /** 是否在动画 */ private boolean isMove = false; /** 绳子的图片*/ private ImageView img_curtain_rope; /** 广告的图片*/ private ImageView img_curtain_ad; /** 上升动画时间 */ private int upDuration = 1000; /** 下落动画时间 */ private int downDuration = 500; public CurtainView(Context context) { super(context); init(context); } public CurtainView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); init(context); } public CurtainView(Context context, AttributeSet attrs) { super(context, attrs); init(context); } /** 初始化 */ private void init(Context context) { this.mContext = context; //Interpolator 设置为有反弹效果的 (Bounce:反弹) Interpolator interpolator = new BounceInterpolator(); mScroller = new Scroller(context, interpolator); mScreenHeigh = BaseTools.getWindowHeigh(context); mScreenWidth = BaseTools.getWindowWidth(context); // 背景设置成透明 this.setBackgroundColor(Color.argb(0, 0, 0, 0)); final View view = LayoutInflater.from(mContext).inflate(R.layout.curtain, null); img_curtain_ad = (ImageView)view.findViewById(R.id.img_curtain_ad); img_curtain_rope = (ImageView)view.findViewById(R.id.img_curtain_rope); addView(view); img_curtain_ad.post(new Runnable() { @Override public void run() { // TODO Auto-generated method stub curtainHeigh = img_curtain_ad.getHeight(); Log.d(TAG, "curtainHeigh= " curtainHeigh); CurtainView.this.scrollTo(0, curtainHeigh); //注意scrollBy和scrollTo的区别 } }); img_curtain_rope.setOnTouchListener(this); } /** * 拖动动画 * @param startY * @param dy 垂直距离, 滚动的y距离 * @param duration 时间 */ public void startMoveAnim(int startY, int dy, int duration) { isMove = true; mScroller.startScroll(0, startY, 0, dy, duration); invalidate();//通知UI线程的更新 } @Override protected void onLayout(boolean changed, int l, int t, int r, int b) { // TODO Auto-generated method stub super.onLayout(changed, l, t, r, b); } @Override public void computeScroll() { //判断是否还在滚动,还在滚动为true if (mScroller.computeScrollOffset()) { scrollTo(mScroller.getCurrX(), mScroller.getCurrY()); //更新界面 postInvalidate(); isMove = true; } else { isMove = false; } super.computeScroll(); } @Override public boolean onTouch(View v, MotionEvent event) { // TODO Auto-generated method stub if (!isMove) { int offViewY = 0;//屏幕顶部和该布局顶部的距离 switch (event.getAction()) { case MotionEvent.ACTION_DOWN: downY = (int) event.getRawY(); offViewY = downY - (int)event.getX(); return true; case MotionEvent.ACTION_MOVE: moveY = (int) event.getRawY(); scrollY = moveY - downY; if (scrollY < 0) { // 向上滑动 if(isOpen){ if(Math.abs(scrollY) <= img_curtain_ad.getBottom() - offViewY){ scrollTo(0, -scrollY); } } } else { // 向下滑动 if(!isOpen){ if (scrollY <= curtainHeigh) { scrollTo(0, curtainHeigh - scrollY); } } } break; case MotionEvent.ACTION_UP: upY = (int) event.getRawY(); if(Math.abs(upY - downY) < 10){ onRopeClick(); break; } if (downY > upY) { // 向上滑动 if(isOpen){ if (Math.abs(scrollY) > curtainHeigh / 2) { // 向上滑动超过半个屏幕高的时候 开启向上消失动画 startMoveAnim(this.getScrollY(), (curtainHeigh - this.getScrollY()), upDuration); isOpen = false; } else { startMoveAnim(this.getScrollY(), -this.getScrollY(),upDuration); isOpen = true; } } } else { // 向下滑动 if (scrollY > curtainHeigh / 2) { // 向上滑动超过半个屏幕高的时候 开启向上消失动画 startMoveAnim(this.getScrollY(), -this.getScrollY(),upDuration); isOpen = true; } else { startMoveAnim(this.getScrollY(),(curtainHeigh - this.getScrollY()), upDuration); isOpen = false; } } break; default: break; } } return false; } /** * 点击绳索开关,会展开关闭 * 在onToch中使用这个中的方法来当点击事件,避免了点击时候响应onTouch的衔接不完美的影响 */ public void onRopeClick(){ if(isOpen){ CurtainView.this.startMoveAnim(0, curtainHeigh, upDuration); }else{ CurtainView.this.startMoveAnim(curtainHeigh,-curtainHeigh, downDuration); } isOpen = !isOpen; } }
好例子网口号:伸出你的我的手 — 分享!
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
网友评论
我要评论