A better way to color status bar
This commit is contained in:
1 parent
e0713bdfec
commit
586806dd8e
7 files changed
+208
-53
No files matched your search
@@ -26,14 +26,17 @@ import android.view.Gravity;
|
||||
import com.hippo.content.VectorContext;
|
||||
import com.hippo.ehviewer.R;
|
||||
import com.hippo.ehviewer.ui.scene.GalleryListScene;
|
||||
import com.hippo.ehviewer.widget.StatusBarLayout;
|
||||
import com.hippo.scene.StageActivity;
|
||||
import com.hippo.scene.StageLayout;
|
||||
|
||||
public class ContentActivity extends StageActivity {
|
||||
public class ContentActivity extends StageActivity
|
||||
implements StatusBarLayout.OnGetFitPaddingBottomListener {
|
||||
|
||||
private Resources mResources;
|
||||
|
||||
private DrawerLayout mDrawerLayout;
|
||||
private StatusBarLayout mStatusBarLayout;
|
||||
private StageLayout mContentLayout;
|
||||
|
||||
@Override
|
||||
@@ -43,16 +46,20 @@ public class ContentActivity extends StageActivity {
|
||||
|
||||
mResources = getResources();
|
||||
|
||||
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
|
||||
mContentLayout = (StageLayout) mDrawerLayout.findViewById(R.id.stage);
|
||||
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
|
||||
mStatusBarLayout = (StatusBarLayout) mDrawerLayout.findViewById(R.id.status_bar_layout);
|
||||
mContentLayout = (StageLayout) mStatusBarLayout.findViewById(R.id.stage);
|
||||
|
||||
mDrawerLayout.setStatusBarBackground(R.color.theme_primary_dark);
|
||||
mStatusBarLayout.setOnGetFitPaddingBottomListener(this);
|
||||
|
||||
if (savedInstanceState == null) {
|
||||
startScene(GalleryListScene.class, null);
|
||||
}
|
||||
}
|
||||
|
||||
onCreateStageLayout(mContentLayout);
|
||||
@Override
|
||||
public int getFitPaddingBottom() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -73,4 +80,9 @@ public class ContentActivity extends StageActivity {
|
||||
mDrawerLayout.openDrawer(Gravity.LEFT);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onGetFitPaddingBottom(int b) {
|
||||
setFitPaddingBottom(b);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,158 @@
|
||||
/*
|
||||
* Copyright (C) 2015 Hippo Seven
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.hippo.ehviewer.widget;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.res.TypedArray;
|
||||
import android.graphics.Canvas;
|
||||
import android.graphics.Color;
|
||||
import android.graphics.Paint;
|
||||
import android.graphics.Rect;
|
||||
import android.os.Build;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.util.AttributeSet;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
|
||||
import com.hippo.ehviewer.R;
|
||||
|
||||
public class StatusBarLayout extends ViewGroup {
|
||||
|
||||
private int mFitPaddingTop = -1;
|
||||
private OnGetFitPaddingBottomListener mOnGetFitPaddingBottomListener;
|
||||
|
||||
private Paint mPaint;
|
||||
private int mStatusBarColor = Color.BLACK;
|
||||
|
||||
public StatusBarLayout(Context context) {
|
||||
super(context);
|
||||
init(context, null);
|
||||
}
|
||||
|
||||
public StatusBarLayout(Context context, AttributeSet attrs) {
|
||||
super(context, attrs);
|
||||
init(context, attrs);
|
||||
}
|
||||
|
||||
public StatusBarLayout(Context context, AttributeSet attrs, int defStyleAttr) {
|
||||
super(context, attrs, defStyleAttr);
|
||||
init(context, attrs);
|
||||
}
|
||||
|
||||
private void init(Context context, AttributeSet attrs) {
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
|
||||
setWillNotDraw(false);
|
||||
mPaint = new Paint();
|
||||
if (attrs != null) {
|
||||
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.StatusBarLayout);
|
||||
mPaint.setColor(a.getColor(R.styleable.StatusBarLayout_statusBarColor, Color.BLACK));
|
||||
a.recycle();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void setStatusBarColor(int color) {
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
|
||||
if (mStatusBarColor != color) {
|
||||
mStatusBarColor = color;
|
||||
mPaint.setColor(color);
|
||||
invalidate();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onDraw(Canvas canvas) {
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
|
||||
if (mFitPaddingTop > 0) {
|
||||
canvas.drawRect(0, 0, getWidth(), mFitPaddingTop, mPaint);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
|
||||
int count = getChildCount();
|
||||
|
||||
int maxHeight = 0;
|
||||
int maxWidth = 0;
|
||||
|
||||
// Find out how big everyone wants to be
|
||||
measureChildren(widthMeasureSpec, heightMeasureSpec);
|
||||
|
||||
// Find rightmost and bottom-most child
|
||||
for (int i = 0; i < count; i++) {
|
||||
View child = getChildAt(i);
|
||||
if (child.getVisibility() != GONE) {
|
||||
int childRight;
|
||||
int childBottom;
|
||||
|
||||
childRight = child.getMeasuredWidth();
|
||||
childBottom = child.getMeasuredHeight();
|
||||
|
||||
maxWidth = Math.max(maxWidth, childRight);
|
||||
maxHeight = Math.max(maxHeight, childBottom);
|
||||
}
|
||||
}
|
||||
|
||||
// Account for padding too
|
||||
maxWidth += getPaddingLeft() + getPaddingRight();
|
||||
maxHeight += getPaddingTop() + getPaddingBottom();
|
||||
|
||||
// Check against minimum height and width
|
||||
maxHeight = Math.max(maxHeight, getSuggestedMinimumHeight());
|
||||
maxWidth = Math.max(maxWidth, getSuggestedMinimumWidth());
|
||||
|
||||
setMeasuredDimension(resolveSizeAndState(maxWidth, widthMeasureSpec, 0),
|
||||
resolveSizeAndState(maxHeight, heightMeasureSpec, 0));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onLayout(boolean changed, int l, int t, int r, int b) {
|
||||
int paddingLeft = getPaddingLeft();
|
||||
int paddingTop = getPaddingTop();
|
||||
int size = getChildCount();
|
||||
for (int i = 0; i < size; i++) {
|
||||
View child = getChildAt(i);
|
||||
if (child.getVisibility() == View.GONE) {
|
||||
continue;
|
||||
}
|
||||
child.layout(paddingLeft, paddingTop, paddingLeft + child.getMeasuredWidth(), paddingTop + child.getMeasuredHeight());
|
||||
}
|
||||
}
|
||||
|
||||
public void setOnGetFitPaddingBottomListener(OnGetFitPaddingBottomListener listener) {
|
||||
mOnGetFitPaddingBottomListener = listener;
|
||||
}
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
@Override
|
||||
protected boolean fitSystemWindows(@NonNull Rect insets) {
|
||||
mFitPaddingTop = insets.top;
|
||||
|
||||
if (mOnGetFitPaddingBottomListener != null) {
|
||||
mOnGetFitPaddingBottomListener.onGetFitPaddingBottom(insets.bottom);
|
||||
}
|
||||
|
||||
insets.set(insets.left, insets.top, insets.right, 0);
|
||||
|
||||
return super.fitSystemWindows(insets);
|
||||
}
|
||||
|
||||
public interface OnGetFitPaddingBottomListener {
|
||||
void onGetFitPaddingBottom(int b);
|
||||
}
|
||||
}
|
||||
@@ -101,8 +101,8 @@ class SceneManager {
|
||||
scene.create(null);
|
||||
|
||||
// Update fit padding
|
||||
int fitPaddingBottom = getStageActivity().getStageLayout().getFitPaddingBottom();
|
||||
if (fitPaddingBottom != -1) {
|
||||
int fitPaddingBottom = getStageActivity().getFitPaddingBottom();
|
||||
if (fitPaddingBottom > 0) {
|
||||
scene.getFitPaddingBottom(fitPaddingBottom);
|
||||
}
|
||||
|
||||
|
||||
@@ -25,8 +25,7 @@ import android.view.View;
|
||||
import com.hippo.ehviewer.ui.AbsActionBarActivity;
|
||||
import com.hippo.util.IntIdGenerator;
|
||||
|
||||
public abstract class StageActivity extends AbsActionBarActivity
|
||||
implements StageLayout.OnGetFitPaddingBottomListener {
|
||||
public abstract class StageActivity extends AbsActionBarActivity {
|
||||
|
||||
private IntIdGenerator mActivityResultIdGenerator = IntIdGenerator.create();
|
||||
private SparseArray<Scene.ActivityResultListener> mActivityResultListenerMap =
|
||||
@@ -34,6 +33,8 @@ public abstract class StageActivity extends AbsActionBarActivity
|
||||
|
||||
private static SceneManager sSceneManager;
|
||||
|
||||
private int mFitPaddingBottom = -1;
|
||||
|
||||
static void setSceneManager(SceneManager sceneManager) {
|
||||
sSceneManager = sceneManager;
|
||||
}
|
||||
@@ -49,10 +50,6 @@ public abstract class StageActivity extends AbsActionBarActivity
|
||||
((SceneApplication) getApplication()).getSceneManager().setStageActivity(this);
|
||||
}
|
||||
|
||||
protected void onCreateStageLayout(StageLayout stageLayout) {
|
||||
stageLayout.setOnGetFitPaddingListener(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onDestroy() {
|
||||
super.onDestroy();
|
||||
@@ -60,10 +57,16 @@ public abstract class StageActivity extends AbsActionBarActivity
|
||||
((SceneApplication) getApplication()).getSceneManager().setStageActivity(null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onGetFitPaddingBottom(int b) {
|
||||
for (Scene scene : sSceneManager.getSceneStack()) {
|
||||
scene.getFitPaddingBottom(b);
|
||||
public int getFitPaddingBottom() {
|
||||
return mFitPaddingBottom;
|
||||
}
|
||||
|
||||
public void setFitPaddingBottom(int b) {
|
||||
if (mFitPaddingBottom != b) {
|
||||
mFitPaddingBottom = b;
|
||||
for (Scene scene : sSceneManager.getSceneStack()) {
|
||||
scene.getFitPaddingBottom(b);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -16,7 +16,6 @@
|
||||
package com.hippo.scene;
|
||||
|
||||
import android.content.Context;
|
||||
import android.graphics.Rect;
|
||||
import android.os.Parcelable;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.util.AttributeSet;
|
||||
@@ -25,10 +24,6 @@ import android.widget.FrameLayout;
|
||||
|
||||
public class StageLayout extends FrameLayout {
|
||||
|
||||
private OnGetFitPaddingBottomListener mOnGetFitPaddingBottomListener;
|
||||
|
||||
private int mFitPaddingBottom = -1;
|
||||
|
||||
public StageLayout(Context context) {
|
||||
super(context);
|
||||
}
|
||||
@@ -50,29 +45,4 @@ public class StageLayout extends FrameLayout {
|
||||
protected void dispatchRestoreInstanceState(@NonNull SparseArray<Parcelable> container) {
|
||||
// Do nothing
|
||||
}
|
||||
|
||||
public void setOnGetFitPaddingListener(OnGetFitPaddingBottomListener listener) {
|
||||
mOnGetFitPaddingBottomListener = listener;
|
||||
}
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
@Override
|
||||
protected boolean fitSystemWindows(@NonNull Rect insets) {
|
||||
mFitPaddingBottom = insets.bottom;
|
||||
if (mOnGetFitPaddingBottomListener != null) {
|
||||
mOnGetFitPaddingBottomListener.onGetFitPaddingBottom(mFitPaddingBottom);
|
||||
}
|
||||
|
||||
insets.set(insets.left, insets.top, insets.right, 0);
|
||||
|
||||
return super.fitSystemWindows(insets);
|
||||
}
|
||||
|
||||
public int getFitPaddingBottom() {
|
||||
return mFitPaddingBottom;
|
||||
}
|
||||
|
||||
public interface OnGetFitPaddingBottomListener {
|
||||
void onGetFitPaddingBottom(int b);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user