Android 仿 窗帘效果 和 登录界面拖动效果 (Scroller类的应用) 附 2个DEMO及源码
在android学习中,动作交互是软件中重要的一部分,其中的Scroller就是提供了拖动效果的类,在网上,比如说一些Launcher实现滑屏都可以通过这个类去实现。下面要说的就是上次Scroller类学习的后的实践了。
如果你还不了解Scroller类,那请先点击:Android 界面滑动实现---Scroller类 从源码和开发文档中学习(让你的布局动起来)
了解之后再阅读以下内容,你会发现原来实现起来很简单。




- 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;
- }
- }
- public class LoginView extends RelativeLayout {
- /** 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 Boolean isMoving = false;
- /** 布局的高度*/
- private int viewHeight = 0;
- /** 是否打开*/
- public boolean isShow = false;
- /** 是否可以拖动*/
- public boolean mEnabled = true;
- /** 点击外面是否关闭该界面*/
- public boolean mOutsideTouchable = true;
- /** 上升动画时间 */
- private int mDuration = 800;
- private final static String TAG = "LoginView";
- public LoginView(Context context) {
- super(context);
- init(context);
- }
- public LoginView(Context context, AttributeSet attrs) {
- super(context, attrs);
- init(context);
- }
- public LoginView(Context context, AttributeSet attrs, int defStyle) {
- super(context, attrs, defStyle);
- init(context);
- }
- private void init(Context context) {
- setDescendantFocusability(FOCUS_AFTER_DESCENDANTS);
- setFocusable(true);
- mScroller = new Scroller(context);
- mScreenHeigh = BaseTools.getWindowHeigh(context);
- mScreenWidth = BaseTools.getWindowWidth(context);
- // 背景设置成透明
- this.setBackgroundColor(Color.argb(0, 0, 0, 0));
- final View view = LayoutInflater.from(context).inflate(R.layout.view_login,null);
- LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);// 如果不给他设这个,它的布局的MATCH_PARENT就不知道该是多少
- addView(view, params);// ViewGroup的大小,
- // 背景设置成透明
- this.setBackgroundColor(Color.argb(0, 0, 0, 0));
- view.post(new Runnable() {
- @Override
- public void run() {
- // TODO Auto-generated method stub
- viewHeight = view.getHeight();
- }
- });
- LoginView.this.scrollTo(0, mScreenHeigh);
- ImageView btn_close = (ImageView)view.findViewById(R.id.btn_close);
- btn_close.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- // TODO Auto-generated method stub
- dismiss();
- }
- });
- }
- @Override
- public boolean onInterceptTouchEvent(MotionEvent ev) {
- if(!mEnabled){
- return false;
- }
- return super.onInterceptTouchEvent(ev);
- }
- @Override
- public boolean onTouchEvent(MotionEvent event) {
- // TODO Auto-generated method stub
- switch (event.getAction()) {
- case MotionEvent.ACTION_DOWN:
- downY = (int) event.getY();
- Log.d(TAG, "downY = " + downY);
- //如果完全显示的时候,让布局得到触摸监听,如果不显示,触摸事件不拦截,向下传递
- if(isShow){
- return true;
- }
- break;
- case MotionEvent.ACTION_MOVE:
- moveY = (int) event.getY();
- scrollY = moveY - downY;
- //向下滑动
- if (scrollY > 0) {
- if(isShow){
- scrollTo(0, -Math.abs(scrollY));
- }
- }else{
- if(mScreenHeigh - this.getTop() <= viewHeight && !isShow){
- scrollTo(0, Math.abs(viewHeight - scrollY));
- }
- }
- break;
- case MotionEvent.ACTION_UP:
- upY = (int) event.getY();
- if(isShow){
- if( this.getScrollY() <= -(viewHeight /2)){
- startMoveAnim(this.getScrollY(),-(viewHeight - this.getScrollY()), mDuration);
- isShow = false;
- Log.d("isShow", "false");
- } else {
- startMoveAnim(this.getScrollY(), -this.getScrollY(), mDuration);
- isShow = true;
- Log.d("isShow", "true");
- }
- }
- Log.d("this.getScrollY()", ""+this.getScrollY());
- changed();
- break;
- case MotionEvent.ACTION_OUTSIDE:
- Log.d(TAG, "ACTION_OUTSIDE");
- break;
- default:
- break;
- }
- return super.onTouchEvent(event);
- }
- /**
- * 拖动动画
- * @param startY
- * @param dy 移动到某点的Y坐标距离
- * @param duration 时间
- */
- public void startMoveAnim(int startY, int dy, int duration) {
- isMoving = true;
- mScroller.startScroll(0, startY, 0, dy, duration);
- invalidate();//通知UI线程的更新
- }
- @Override
- public void computeScroll() {
- if (mScroller.computeScrollOffset()) {
- scrollTo(mScroller.getCurrX(), mScroller.getCurrY());
- // 更新界面
- postInvalidate();
- isMoving = true;
- } else {
- isMoving = false;
- }
- super.computeScroll();
- }
- /** 开打界面 */
- public void show(){
- if(!isShow && !isMoving){
- LoginView.this.startMoveAnim(-viewHeight, viewHeight, mDuration);
- isShow = true;
- Log.d("isShow", "true");
- changed();
- }
- }
- /** 关闭界面 */
- public void dismiss(){
- if(isShow && !isMoving){
- LoginView.this.startMoveAnim(0, -viewHeight, mDuration);
- isShow = false;
- Log.d("isShow", "false");
- changed();
- }
- }
- /** 是否打开 */
- public boolean isShow(){
- return isShow;
- }
- /** 获取是否可以拖动*/
- public boolean isSlidingEnabled() {
- return mEnabled;
- }
- /** 设置是否可以拖动*/
- public void setSlidingEnabled(boolean enabled) {
- mEnabled = enabled;
- }
- /**
- * 设置监听接口,实现遮罩层效果
- */
- public void setOnStatusListener(onStatusListener listener){
- this.statusListener = listener;
- }
- public void setOutsideTouchable(boolean touchable) {
- mOutsideTouchable = touchable;
- }
- /**
- * 显示状态发生改变时候执行回调借口
- */
- public void changed(){
- if(statusListener != null){
- if(isShow){
- statusListener.onShow();
- }else{
- statusListener.onDismiss();
- }
- }
- }
- /** 监听接口*/
- public onStatusListener statusListener;
- /**
- * 监听接口,来在主界面监听界面变化状态
- */
- public interface onStatusListener{
- /** 开打状态 */
- public void onShow();
- /** 关闭状态 */
- public void onDismiss();
- }
- @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);
- }
- }
版权声明:本文为博主原创文章,未经博主允许不得转载。
Android 仿 窗帘效果 和 登录界面拖动效果 (Scroller类的应用) 附 2个DEMO及源码的更多相关文章
- Android 仿 窗帘效果 和 登录界面拖动效果 (Scroller类的应用) 附 2个DEMO及源代码
在android学习中,动作交互是软件中重要的一部分.当中的Scroller就是提供了拖动效果的类,在网上.比方说一些Launcher实现滑屏都能够通过这个类去实现.以下要说的就是上次Scroller ...
- Android 界面滑动实现---Scroller类 从源码和开发文档中学习(让你的布局动起来)
在android学习中,动作交互是软件中重要的一部分,其中的Scroller就是提供了拖动效果的类,在网上,比如说一些Launcher实现滑屏都可以通过这个类去实现.. 例子相关博文:Androi ...
- 仿知乎app登录界面(Material Design设计框架拿来就用的TexnInputLayout)
在我脑子里还没有Material Design这种概念,就我个人而言,PC端应用扁平化设计必须成为首选,手当其冲的两款即时通讯旺旺和QQ早就完成UI扁平化的更新,然而客户端扁平化的设计本身就存在天生的 ...
- Android studio 开发一个用户登录界面
Android studio 开发一个用户登录界面 activity_main.xml <?xml version="1.0" encoding="utf-8&qu ...
- Android开发实例之miniTwitter登录界面的实现
原文: http://www.jizhuomi.com/android/example/134.html 本文要演示的Android开发实例是如何完成一个Android中的miniTwitter登录界 ...
- Android 仿 新闻阅读器 菜单弹出效果(附源码DEMO)
这一系列博文都是:(android高仿系列)今日头条 --新闻阅读器 (一) 开发中碰到问题之后实现的,觉得可能有的开发者用的到或则希望独立成一个小功能DEMO,所以就放出来这么一个DEMO. 原本觉 ...
- Android 比较好看的注册登录界面
各位看官姥爷: 对于一款android手机app而言,美观的界面使得用户有好的使用体验,而一款好看的注册登录界面也会给用户好的用户体验,那么话不多说,直接上代码 首先是一款简单的界面展示 1.登陆界面 ...
- Android中滑屏实现----触摸滑屏以及Scroller类详解 .
转:http://blog.csdn.net/qinjuning/article/details/7419207 知识点一: 关于scrollTo()和scrollBy()以及偏移坐标的设置/取值问 ...
- 商城项目实战 | 1.1 Android 仿京东商城底部布局的选择效果 —— Selector 选择器的实现
前言 本文为菜鸟窝作者刘婷的连载."商城项目实战"系列来聊聊仿"京东淘宝的购物商城"如何实现. 京东商城的底部布局的选择效果看上去很复杂,其实很简单,这主要是要 ...
随机推荐
- Python中函数式使用
对于链表来讲,有三个内置函数非常有用: filter(),map() 以及 reduce(). filter(function, sequence) 返回一个 sequence(序列),包括了给定序列 ...
- ZOJ 2794 Just Pour the Water 【矩阵快速幂】
给你n个杯子,每次有特定的到水规则,倒m次请问最后每个被子里还有多少水 我们很容易发现每次变化的规则相同,那么可以set 一个矩阵存放 然后多次倒水就相当于矩阵相乘,在m 范围达到(1<= M ...
- WeCenter 社交化问答社区程序 | WeCenter 是一款知识型的社交化问答社区程序,专注于社区内容的整理、归类、检索和再发行
WeCenter 社交化问答社区程序 | WeCenter 是一款知识型的社交化问答社区程序,专注于社区内容的整理.归类.检索和再发行 为什么选择 WeCenter 程序? 让您的社区更智能地运作,强 ...
- hdu 1528 Card Game Cheater ( 二分图匹配 )
题目:点击打开链接 题意:两个人纸牌游戏,牌大的人得分.牌大:2 < 3 < 4 < 5 < 6 < 7 < 8 < 9 < T < J < ...
- win7删除桌面文件需要刷新才会消失(2种解决方法)
有没有遇到过这种情况,删除桌面文件没有效果,要点右键的刷新删除过的文件才会在桌面上消失!解决方法有两种: 第一种方法 点击"开始→运行",在对话框中输入"regedit& ...
- Acitivity创建与配置
•Activity的创建和配置 –Activity提供了和用户交互的可视化界面.创建一个Activity一般是继承Activity(当然也可以继承ListActivity.MapActivity等), ...
- Nginx 因 Selinux 服务导致无法远程訪问
文章来源:http://blog.csdn.net/johnnycode/article/details/41947581 2014-12-16日 昨天晚上处理好的网络訪问连接.早晨又訪问不到了. 现 ...
- 【译】在Asp.Net中操作PDF - iTextSharp - 绘制矢量图
原文 [译]在Asp.Net中操作PDF - iTextSharp - 绘制矢量图 在上一篇iTextSharp文章中讲述了如何将现有的图片插入PDF中并对其进行操作.但有时,你需要在PDF中绘制不依 ...
- Introduction to the Build Lifecycle
Introduction to the Build Lifecycle Table Of Contents Build Lifecycle Basics Setting Up Your Project ...
- android用户界面之ScrollView教程实例汇总
--------------------------汇总不容易啊------------------------------- 一.ScrollView基础知识 1.Android中ScrollVie ...