Skip to content

Commit a36bcfd

Browse files
committed
修复压缩包解码时的闪退问题
修复低版本Android图像被裁剪的问题
1 parent 3e9c361 commit a36bcfd

File tree

9 files changed

+179
-111
lines changed

9 files changed

+179
-111
lines changed

app/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ android {
2626

2727
defaultConfig {
2828
applicationId "com.xjs.ehviewer"
29-
minSdkVersion 23
29+
minSdkVersion 24
3030
//noinspection ExpiredTargetSdkVersion
31-
targetSdkVersion 29
31+
targetSdkVersion 30
3232
versionCode 111
3333
versionName "1.9.9.3"
3434
vectorDrawables.useSupportLibrary = true
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
/*
2+
* Copyright 2016 Hippo Seven
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.hippo.drawable;
18+
19+
import android.graphics.Canvas;
20+
import android.graphics.Rect;
21+
import android.graphics.RectF;
22+
import android.graphics.drawable.ClipDrawable;
23+
import android.graphics.drawable.Drawable;
24+
import android.graphics.drawable.DrawableWrapper;
25+
import android.view.Gravity;
26+
27+
import androidx.annotation.NonNull;
28+
29+
import com.hippo.lib.yorozuya.MathUtils;
30+
31+
/**
32+
* Show a part of the original drawable
33+
*/
34+
public class PreciselyClipDrawable extends DrawableWrapper {
35+
36+
private final boolean mClip;
37+
private RectF mScale;
38+
private Rect mTemp;
39+
private int offsetX;
40+
private int offsetY;
41+
private int width;
42+
private int height;
43+
44+
public PreciselyClipDrawable(Drawable drawable, int offsetX, int offsetY, int width, int height) {
45+
super(drawable);
46+
this.offsetX = offsetX;
47+
this.offsetY = offsetY;
48+
this.width = width;
49+
this.height = height;
50+
float originWidth = drawable.getIntrinsicWidth();
51+
float originHeight = drawable.getIntrinsicHeight();
52+
if (originWidth <= 0 || originHeight <= 0) {
53+
// Can not clip
54+
mClip = false;
55+
} else {
56+
mClip = true;
57+
mScale = new RectF();
58+
mScale.set(MathUtils.clamp(offsetX / originWidth, 0.0f, 1.0f),
59+
MathUtils.clamp(offsetY / originHeight, 0.0f, 1.0f),
60+
MathUtils.clamp((offsetX + width) / originWidth, 0.0f, 1.0f),
61+
MathUtils.clamp((offsetY + height) / originHeight, 0.0f, 1.0f));
62+
mTemp = new Rect();
63+
}
64+
}
65+
66+
@Override
67+
protected void onBoundsChange(@NonNull Rect bounds) {
68+
if (mClip) {
69+
if (!mScale.isEmpty()) {
70+
mTemp.left = (int) ((mScale.left * bounds.right - mScale.right * bounds.left) /
71+
(mScale.left * (1 - mScale.right) - mScale.right * (1 - mScale.left)));
72+
mTemp.right = (int) (((1 - mScale.right) * bounds.left - (1 - mScale.left) * bounds.right) /
73+
(mScale.left * (1 - mScale.right) - mScale.right * (1 - mScale.left)));
74+
mTemp.top = (int) ((mScale.top * bounds.bottom - mScale.bottom * bounds.top) /
75+
(mScale.top * (1 - mScale.bottom) - mScale.bottom * (1 - mScale.top)));
76+
mTemp.bottom = (int) (((1 - mScale.bottom) * bounds.top - (1 - mScale.top) * bounds.bottom) /
77+
(mScale.top * (1 - mScale.bottom) - mScale.bottom * (1 - mScale.top)));
78+
super.onBoundsChange(mTemp);
79+
}
80+
} else {
81+
super.onBoundsChange(bounds);
82+
}
83+
// super.onBoundsChange(bounds);
84+
}
85+
86+
87+
@Override
88+
public int getIntrinsicWidth() {
89+
if (mClip) {
90+
return (int) (super.getIntrinsicWidth() * mScale.width());
91+
} else {
92+
return super.getIntrinsicWidth();
93+
}
94+
}
95+
96+
@Override
97+
public int getIntrinsicHeight() {
98+
if (mClip) {
99+
return (int) (super.getIntrinsicHeight() * mScale.height());
100+
} else {
101+
return super.getIntrinsicHeight();
102+
}
103+
}
104+
105+
@Override
106+
public void draw(@NonNull Canvas canvas) {
107+
if (mClip) {
108+
if (!mScale.isEmpty()) {
109+
// int saveCount = canvas.save();
110+
canvas.clipRect(getBounds());
111+
super.draw(canvas);
112+
// canvas.restoreToCount(saveCount);
113+
canvas.restore();
114+
}
115+
} else {
116+
super.draw(canvas);
117+
}
118+
}
119+
}

app/src/main/java/com/hippo/drawable/PreciselyClipDrawable.kt

Lines changed: 0 additions & 64 deletions
This file was deleted.

app/src/main/java/com/hippo/ehviewer/gallery/ArchiveGalleryProvider.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
package com.hippo.ehviewer.gallery;
1818

1919
import android.content.Context;
20+
import android.graphics.drawable.BitmapDrawable;
21+
import android.graphics.drawable.Drawable;
2022
import android.net.Uri;
2123
import android.os.Process;
2224
import androidx.annotation.NonNull;
@@ -260,7 +262,7 @@ public void run() {
260262
}
261263

262264
try {
263-
Image image = Image.decode((FileInputStream) stream, false);
265+
Image image = Image.decode(BitmapDrawable.createFromStream(stream,null), false);
264266
if (image != null) {
265267
notifyPageSucceed(index, image);
266268
} else {

app/src/main/java/com/hippo/lib/glview/image/ImageWrapper.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -122,14 +122,14 @@ public int getHeight() {
122122
return mCut.height();
123123
}
124124

125-
/**
126-
* @see Image#render(int, int, Bitmap, int, int, int, int)
127-
*/
128-
public void render(int srcX, int srcY, Bitmap dst, int dstX, int dstY,
129-
int width, int height, boolean fillBlank, int defaultColor) {
130-
mImage.render(srcX + mCut.left, srcY + mCut.top, dst, dstX, dstY,
131-
width, height);
132-
}
125+
// /**
126+
// * @see Image#render(int, int, Bitmap, int, int, int, int)
127+
// */
128+
// public void render(int srcX, int srcY, Bitmap dst, int dstX, int dstY,
129+
// int width, int height, boolean fillBlank, int defaultColor) {
130+
// mImage.render(srcX + mCut.left, srcY + mCut.top, dst, dstX, dstY,
131+
// width, height);
132+
// }
133133

134134
/**
135135
* @see Image#texImage(boolean, int, int, int, int)

app/src/main/java/com/hippo/lib/image/Image.kt

Lines changed: 38 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ package com.hippo.lib.image
2020
import android.content.Context
2121
import android.content.res.Resources
2222
import android.graphics.Bitmap
23+
import android.graphics.BitmapFactory
2324
import android.graphics.Canvas
2425
import android.graphics.ImageDecoder
2526
import android.graphics.ImageDecoder.ALLOCATOR_DEFAULT
@@ -81,11 +82,11 @@ class Image private constructor(
8182
}
8283
// Should we lazy decode it?
8384
} else {
84-
mObtainedDrawable=Drawable.createFromStream(source,null)
85+
mObtainedDrawable=BitmapDrawable.createFromStream(source,null)
8586
}
8687
}
8788
if (mObtainedDrawable == null) {
88-
mObtainedDrawable = drawable
89+
mObtainedDrawable = drawable!!
8990
// throw IllegalArgumentException("数据解码出错")
9091
}
9192
}
@@ -95,8 +96,8 @@ class Image private constructor(
9596
} else {
9697
mObtainedDrawable is AnimationDrawable
9798
}
98-
val width = mObtainedDrawable!!.intrinsicWidth
99-
val height = mObtainedDrawable!!.intrinsicHeight
99+
val width = (mObtainedDrawable as? BitmapDrawable)?.bitmap?.width ?: mObtainedDrawable!!.intrinsicWidth
100+
val height = (mObtainedDrawable as? BitmapDrawable)?.bitmap?.height ?: mObtainedDrawable!!.intrinsicHeight
100101
val isRecycled = mObtainedDrawable == null
101102

102103
var started = false
@@ -129,28 +130,28 @@ class Image private constructor(
129130
mObtainedDrawable!!.draw(Canvas(mBitmap!!))
130131
}
131132

132-
fun render(
133-
srcX: Int, srcY: Int, dst: Bitmap, dstX: Int, dstY: Int,
134-
width: Int, height: Int
135-
) {
136-
check(!hardware) { "Hardware buffer cannot be used in glgallery" }
137-
val bitmap: Bitmap = if (animated) {
138-
updateBitmap()
139-
mBitmap!!
140-
} else {
141-
(mObtainedDrawable as BitmapDrawable).bitmap
142-
}
143-
nativeRender(
144-
bitmap,
145-
srcX,
146-
srcY,
147-
dst,
148-
dstX,
149-
dstY,
150-
width,
151-
height
152-
)
153-
}
133+
// fun render(
134+
// srcX: Int, srcY: Int, dst: Bitmap, dstX: Int, dstY: Int,
135+
// width: Int, height: Int
136+
// ) {
137+
// check(!hardware) { "Hardware buffer cannot be used in glgallery" }
138+
// val bitmap: Bitmap = if (animated) {
139+
// updateBitmap()
140+
// mBitmap!!
141+
// } else {
142+
// (mObtainedDrawable as BitmapDrawable).bitmap
143+
// }
144+
// nativeRender(
145+
// bitmap,
146+
// srcX,
147+
// srcY,
148+
// dst,
149+
// dstX,
150+
// dstY,
151+
// width,
152+
// height
153+
// )
154+
// }
154155

155156
fun texImage(init: Boolean, offsetX: Int, offsetY: Int, width: Int, height: Int) {
156157
check(!hardware) { "Hardware buffer cannot be used in glgallery" }
@@ -211,6 +212,17 @@ class Image private constructor(
211212
}
212213
}
213214

215+
@JvmStatic
216+
fun decode(drawable: Drawable?, hardware: Boolean = true): Image? {
217+
try {
218+
return Image(null,drawable, hardware = hardware)
219+
} catch (e: Exception) {
220+
e.printStackTrace()
221+
FirebaseCrashlytics.getInstance().recordException(e)
222+
return null
223+
}
224+
}
225+
214226
// @JvmStatic
215227
// fun decode(buffer: ByteBuffer, hardware: Boolean = true, release: () -> Unit? = {}): Image {
216228
// val src = ImageDecoder.createSource(buffer)

app/src/main/java/com/hippo/widget/FixedAspectImageView.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,12 @@
2222
import android.util.AttributeSet;
2323
import android.view.View;
2424
import androidx.appcompat.widget.AppCompatImageView;
25+
26+
import com.google.android.material.imageview.ShapeableImageView;
2527
import com.hippo.ehviewer.R;
2628
import com.hippo.lib.yorozuya.MathUtils;
2729

28-
public class FixedAspectImageView extends AppCompatImageView {
30+
public class FixedAspectImageView extends ShapeableImageView {
2931

3032
private static final int[] MIN_ATTRS = {
3133
android.R.attr.minWidth,

0 commit comments

Comments
 (0)