项目中用到的滑动关闭页面,是基于SlidingMenu做的
下面贴出源码,并且讲解以下
/** * 向右滑动关闭Activity * * 添加关闭动画 */ public class SwipeBackView extends SlidingMenu { public Context context; public SwipeBackView(Context context) { super(context); this.context = context; init(); } private void init(){ setMode(LEFT); setTouchModeAbove(TOUCHMODE_MARGIN);//触摸边缘触发 setOnOpenedListener(new SlidingMenu.OnOpenedListener() { @Override public void onOpened() { TranslateAnimation translateAnimation = new TranslateAnimation(getContent().getX(), BaseApplication.WINDOW_WIDTH,0,0); translateAnimation.setDuration(300); translateAnimation.setFillAfter(false); ((Activity)context).finish(); startAnimation(translateAnimation); } }); } }
setOnOpenedListener
是抽屉打开鉴定,抽屉在完全打开时候调用
public void activityEnterAnimation(View contentView){ this.contentView = contentView; contentView.startAnimation(AnimationUtils.loadAnimation(this,R.anim.activity_start)); } /** * 关闭时过场动画 * @param contentView */ public void activityBackAnimation(){ Animation translateAnimation = AnimationUtils.loadAnimation(this,R.anim.activity_close); this.contentView.startAnimation(translateAnimation); translateAnimation.setFillAfter(true); translateAnimation.setAnimationListener(new Animation.AnimationListener() { @Override public void onAnimationStart(Animation animation) { } @Override public void onAnimationEnd(Animation animation) { BaseActivity.this.finish(); } @Override public void onAnimationRepeat(Animation animation) { } });在BaseActivity中为Activity种的contentView设置一个过场动画
public class WebActivity extends BaseActivity { private SwipeBackView swipeBackView; private BaseWebView baseWebView; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); baseWebView = new BaseWebView(this); swipeBackView = new SwipeBackView(this); swipeBackView.setContent(baseWebView);
//如果不为抽屉设置MenuView的话,SlidingMenu会自动补上一个空的View setContentView(swipeBackView); activityEnterAnimation(baseWebView); baseWebView.loadUrl("https://www.baidu.com/"); } @Override public void onBackPressed() { // activityBackAnimation(); if(baseWebView.getScrollWebView().canGoBack()){ baseWebView.getScrollWebView().goBack(); }else{ activityBackAnimation(); } } }
另外最重要的一点就是在Manifest中设置Activity的样式为完全透明
android:theme="@android:style/Theme.Translucent.NoTitleBar"这句在 AppCompatActivity是调用不到的哦