Skip to content

Commit

Permalink
fix: 修复视差动画重影问题
Browse files Browse the repository at this point in the history
  • Loading branch information
goweii committed Dec 18, 2021
1 parent be72a3a commit c60e25c
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 15 deletions.
4 changes: 3 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
buildscript {
repositories {
google()
mavenCentral()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:4.0.0'
classpath "com.android.tools.build:gradle:4.1.1"
}
}

allprojects {
repositories {
google()
mavenCentral()
jcenter()
maven { url "https://jitpack.io" }
}
Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-6.1.1-bin.zip
distributionUrl=https://services.gradle.org/distributions/gradle-6.5-all.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
50 changes: 37 additions & 13 deletions swipeback/src/main/java/per/goweii/swipeback/SwipeBackNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,9 @@
import android.view.Window;
import android.widget.FrameLayout;

import androidx.annotation.ColorInt;
import androidx.annotation.FloatRange;
import androidx.annotation.IntRange;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.Px;

import per.goweii.swipeback.utils.ActivityTranslucentConverter;

Expand All @@ -26,7 +23,6 @@ class SwipeBackNode {
private SwipeBackLayout mLayout = null;

private SwipeBackTransformer mTransformer = null;
private SwipeBackNode mPreviousNode = null;

SwipeBackNode(@NonNull Activity activity) {
mActivity = activity;
Expand All @@ -50,7 +46,6 @@ void inject() {
TypedArray typedArray = mActivity.getTheme().obtainStyledAttributes(new int[]{android.R.attr.windowBackground});
int background = typedArray.getResourceId(0, 0);
typedArray.recycle();
window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
decorView.setBackground(new ColorDrawable(Color.TRANSPARENT));
decorChildView.setBackgroundResource(background);
SwipeBackLayout swipeBackLayout = new SwipeBackLayout(mActivity);
Expand Down Expand Up @@ -90,11 +85,16 @@ private SwipeBackNode findPreviousNode() {
}

@Nullable
private View getPreviousView() {
if (mPreviousNode == null) return null;
Window window = mPreviousNode.mActivity.getWindow();
private FrameLayout getDecorView() {
Window window = mActivity.getWindow();
if (window == null) return null;
FrameLayout decorView = (FrameLayout) window.getDecorView();
return (FrameLayout) window.getDecorView();
}

@Nullable
private View getDecorChild0() {
FrameLayout decorView = getDecorView();
if (decorView == null) return null;
if (decorView.getChildCount() == 0) return null;
return decorView.getChildAt(0);
}
Expand All @@ -113,6 +113,8 @@ public int hashCode() {
}

private class SwipeBackListener implements SwipeBackLayout.SwipeBackListener {
private SwipeBackNode mPreviousNode = null;

@Override
public void onBeforeSwipe(@FloatRange(from = 0F, to = 1F) float swipeFraction, @NonNull SwipeBackDirection swipeDirection) {
configLayout();
Expand All @@ -126,24 +128,46 @@ public void onStartSwipe(@FloatRange(from = 0F, to = 1F) float swipeFraction, @N
mTranslucentConverter.toTranslucent();
}
if (mLayout != null && mTransformer != null) {
View previousDecorView = null;
View previousDecorChild0 = null;
if (mPreviousNode != null) {
previousDecorView = mPreviousNode.getDecorView();
previousDecorChild0 = mPreviousNode.getDecorChild0();
}
if (previousDecorView != null) {
previousDecorView.setBackground(new ColorDrawable(Color.BLACK));
}
if (swipeFraction == 0) {
mTransformer.initialize(mLayout, getPreviousView());
mTransformer.initialize(mLayout, previousDecorChild0);
}
mTransformer.transform(mLayout, getPreviousView(), swipeFraction, swipeDirection);
mTransformer.transform(mLayout, previousDecorChild0, swipeFraction, swipeDirection);
}
}

@Override
public void onSwiping(@FloatRange(from = 0F, to = 1F) float swipeFraction, @NonNull SwipeBackDirection swipeDirection) {
if (mLayout != null && mTransformer != null) {
mTransformer.transform(mLayout, getPreviousView(), swipeFraction, swipeDirection);
View previousDecorChild0 = null;
if (mPreviousNode != null) {
previousDecorChild0 = mPreviousNode.getDecorChild0();
}
mTransformer.transform(mLayout, previousDecorChild0, swipeFraction, swipeDirection);
}
}

@Override
public void onEndSwipe(@FloatRange(from = 0F, to = 1F) float swipeFraction, @NonNull SwipeBackDirection swipeDirection) {
if (mLayout != null && mTransformer != null) {
mTransformer.restore(mLayout, getPreviousView());
View previousDecorView = null;
View previousDecorChild0 = null;
if (mPreviousNode != null) {
previousDecorView = mPreviousNode.getDecorView();
previousDecorChild0 = mPreviousNode.getDecorChild0();
}
mTransformer.restore(mLayout, previousDecorChild0);
if (previousDecorView != null) {
previousDecorView.setBackground(new ColorDrawable(Color.TRANSPARENT));
}
}
if (swipeFraction != 1) {
if (!mThemeTranslucent) {
Expand Down

0 comments on commit c60e25c

Please sign in to comment.