Do trick to draw disappearing children first

This commit is contained in:
seven332 committed 2016-01-31 15:21:48 +08:00
1 parent 9b23153e04
commit 9925abf40f
11 files changed
+210 -14

No files matched your search

@@ -19,8 +19,12 @@ package com.hippo.scene;
import android.os.Bundle;
import android.support.annotation.IntDef;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import com.hippo.ehviewer.R;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
@@ -97,4 +101,12 @@ public class SceneFragment extends Fragment {
public boolean onBackPressed() {
return false;
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
view.setTag(R.id.fragment_tag, getTag());
view.setBackgroundColor(getContext().getResources().getColor(R.color.background_light));
}
}
@@ -28,7 +28,6 @@ import com.hippo.ehviewer.R;
import com.hippo.yorozuya.IntIdGenerator;
import java.util.ArrayList;
import java.util.Collections;
public abstract class StageActivity extends AppCompatActivity {
@@ -148,7 +147,14 @@ public abstract class StageActivity extends AppCompatActivity {
}
int getStackIndex(String tag) {
return Collections.binarySearch(mSceneTagList, tag);
return mSceneTagList.indexOf(tag); // Collections.binarySearch(mSceneTagList, tag);
}
// TODO What about id is negative
int compareScene(String tag1, String tag2) throws NumberFormatException {
int int1 = Integer.parseInt(tag1);
int int2 = Integer.parseInt(tag2);
return int1 - int2;
}
public void finishScene(SceneFragment scene) {
@@ -0,0 +1,143 @@
/*
* Copyright 2016 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
* distributed under the License is distributed on an "AS IS" BASIS,
* 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.scene;
import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import com.hippo.ehviewer.R;
import java.lang.reflect.Field;
import java.util.ArrayList;
public class StageLayout extends FrameLayout {
private Field mDisappearingChildrenField;
private ArrayList<View> mSuperDisappearingChildren;
private View mDumpView;
private boolean mDoTrick;
public StageLayout(Context context) {
super(context);
init(context);
}
public StageLayout(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
public StageLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context);
}
private void init(Context context) {
try {
mDisappearingChildrenField = ViewGroup.class.getDeclaredField("mDisappearingChildren");
mDisappearingChildrenField.setAccessible(true);
} catch (NoSuchFieldException e) {
e.printStackTrace();
}
if (mDisappearingChildrenField != null) {
mDumpView = new View(context);
addView(mDumpView, LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
}
}
@SuppressWarnings("unchecked")
private void getSuperDisappearingChildren() {
if (mDisappearingChildrenField == null || mSuperDisappearingChildren != null) {
return;
}
try {
mSuperDisappearingChildren = (ArrayList<View>) mDisappearingChildrenField.get(this);
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
private String getViewTag(View view) {
Object tag = view.getTag(R.id.fragment_tag);
if (tag instanceof String) {
return (String) tag;
} else {
return null;
}
}
private boolean beforeDispatchDraw() {
getSuperDisappearingChildren();
if (mSuperDisappearingChildren == null ||
mSuperDisappearingChildren.size() <= 0 || getChildCount() <= 1) { // dump view
return false;
}
StageActivity stage = null;
Context context = getContext();
if (context instanceof StageActivity) {
stage = (StageActivity) context;
}
if (stage == null) {
return false;
}
View view1 = mSuperDisappearingChildren.get(0);
View view2 = getChildAt(1); // dump view
String tag1 = getViewTag(view1);
String tag2 = getViewTag(view2);
try {
return stage.compareScene(tag1, tag2) < 0;
} catch (NumberFormatException e) {
return false;
}
}
@Override
protected void dispatchDraw(Canvas canvas) {
mDoTrick = beforeDispatchDraw();
super.dispatchDraw(canvas);
mDoTrick = false;
}
@Override
protected boolean drawChild(Canvas canvas, View child, long drawingTime) {
ArrayList<View> disappearingChildren = mSuperDisappearingChildren;
if (mDoTrick && disappearingChildren != null && disappearingChildren.size() > 0) {
if (child == mDumpView) {
boolean more = false;
for (int i = disappearingChildren.size() - 1; i >= 0; i--) {
more |= super.drawChild(canvas, disappearingChildren.get(i), drawingTime);
}
return more;
} else if (disappearingChildren.contains(child)) {
// Skip
return false;
}
}
return super.drawChild(canvas, child, drawingTime);
}
}
@@ -15,6 +15,6 @@
~ limitations under the License.
-->
<accelerateInterpolator
<decelerateInterpolator
xmlns:android="http://schemas.android.com/apk/res/android"
android:factor="1.5"/>
+2 -2
View File
@@ -19,12 +19,12 @@
xmlns:android="http://schemas.android.com/apk/res/android">
<alpha
android:fromAlpha="0.0"
android:fromAlpha="0.7"
android:toAlpha="1.0"
android:fillEnabled="true"
android:fillBefore="true"
android:fillAfter="true"
android:interpolator="@anim/accelerate_terz"
android:interpolator="@anim/decelerate_terz"
android:duration="250"/>
</set>
+4 -5
View File
@@ -16,16 +16,15 @@
-->
<set
xmlns:android="http://schemas.android.com/apk/res/android"
android:background="#ff000000">
xmlns:android="http://schemas.android.com/apk/res/android">
<alpha
android:fromAlpha="1.0"
android:toAlpha="0.0"
android:toAlpha="0.7"
android:fillEnabled="true"
android:fillBefore="true"
android:fillBefore="false"
android:fillAfter="true"
android:interpolator="@android:interpolator/accelerate_decelerate"
android:duration="350"/>
android:duration="217"/>
</set>
+2 -1
View File
@@ -22,7 +22,7 @@
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<FrameLayout
<com.hippo.scene.StageLayout
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
@@ -34,6 +34,7 @@
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="left"
android:background="@color/background_light"
android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header_main"
app:menu="@menu/nav_drawer_main"/>
+1 -1
View File
@@ -17,7 +17,7 @@
<resources>
<style name="AppTheme.Main" parent="AppTheme.NoActionBar">
<style name="AppTheme.Main" parent="AppTheme.Translucent.NoActionBar">
<item name="android:windowDrawsSystemBarBackgrounds">true</item>
<item name="android:statusBarColor">@android:color/transparent</item>
</style>
+3
View File
@@ -36,6 +36,9 @@
<color name="secondary_text_default_dark">#61ffffff</color>
<color name="secondary_text_disabled_light">#24000000</color>
<color name="background_dark">#ff303030</color>
<color name="background_light">#fffafafa</color>
<color name="red_500">#fff44336</color>
<color name="teal_500">#ff009688</color>
<color name="teal_700">#ff00796b</color>
+22
View File
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
~ Copyright 2016 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
~ distributed under the License is distributed on an "AS IS" BASIS,
~ 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.
-->
<resources>
<item name="fragment_tag" type="id"/>
</resources>
+12 -2
View File
@@ -24,12 +24,22 @@
<item name="colorAccent">@color/colorAccent</item>
</style>
<style name="AppTheme.NoActionBar">
<style name="AppTheme.Translucent">
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:colorBackgroundCacheHint">@null</item>
<item name="android:windowIsTranslucent">true</item>
<!-- Note that we use the base animation style here (that is no
animations) because we really have no idea how this kind of
activity will be used. -->
<item name="android:windowAnimationStyle">@android:style/Animation</item>
</style>
<style name="AppTheme.Translucent.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
<style name="AppTheme.Main" parent="AppTheme.NoActionBar">
<style name="AppTheme.Main" parent="AppTheme.Translucent.NoActionBar">
</style>
</resources>