Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix root background not apply #129

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,20 @@
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Rect;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.support.v4.view.ViewCompat;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

Expand Down Expand Up @@ -460,11 +465,45 @@ public void attachToActivity(Activity activity) {

ViewGroup decor = (ViewGroup) activity.getWindow().getDecorView();
ViewGroup decorChild = (ViewGroup) decor.getChildAt(0);
decorChild.setBackgroundResource(background);
Drawable d = decorChild.getBackground();
// no background or background is TRANSPARENT,use 'windowBackground' to background
if (d == null || (d instanceof ColorDrawable && isTransparent((ColorDrawable) d))) {
decorChild.setBackgroundResource(background);
}
decor.removeView(decorChild);
addView(decorChild);
setContentView(decorChild);
decor.addView(this);

FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) decorChild.getLayoutParams();
decorChild.setLayoutParams(new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
setLayoutParams(lp);

decor.addView(this,0);
}

private boolean isTransparent(ColorDrawable drawable) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
return drawable.getColor() == Color.TRANSPARENT;
} else {
try {
int color = 0;
final Field stateField = drawable.getClass().getDeclaredField("mState");
stateField.setAccessible(true);
final Object state = stateField.get(drawable);

final Field useColorField = state.getClass().getDeclaredField("mUseColor");
useColorField.setAccessible(true);
useColorField.setInt(state, color);

final Field baseColorField = state.getClass().getDeclaredField(
"mBaseColor");
baseColorField.setAccessible(true);
baseColorField.setInt(state, color);
return color == Color.TRANSPARENT;
} catch (Exception e) {
return false;
}
}
}

@Override
Expand Down Expand Up @@ -549,7 +588,7 @@ public void onViewPositionChanged(View changedView, int left, int top, int dx, i
if (mScrollPercent >= 1) {
if (!mActivity.isFinishing()) {
mActivity.finish();
mActivity.overridePendingTransition(0, 0);
mActivity.overridePendingTransition(0, 0);
}
}
}
Expand Down