Skip to content

Commit

Permalink
为低版本提供webp支持,但是解码webp动图会闪退
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaojieonly committed Nov 6, 2024
1 parent cfe54db commit 0661910
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 35 deletions.
6 changes: 5 additions & 1 deletion app/src/main/java/com/hippo/ehviewer/client/EhHosts.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,13 @@ public class EhHosts implements Dns {
put(map, "upld.e-hentai.org", "94.100.18.249", "94.100.18.247");
put(map, "ehgt.org",
"109.236.85.28",
"62.112.8.21",
"89.39.106.43",
"2a00:7c80:0:123::3a85",
"2a00:7c80:0:12d::38a1",
"2a00:7c80:0:13b::37a4",
"2a00:7c80:0:13b::37a4");
put(map, "ehgt.org",
"109.236.85.28",
"62.112.8.21",
"89.39.106.43");
put(map, "raw.githubusercontent.com", "151.101.0.133", "151.101.64.133", "151.101.128.133", "151.101.192.133");
Expand Down
48 changes: 28 additions & 20 deletions app/src/main/java/com/hippo/lib/image/Image.kt
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import android.graphics.drawable.Drawable
import android.os.Build
import androidx.core.graphics.drawable.toDrawable
import com.google.firebase.crashlytics.FirebaseCrashlytics
import com.hippo.ehviewer.EhApplication
import java.io.FileInputStream
import java.nio.channels.FileChannel
import kotlin.Exception
Expand All @@ -60,10 +61,11 @@ class Image private constructor(
source.available().toLong()
)
)
try{
try {
mObtainedDrawable =
ImageDecoder.decodeDrawable(src) { decoder: ImageDecoder, info: ImageInfo, _: Source ->
decoder.allocator = if (hardware) ALLOCATOR_DEFAULT else ALLOCATOR_SOFTWARE
decoder.allocator =
if (hardware) ALLOCATOR_DEFAULT else ALLOCATOR_SOFTWARE
// Sadly we must use software memory since we need copy it to tile buffer, fuck glgallery
// Idk it will cause how much performance regression

Expand All @@ -75,16 +77,17 @@ class Image private constructor(
)
// Don't
}
}catch (e:DecodeException){
throw Exception("Android 9 解码失败",e)
} catch (e: DecodeException) {
throw Exception("Android 9 解码失败", e)
}
// Should we lazy decode it?
} else {
mObtainedDrawable = Drawable.createFromStream(source, null);
mObtainedDrawable = Drawable.createFromStream(source, null)
}
}
if (mObtainedDrawable == null) {
mObtainedDrawable = drawable!!
// mObtainedDrawable = drawable!!
throw IllegalArgumentException("数据解码出错")
}
}

Expand Down Expand Up @@ -175,9 +178,9 @@ class Image private constructor(

fun start() {
if (!started) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P){
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
(mObtainedDrawable as AnimatedImageDrawable?)?.start()
}else{
} else {
(mObtainedDrawable as AnimationDrawable?)?.start()
}

Expand All @@ -201,20 +204,20 @@ class Image private constructor(
var screenHeight: Int = 0

@JvmStatic
fun initialize(context: Context) {
screenWidth = context.resources.displayMetrics.widthPixels
screenHeight = context.resources.displayMetrics.heightPixels
fun initialize(ehApplication: EhApplication) {
screenWidth = ehApplication.resources.displayMetrics.widthPixels
screenHeight = ehApplication.resources.displayMetrics.heightPixels
}

@JvmStatic
fun decode(stream: FileInputStream, hardware: Boolean = true): Image? {
try {
return Image(stream, hardware = hardware)
}catch (e:Exception){
e.printStackTrace()
FirebaseCrashlytics.getInstance().recordException(e)
return null
}
try {
return Image(stream, hardware = hardware)
} catch (e: Exception) {
e.printStackTrace()
FirebaseCrashlytics.getInstance().recordException(e)
return null
}
}

// @JvmStatic
Expand All @@ -226,8 +229,13 @@ class Image private constructor(
// }

@JvmStatic
fun create(bitmap: Bitmap): Image {
return Image(null, bitmap.toDrawable(Resources.getSystem()), false)
fun create(bitmap: Bitmap): Image? {
try {
return Image(null, bitmap.toDrawable(Resources.getSystem()), false)
} catch (e: Exception) {
e.printStackTrace()
return null
}
}

@JvmStatic
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/java/com/hippo/lib/image/ImageBitmap.kt
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ class ImageBitmap private constructor(image: Image) {

companion object {
@JvmStatic
fun decode(stream: FileInputStream): ImageBitmap {
fun decode(stream: FileInputStream): ImageBitmap? {
val image = Image.decode(stream)
return ImageBitmap(image)
return image?.let { ImageBitmap(it) }
}
}
}
55 changes: 43 additions & 12 deletions app/src/main/java/com/hippo/widget/AvatarImageView.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import android.graphics.RectF;
import android.graphics.drawable.Animatable;
import android.graphics.drawable.AnimatedImageDrawable;
import android.graphics.drawable.AnimationDrawable;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
Expand Down Expand Up @@ -390,26 +391,47 @@ public void onCancel() {
@Override
public void start() {
Drawable drawable = getImageDrawable();
if (drawable instanceof AnimatedImageDrawable animatedImageDrawable) {
animatedImageDrawable.start();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
if (drawable instanceof AnimatedImageDrawable animatedImageDrawable) {
animatedImageDrawable.start();
}
}else {
if (drawable instanceof AnimationDrawable animatedImageDrawable) {
animatedImageDrawable.start();
}
}

}

@Override
public void stop() {
Drawable drawable = getImageDrawable();
if (drawable instanceof AnimatedImageDrawable animatedImageDrawable) {
animatedImageDrawable.stop();
if (Build.VERSION.SDK_INT>Build.VERSION_CODES.P){
if (drawable instanceof AnimatedImageDrawable animatedImageDrawable) {
animatedImageDrawable.stop();
}
}else {
if (drawable instanceof AnimationDrawable animatedImageDrawable) {
animatedImageDrawable.stop();
}
}
}

@Override
public boolean isRunning() {
Drawable drawable = getImageDrawable();
if (drawable instanceof AnimatedImageDrawable animatedImageDrawable) {
return animatedImageDrawable.isRunning();
} else {
return false;
if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.P){
if (drawable instanceof AnimatedImageDrawable animatedImageDrawable) {
return animatedImageDrawable.isRunning();
} else {
return false;
}
}else {
if (drawable instanceof AnimationDrawable animatedImageDrawable) {
return animatedImageDrawable.isRunning();
} else {
return false;
}
}
}

Expand Down Expand Up @@ -475,12 +497,21 @@ private Bitmap createCircleImage(Bitmap source, int width, int height) {
// 核心代码取两个图片的交集部分
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
Bitmap src;
if (source.getConfig() == HARDWARE) {
src = source.copy(Bitmap.Config.ARGB_8888, false);
} else {
src = source;
if (Build.VERSION.SDK_INT>Build.VERSION_CODES.P){
if (source.getConfig() == HARDWARE) {
src = source.copy(Bitmap.Config.ARGB_8888, false);
} else {
src = source;
}
}else {
if (source.getConfig()== Bitmap.Config.ARGB_8888){
src = source;
}else {
src = source.copy(Bitmap.Config.ARGB_8888, false);
}
}


canvas.drawBitmap(src, (width - source.getWidth()) >> 1,
(height - source.getHeight()) >> 1, paint);
return target;
Expand Down

0 comments on commit 0661910

Please sign in to comment.