From 031d5d6c2608e91422c2b32f19b009df871f9fa4 Mon Sep 17 00:00:00 2001 From: heyawei <379135467@qq.com> Date: Wed, 19 May 2021 20:08:09 +0800 Subject: [PATCH] =?UTF-8?q?RoundImageView.java=20=E5=8A=A0=E8=BD=BD?= =?UTF-8?q?=E8=BF=87=E5=A4=A7=E5=9B=BE=E7=89=87=E9=80=A0=E6=88=90=E7=9A=84?= =?UTF-8?q?=20OOM=20=EF=BC=88=E5=AF=B9=E4=BD=8D=E5=9B=BE=E8=B5=84=E6=BA=90?= =?UTF-8?q?=E5=81=9A=E6=9C=89=E9=99=90=E5=8E=8B=E7=BC=A9=E5=86=8D=E5=8A=A0?= =?UTF-8?q?=E8=BD=BD=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit https://console.firebase.google.com/project/tencent-iot/crashlytics/app/android:com.tencent.iot.explorer.link.opensource/issues/9bd4aba9acbf61491b70df11fd0732fa?time=last-ninety-days&sessionEventKey=60617EA4023D0001399A3EDE8FD78668_1523417519714142575 Change-Id: I3ab9ae42a65b56f998d161a3e46fcf9b017e8110 --- .../link/customview/image/RoundImageView.java | 55 ++++++++++++++++--- .../link/kitlink/fragment/MeFragment.kt | 7 +-- 2 files changed, 48 insertions(+), 14 deletions(-) diff --git a/app/src/main/java/com/tencent/iot/explorer/link/customview/image/RoundImageView.java b/app/src/main/java/com/tencent/iot/explorer/link/customview/image/RoundImageView.java index 767f6ba77..04e42d379 100644 --- a/app/src/main/java/com/tencent/iot/explorer/link/customview/image/RoundImageView.java +++ b/app/src/main/java/com/tencent/iot/explorer/link/customview/image/RoundImageView.java @@ -2,26 +2,27 @@ import android.content.Context; import android.graphics.Bitmap; +import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.Paint; -import android.graphics.PixelFormat; import android.graphics.PorterDuff; import android.graphics.PorterDuffXfermode; import android.graphics.Rect; -import android.graphics.drawable.BitmapDrawable; import android.graphics.drawable.Drawable; import android.graphics.drawable.NinePatchDrawable; import android.util.AttributeSet; import androidx.appcompat.widget.AppCompatImageView; -/** - * 将矩形的图片裁剪成圆形 - * 圆形ImageView,可设置最多两个宽度不同且颜色不同的圆形边框。 - * Created by THINK on 2017/4/26. - */ +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; + public class RoundImageView extends AppCompatImageView { + public static int defaultKeepValue = 50; //默认压缩到 50KB + private static int ORI_SCALE = 100; //原始比例 + private int mBorderThickness = 0; private int defaultColor = 0xFFFFFFFF; // 如果只有其中一个有值,则只画一个圆形边框 @@ -128,7 +129,45 @@ private Bitmap drawableToBitmap(Drawable drawable, int w, int h) { drawable.setBounds(0, 0, w, h); // 把 drawable 内容画到画布中 drawable.draw(canvas); - return bitmap; + return compressImage(bitmap, defaultKeepValue); + } + + private double getBitmapSize(Bitmap image) { // 解析图片失败,直接返回没有大小 + try (ByteArrayOutputStream tagBaos = new ByteArrayOutputStream()) { + image.compress(Bitmap.CompressFormat.JPEG, ORI_SCALE, tagBaos); + double imgBytes = tagBaos.toByteArray().length * 1.0 / 1024; + return imgBytes; + } catch (IOException e) { + e.printStackTrace(); + } + return 0; + } + + private Bitmap compressImage(Bitmap image, int expectSize) { + if (image == null) return null; + + int options = ORI_SCALE; + double imgBytes = getBitmapSize(image); + if (imgBytes <= 0) return image; // 没有尺寸结果,返回原图 + + try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) { + if (imgBytes > expectSize) { // 超出默认的值。计算新的尺寸 + options = (int) (expectSize / imgBytes * ORI_SCALE); + if (options <= 0) options = 1; + } + + image.compress(Bitmap.CompressFormat.JPEG, options, baos); + try (ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray())) {//获取图片输入流 + return BitmapFactory.decodeStream(isBm, null, null); + } catch (IOException e) { + e.printStackTrace(); + } + + } catch (IOException e) { + e.printStackTrace(); + } + + return image; // 获取图片流失败,返回原图 } /** diff --git a/app/src/main/java/com/tencent/iot/explorer/link/kitlink/fragment/MeFragment.kt b/app/src/main/java/com/tencent/iot/explorer/link/kitlink/fragment/MeFragment.kt index 20e68d992..adcd22f91 100644 --- a/app/src/main/java/com/tencent/iot/explorer/link/kitlink/fragment/MeFragment.kt +++ b/app/src/main/java/com/tencent/iot/explorer/link/kitlink/fragment/MeFragment.kt @@ -74,12 +74,7 @@ class MeFragment : BaseFragment(), View.OnClickListener, MyCallback { private fun showUserInfo() { tv_me_name.text = App.data.userInfo.NickName tv_me_phone.text = App.data.userInfo.PhoneNumber - ImageManager.setImagePath( - this.context, - me_portrait, - App.data.userInfo.Avatar, - 0 - ) + ImageManager.setImagePath(this.context, me_portrait, App.data.userInfo.Avatar, 0) } override fun fail(msg: String?, reqCode: Int) {