标签:
在android学习中,动作交互是软件中重要的一部分,其中的Scroller就是提供了拖动效果的类,在网上,比如说一些Launcher实现滑屏都可以通过这个类去实现。下面要说的就是上次Scroller类学习的后的实践了。
如果你还不了解Scroller类,那请先点击:Android 界面滑动实现---Scroller类 从源码和开发文档中学习(让你的布局动起来)
了解之后再阅读以下内容,你会发现原来实现起来很简单。
之前说到过,在广泛使用的侧边滑动导航开源库 --SlidingLayer其实就是使用到了Scroller类进行的实现,(SlidingLayer下载地址:GITHUB ),而是这个库的实现过程中使用到的---Scroller类,我们可以使用这个库实现以下我要达到的效果,可是这样拿来就用,对于初学者提升不大,所以我决定直接去使用Scroller类去实现:
1)窗帘展开和关闭效果
2)登录界面拖动效果(有点类似PopupWindow,可是带上了拖拽效果)。
通过这2个例子,你就大概知道了Scroller类的基本使用情况,可以自己去写一些类似的效果了。
先上图,在上主要代码,最后上DEMO源码。
申明下:DEMO中的资源文件是在网上下载的2个应用中,发现效果不错和可以进一步完善(比如窗帘效果,原本是不带推拽效果),提取了应用的资源文件去自己实现的,目的是为了更好的达到展示效果。
代码中都带上了注释和说明,以便更好的了解实现过程。可能有的地方优化做的不足,望大家见谅。
效果图:
1)窗帘 效果
用途:可以使用于广告墙,公告栏等地方
说明:点击开关可以实现展开关闭功能,也可以通过推拽开关实现展开关闭效果,动画中加入了反弹效果,更加真实。
2)登录窗体 效果
用途:可以使用在登录时候的登录方式选择,菜单选项等,有点类似于带拖拽效果的PopupWindow
说明:可以登录按钮展开关闭登录窗体,也可以通过推拽进行关闭。
注:这里的点击窗体之外消失是通过回调接口实现,这里没有列出,可以下载源码查看
学习了Scroller类,大概的你也知道核心代码会是哪些内容,下面列举下
核心代码:
窗帘效果:
- public class CurtainView extends RelativeLayout implements OnTouchListener{
- private static String TAG = "CurtainView";
- private Context mContext;
-
- private Scroller mScroller;
-
- private int mScreenHeigh = 0;
-
- private int mScreenWidth = 0;
-
- private int downY = 0;
-
- private int moveY = 0;
-
- private int scrollY = 0;
-
- 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 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() {
-
- curtainHeigh = img_curtain_ad.getHeight();
- Log.d(TAG, "curtainHeigh= " + curtainHeigh);
- CurtainView.this.scrollTo(0, curtainHeigh);
-
- }
- });
- img_curtain_rope.setOnTouchListener(this);
- }
-
-
- public void startMoveAnim(int startY, int dy, int duration) {
- isMove = true;
- mScroller.startScroll(0, startY, 0, dy, duration);
- invalidate();
- }
-
- @Override
- protected void onLayout(boolean changed, int l, int t, int r, int b) {
-
- super.onLayout(changed, l, t, r, b);
- }
-
- @Override
- public void computeScroll() {
-
- if (mScroller.computeScrollOffset()) {
- scrollTo(mScroller.getCurrX(), mScroller.getCurrY());
-
- postInvalidate();
- isMove = true;
- } else {
- isMove = false;
- }
- super.computeScroll();
- }
-
- @Override
- public boolean onTouch(View v, MotionEvent event) {
-
- 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;
- }
-
- 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 {
-
- private Scroller mScroller;
-
- private int mScreenHeigh = 0;
-
- private int mScreenWidth = 0;
-
- private int downY = 0;
-
- private int moveY = 0;
-
- private int scrollY = 0;
-
- 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);
- addView(view, params);
-
- this.setBackgroundColor(Color.argb(0, 0, 0, 0));
- view.post(new Runnable() {
-
- @Override
- public void run() {
-
- 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) {
-
- dismiss();
- }
- });
- }
-
- @Override
- public boolean onInterceptTouchEvent(MotionEvent ev) {
- if(!mEnabled){
- return false;
- }
- return super.onInterceptTouchEvent(ev);
- }
-
- @Override
- public boolean onTouchEvent(MotionEvent event) {
-
- 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);
- }
-
-
- public void startMoveAnim(int startY, int dy, int duration) {
- isMoving = true;
- mScroller.startScroll(0, startY, 0, dy, duration);
- invalidate();
- }
-
- @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) {
-
- super.onLayout(changed, l, t, r, b);
- }
- }
其实代码大同小异,了解后你就可以举一反三,去自己的VIEW中实现自己想要的效果。
版权声明:本文为博主原创文章,未经博主允许不得转载。
Android 仿 窗帘效果 和 登录界面拖动效果 (Scroller类的应用) 附 2个DEMO及源码
标签:
原文地址:http://www.cnblogs.com/Free-Thinker/p/4785004.html