最近感觉有一个Activity关闭的效果挺不错的,就是手势滑动就可以关闭当前Activity,于是就想写一篇博客和大家一起分享下!废话不多说,老规矩,还先上效果图,更直观!
项目地址:https://github.com/xinyitiandi/SlidingFinishDemo
上代码:
1.第一个Activity:
package com.ekeguan.slIDingfinishdemo;import androID.content.Intent;import androID.os.Bundle;import androID.support.v7.app.AppCompatActivity;import androID.vIEw.VIEw;import androID.Widget.button;public class MainActivity extends AppCompatActivity implements VIEw.OnClickListener { private button button; @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_main); initVIEw(); initEventListener(); } private voID initVIEw() { button = (button) findVIEwByID(R.ID.button); } private voID initEventListener() { button.setonClickListener(this); } @OverrIDe public voID onClick(VIEw vIEw) { switch(vIEw.getID()) { case R.ID.button: startActivity(new Intent(MainActivity.this,SecondActivity.class)); break; default: break; } }}
布局文件:
<?xml version="1.0" enCoding="utf-8"?><androID.support.constraint.ConstraintLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" xmlns:app="http://schemas.androID.com/apk/res-auto" xmlns:tools="http://schemas.androID.com/tools" androID:layout_wIDth="match_parent" androID:layout_height="match_parent" tools:context="com.ekeguan.slIDingfinishdemo.MainActivity"> <button androID:ID="@+ID/button" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:text="跳转到第二个Activity" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintleft_toleftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constrainttop_totopOf="parent" /></androID.support.constraint.ConstraintLayout>
2.第二个Activity,即要跳转的目标Activity
package com.ekeguan.slIDingfinishdemo;import androID.os.Bundle;import androID.support.v7.app.AppCompatActivity;public class SecondActivity extends AppCompatActivity { private SildingFinishLayout mSildingFinishLayout; @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_second); initVIEw(); initEventListener(); } private voID initVIEw() { mSildingFinishLayout = (SildingFinishLayout) findVIEwByID(R.ID.sildingFinishLayout); mSildingFinishLayout.settouchVIEw(mSildingFinishLayout); } private voID initEventListener() { mSildingFinishLayout .setonSildingFinishListener(new SildingFinishLayout.OnSildingFinishListener() { @OverrIDe public voID onSildingFinish() { finish(); } }); }}
布局文件:
<?xml version="1.0" enCoding="utf-8"?><com.ekeguan.slIDingfinishdemo.SildingFinishLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" androID:ID="@+ID/sildingFinishLayout" androID:layout_wIDth="match_parent" androID:layout_height="match_parent" androID:gravity="center"> <FrameLayout androID:layout_wIDth="match_parent" androID:layout_height="match_parent" androID:background="#f0f0f0"> <TextVIEw androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:text="向右滑动关闭当前Activity" androID:layout_gravity="center" androID:textcolor="#000"/> </FrameLayout></com.ekeguan.slIDingfinishdemo.SildingFinishLayout>
注意:这里用到了一个自定义的布局SildingFinishLayout ,关于这个布局的代码,我一会在下面贴出,大家不用着急。需要注意的是想要滑动关闭的Activity的布局文件最外层要被SildingFinishLayout 布局包裹,被SildingFinishLayout 包裹的里面的布局设置背景色,如FrameLayout,我在这里设置了背景色为“#f0f0f0”,字体要设置字体颜色,如TextVIEw,我在这里设置了“#000”
为了达到理想的效果,在AndroIDMainfest.xml文件里面要给想要手势滑动的Activity添加上一个透明的主题,如:
<activity androID:name=".SecondActivity" androID:screenorIEntation="portrait" androID:theme="@style/theme.AppCompat.Translucent"></activity>
主题:
<style name="theme.AppCompat.Translucent"> <item name="androID:windowBackground">@androID:color/transparent</item> <item name="androID:colorBackgroundCacheHint">@null</item> <item name="androID:windowIsTranslucent">true</item> <item name="windowActionbar">false</item> <item name="windowNoTitle">true</item> </style>
好了,到这里差不多了,下面贴上自定义布局SildingFinishLayout的代码:
package com.ekeguan.slIDingfinishdemo;import androID.content.Context;import androID.util.AttributeSet;import androID.vIEw.MotionEvent;import androID.vIEw.VIEw;import androID.vIEw.VIEw.OntouchListener;import androID.vIEw.VIEwConfiguration;import androID.vIEw.VIEwGroup;import androID.Widget.AbsListVIEw;import androID.Widget.relativeLayout;import androID.Widget.ScrollVIEw;import androID.Widget.Scroller;/** * 自定义可以滑动的relativeLayout,类似于IOS的滑动删除页面效果,当我们要使用 * 此功能的时候,需要将该Activity的顶层布局设置为SildingFinishLayout, * 然后需要调用settouchVIEw()方法来设置需要滑动的VIEw * * @author xiaanming * * @blog http://blog.csdn.net/xiaanming * */public class SildingFinishLayout extends relativeLayout implements OntouchListener { /** * SildingFinishLayout布局的父布局 */ private VIEwGroup mParentVIEw; /** * 处理滑动逻辑的VIEw */ private VIEw touchVIEw; /** * 滑动的最小距离 */ private int mtouchSlop; /** * 按下点的X坐标 */ private int downX; /** * 按下点的Y坐标 */ private int downY; /** * 临时存储X坐标 */ private int tempX; /** * 滑动类 */ private Scroller mScroller; /** * SildingFinishLayout的宽度 */ private int vIEwWIDth; /** * 记录是否正在滑动 */ private boolean isSilding; private OnSildingFinishListener onSildingFinishListener; private boolean isFinish; public SildingFinishLayout(Context context,AttributeSet attrs) { this(context,attrs,0); } public SildingFinishLayout(Context context,AttributeSet attrs,int defStyle) { super(context,defStyle); mtouchSlop = VIEwConfiguration.get(context).getScaledtouchSlop(); mScroller = new Scroller(context); } @OverrIDe protected voID onLayout(boolean changed,int l,int t,int r,int b) { super.onLayout(changed,l,t,r,b); if (changed) { // 获取SildingFinishLayout所在布局的父布局 mParentVIEw = (VIEwGroup) this.getParent(); vIEwWIDth = this.getWIDth(); } } /** * 设置OnSildingFinishListener,在onSildingFinish()方法中finish Activity * * @param onSildingFinishListener */ public voID setonSildingFinishListener( OnSildingFinishListener onSildingFinishListener) { this.onSildingFinishListener = onSildingFinishListener; } /** * 设置touch的VIEw * * @param touchVIEw */ public voID settouchVIEw(VIEw touchVIEw) { this.touchVIEw = touchVIEw; touchVIEw.setontouchListener(this); } public VIEw gettouchVIEw() { return touchVIEw; } /** * 滚动出界面 */ private voID scrollRight() { final int delta = (vIEwWIDth + mParentVIEw.getScrollX()); // 调用startScroll方法来设置一些滚动的参数,我们在computeScroll()方法中调用scrollTo来滚动item mScroller.startScroll(mParentVIEw.getScrollX(),-delta + 1,Math.abs(delta)); postInvalIDate(); } /** * 滚动到起始位置 */ private voID scrollOrigin() { int delta = mParentVIEw.getScrollX(); mScroller.startScroll(mParentVIEw.getScrollX(),-delta,Math.abs(delta)); postInvalIDate(); } /** * touch的VIEw是否是AbsListVIEw, 例如ListVIEw,GrIDVIEw等其子类 * * @return */ private boolean istouchOnAbsListVIEw() { return touchVIEw instanceof AbsListVIEw ? true : false; } /** * touch的vIEw是否是ScrollVIEw或者其子类 * * @return */ private boolean istouchOnScrollVIEw() { return touchVIEw instanceof ScrollVIEw ? true : false; } @OverrIDe public boolean ontouch(VIEw v,MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: downX = tempX = (int) event.getRawX(); downY = (int) event.getRawY(); break; case MotionEvent.ACTION_MOVE: int moveX = (int) event.getRawX(); int deltaX = tempX - moveX; tempX = moveX; if (Math.abs(moveX - downX) > mtouchSlop && Math.abs((int) event.getRawY() - downY) < mtouchSlop) { isSilding = true; // 若touchVIEw是AbsListVIEw, // 则当手指滑动,取消item的点击事件,不然我们滑动也伴随着item点击事件的发生 if (istouchOnAbsListVIEw()) { MotionEvent cancelEvent = MotionEvent.obtain(event); cancelEvent .setAction(MotionEvent.ACTION_CANCEL | (event.getActionIndex() << MotionEvent.ACTION_POINTER_INDEX_SHIFT)); v.ontouchEvent(cancelEvent); } } if (moveX - downX >= 0 && isSilding) { mParentVIEw.scrollBy(deltaX,0); // 屏蔽在滑动过程中ListVIEw ScrollVIEw等自己的滑动事件 if (istouchOnScrollVIEw() || istouchOnAbsListVIEw()) { return true; } } break; case MotionEvent.ACTION_UP: isSilding = false; if (mParentVIEw.getScrollX() <= -vIEwWIDth / 2) { isFinish = true; scrollRight(); } else { scrollOrigin(); isFinish = false; } break; } // 假如touch的vIEw是AbsListVIEw或者ScrollVIEw 我们处理完上面自己的逻辑之后 // 再交给AbsListVIEw,ScrollVIEw自己处理其自己的逻辑 if (istouchOnScrollVIEw() || istouchOnAbsListVIEw()) { return v.ontouchEvent(event); } // 其他的情况直接返回true return true; } @OverrIDe public voID computeScroll() { // 调用startScroll的时候scroller.computeScrollOffset()返回true, if (mScroller.computeScrollOffset()) { mParentVIEw.scrollTo(mScroller.getCurrX(),mScroller.getCurrY()); postInvalIDate(); if (mScroller.isFinished()) { if (onSildingFinishListener != null && isFinish) { onSildingFinishListener.onSildingFinish(); } } } } public interface OnSildingFinishListener { public voID onSildingFinish(); }}
最后项目地址:https://github.com/xinyitiandi/SlidingFinishDemo
以上所述是小编给大家介绍的AndroID中Activity滑动关闭的效果,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对编程小技巧网站的支持!
总结以上是内存溢出为你收集整理的Android中Activity滑动关闭的效果全部内容,希望文章能够帮你解决Android中Activity滑动关闭的效果所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)