Skip to content

Commit 031d5d6

Browse files
sevenhheSundoggyNew
authored andcommitted
1 parent 755d97d commit 031d5d6

File tree

2 files changed

+48
-14
lines changed

2 files changed

+48
-14
lines changed

app/src/main/java/com/tencent/iot/explorer/link/customview/image/RoundImageView.java

Lines changed: 47 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,27 @@
22

33
import android.content.Context;
44
import android.graphics.Bitmap;
5+
import android.graphics.BitmapFactory;
56
import android.graphics.Canvas;
67
import android.graphics.Paint;
7-
import android.graphics.PixelFormat;
88
import android.graphics.PorterDuff;
99
import android.graphics.PorterDuffXfermode;
1010
import android.graphics.Rect;
11-
import android.graphics.drawable.BitmapDrawable;
1211
import android.graphics.drawable.Drawable;
1312
import android.graphics.drawable.NinePatchDrawable;
1413
import android.util.AttributeSet;
1514

1615
import androidx.appcompat.widget.AppCompatImageView;
1716

18-
/**
19-
* 将矩形的图片裁剪成圆形
20-
* 圆形ImageView,可设置最多两个宽度不同且颜色不同的圆形边框。
21-
* Created by THINK on 2017/4/26.
22-
*/
17+
import java.io.ByteArrayInputStream;
18+
import java.io.ByteArrayOutputStream;
19+
import java.io.IOException;
20+
2321

2422
public class RoundImageView extends AppCompatImageView {
23+
public static int defaultKeepValue = 50; //默认压缩到 50KB
24+
private static int ORI_SCALE = 100; //原始比例
25+
2526
private int mBorderThickness = 0;
2627
private int defaultColor = 0xFFFFFFFF;
2728
// 如果只有其中一个有值,则只画一个圆形边框
@@ -128,7 +129,45 @@ private Bitmap drawableToBitmap(Drawable drawable, int w, int h) {
128129
drawable.setBounds(0, 0, w, h);
129130
// 把 drawable 内容画到画布中
130131
drawable.draw(canvas);
131-
return bitmap;
132+
return compressImage(bitmap, defaultKeepValue);
133+
}
134+
135+
private double getBitmapSize(Bitmap image) { // 解析图片失败,直接返回没有大小
136+
try (ByteArrayOutputStream tagBaos = new ByteArrayOutputStream()) {
137+
image.compress(Bitmap.CompressFormat.JPEG, ORI_SCALE, tagBaos);
138+
double imgBytes = tagBaos.toByteArray().length * 1.0 / 1024;
139+
return imgBytes;
140+
} catch (IOException e) {
141+
e.printStackTrace();
142+
}
143+
return 0;
144+
}
145+
146+
private Bitmap compressImage(Bitmap image, int expectSize) {
147+
if (image == null) return null;
148+
149+
int options = ORI_SCALE;
150+
double imgBytes = getBitmapSize(image);
151+
if (imgBytes <= 0) return image; // 没有尺寸结果,返回原图
152+
153+
try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
154+
if (imgBytes > expectSize) { // 超出默认的值。计算新的尺寸
155+
options = (int) (expectSize / imgBytes * ORI_SCALE);
156+
if (options <= 0) options = 1;
157+
}
158+
159+
image.compress(Bitmap.CompressFormat.JPEG, options, baos);
160+
try (ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray())) {//获取图片输入流
161+
return BitmapFactory.decodeStream(isBm, null, null);
162+
} catch (IOException e) {
163+
e.printStackTrace();
164+
}
165+
166+
} catch (IOException e) {
167+
e.printStackTrace();
168+
}
169+
170+
return image; // 获取图片流失败,返回原图
132171
}
133172

134173
/**

app/src/main/java/com/tencent/iot/explorer/link/kitlink/fragment/MeFragment.kt

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,7 @@ class MeFragment : BaseFragment(), View.OnClickListener, MyCallback {
7474
private fun showUserInfo() {
7575
tv_me_name.text = App.data.userInfo.NickName
7676
tv_me_phone.text = App.data.userInfo.PhoneNumber
77-
ImageManager.setImagePath(
78-
this.context,
79-
me_portrait,
80-
App.data.userInfo.Avatar,
81-
0
82-
)
77+
ImageManager.setImagePath(this.context, me_portrait, App.data.userInfo.Avatar, 0)
8378
}
8479

8580
override fun fail(msg: String?, reqCode: Int) {

0 commit comments

Comments
 (0)