欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android仿微信activity滑动关闭效果

程序员文章站 2023-12-21 20:56:10
android仿微信activity滑动关闭功能 1.利用具体利用v4包下的slidingpanelayout实现透明的activity,代码如下: bas...

android仿微信activity滑动关闭功能

1.利用具体利用v4包下的slidingpanelayout实现透明的activity,代码如下:

baseactivity:
public class baseslidecloseactivity extends appcompatactivity implements
  slidingpanelayout.panelslidelistener {

 @override
 protected void oncreate(bundle savedinstancestate) {
  initslidebackclose();
  super.oncreate(savedinstancestate);
 }

 private void initslidebackclose() {
  if (issupportswipeback()) {
   slidingpanelayout slidingpanelayout = new slidingpanelayout(this);
   // 通过反射改变moverhangsize的值为0,
   // 这个moverhangsize值为菜单到右边屏幕的最短距离,
   // 默认是32dp,现在给它改成0
   try {
    field overhangsize = slidingpanelayout.class.getdeclaredfield("moverhangsize");
    overhangsize.setaccessible(true);
    overhangsize.set(slidingpanelayout, 0);
   } catch (exception e) {
    e.printstacktrace();
   }
   slidingpanelayout.setpanelslidelistener(this);
   slidingpanelayout.setsliderfadecolor(getresources()
     .getcolor(android.r.color.transparent));

   // 左侧的透明视图
   view leftview = new view(this);
   leftview.setlayoutparams(new viewgroup.layoutparams(viewgroup.layoutparams.match_parent, viewgroup.layoutparams.match_parent));
   slidingpanelayout.addview(leftview, 0);

   viewgroup decorview = (viewgroup) getwindow().getdecorview();


   // 右侧的内容视图
   viewgroup decorchild = (viewgroup) decorview.getchildat(0);
   decorchild.setbackgroundcolor(getresources()
     .getcolor(android.r.color.white));
   decorview.removeview(decorchild);
   decorview.addview(slidingpanelayout);

   // 为 slidingpanelayout 添加内容视图
   slidingpanelayout.addview(decorchild, 1);
  }
 }

 protected boolean issupportswipeback() {
  return true;
 }

 @override
 public void onpanelslide(view panel, float slideoffset) {

 }

 @override
 public void onpanelopened(view panel) {
  finish();
 }

 @override
 public void onpanelclosed(view panel) {

 }
}

activity 透明style:

<resources xmlns:tools="http://schemas.android.com/tools">

 <!-- base application theme. -->
 <style name="apptheme" parent="theme.appcompat.light.darkactionbar">
  <!-- customize your theme here. -->
  <item name="colorprimary">@color/colorprimary</item>
  <item name="colorprimarydark">@color/colorprimarydark</item>
  <item name="coloraccent">@color/coloraccent</item>
 </style>

 <style name="diy.slideclose.transparent.theme" parent="apptheme">
  <item name="android:windowbackground">@android:color/transparent</item>
  <item name="android:windowistranslucent">true</item>
  <item name="android:windowanimationstyle">@style/diy.animation.slidingback</item>
  <item name="android:actionbarstyle">@style/diyactionbar.custom</item>
 </style>

 <style name="diyactionbar.custom" parent="@style/widget.appcompat.light.actionbar.solid.inverse">
  <item name="displayoptions">showcustom</item>
  <item name="android:background">@android:color/transparent</item>
  <item name="background">@android:color/transparent</item>
  <item name="android:displayoptions" tools:ignore="newapi">showcustom</item>
  <item name="android:height">?actionbarsize</item>
 </style>

 <style name="diy.animation.slidingback" parent="@android:style/animation.activity">
  <item name="android:activityopenenteranimation">@anim/slide_in_right</item>
  <item name="android:activityopenexitanimation">@anim/slide_out_right</item>
  <item name="android:activitycloseenteranimation">@anim/slide_in_right</item>
  <item name="android:activitycloseexitanimation">@anim/slide_out_right</item>
  <item name="android:wallpaperopenenteranimation">@anim/slide_in_right</item>
  <item name="android:wallpaperopenexitanimation">@anim/slide_out_right</item>
  <item name="android:wallpapercloseenteranimation">@anim/slide_in_right</item>
  <item name="android:wallpapercloseexitanimation">@anim/slide_out_right</item>
  <item name="android:wallpaperintraopenenteranimation">@anim/slide_in_right</item>
  <item name="android:wallpaperintraopenexitanimation">@anim/slide_out_right</item>
  <item name="android:wallpaperintracloseenteranimation">@anim/slide_in_right</item>
  <item name="android:wallpaperintracloseexitanimation">@anim/slide_out_right</item>
 </style>
 
</resources>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

上一篇:

下一篇: