Do trick to draw disappearing children first
This commit is contained in:
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);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user