|
2 | 2 |
|
3 | 3 | import android.content.Context; |
4 | 4 | import android.graphics.Bitmap; |
| 5 | +import android.graphics.BitmapFactory; |
5 | 6 | import android.graphics.Canvas; |
6 | 7 | import android.graphics.Paint; |
7 | | -import android.graphics.PixelFormat; |
8 | 8 | import android.graphics.PorterDuff; |
9 | 9 | import android.graphics.PorterDuffXfermode; |
10 | 10 | import android.graphics.Rect; |
11 | | -import android.graphics.drawable.BitmapDrawable; |
12 | 11 | import android.graphics.drawable.Drawable; |
13 | 12 | import android.graphics.drawable.NinePatchDrawable; |
14 | 13 | import android.util.AttributeSet; |
15 | 14 |
|
16 | 15 | import androidx.appcompat.widget.AppCompatImageView; |
17 | 16 |
|
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 | + |
23 | 21 |
|
24 | 22 | public class RoundImageView extends AppCompatImageView { |
| 23 | + public static int defaultKeepValue = 50; //默认压缩到 50KB |
| 24 | + private static int ORI_SCALE = 100; //原始比例 |
| 25 | + |
25 | 26 | private int mBorderThickness = 0; |
26 | 27 | private int defaultColor = 0xFFFFFFFF; |
27 | 28 | // 如果只有其中一个有值,则只画一个圆形边框 |
@@ -128,7 +129,45 @@ private Bitmap drawableToBitmap(Drawable drawable, int w, int h) { |
128 | 129 | drawable.setBounds(0, 0, w, h); |
129 | 130 | // 把 drawable 内容画到画布中 |
130 | 131 | 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; // 获取图片流失败,返回原图 |
132 | 171 | } |
133 | 172 |
|
134 | 173 | /** |
|
0 commit comments