23
23
import android .support .v7 .widget .AppCompatImageView ;
24
24
import android .widget .Toast ;
25
25
26
+ import java .io .ByteArrayInputStream ;
27
+ import java .io .ByteArrayOutputStream ;
26
28
import java .lang .ref .WeakReference ;
27
29
28
30
/**
@@ -53,7 +55,6 @@ public class PaletteImageView extends View implements ViewTreeObserver.OnGlobalL
53
55
public PaletteImageView mInstance ;
54
56
55
57
56
-
57
58
private Handler mHandler = new Handler () {
58
59
@ Override
59
60
public void handleMessage (Message msg ) {
@@ -106,22 +107,24 @@ protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
106
107
mOnMeasureHeightMode = MeasureSpec .getMode (heightMeasureSpec );
107
108
if (mOnMeasureHeightMode == MeasureSpec .UNSPECIFIED ) {
108
109
if (mBitmap != null ) {
109
- height = (int ) ((width -mPadding * 2 ) * (mBitmap .getHeight () * 1.0f / mBitmap .getWidth ())) + mPadding * 2 ;
110
+ height = (int ) ((width - mPadding * 2 ) * (mBitmap .getHeight () * 1.0f / mBitmap .getWidth ())) + mPadding * 2 ;
110
111
}
111
- if (mImgId != 0 && mRealBitmap != null ){
112
+ if (mImgId != 0 && mRealBitmap != null ) {
112
113
height = mRealBitmap .getHeight () + mPadding * 2 ;
113
114
}
114
115
}
115
116
if (mBitmap != null ) {
116
- height = (int ) ((width -mPadding * 2 ) * (mBitmap .getHeight () * 1.0f / mBitmap .getWidth ())) + mPadding * 2 ;
117
+ height = (int ) ((width - mPadding * 2 ) * (mBitmap .getHeight () * 1.0f / mBitmap .getWidth ())) + mPadding * 2 ;
118
+ Log .e (TAG , "bitmap宽高:" + mBitmap .getWidth () + "/" + mBitmap .getHeight () + "--" + (mBitmap .getRowBytes () * mBitmap .getHeight () / 1024 / 1024 ));
117
119
}
118
120
setMeasuredDimension (width , height );
119
121
}
120
122
121
123
@ Override
122
124
protected void onSizeChanged (int w , int h , int oldw , int oldh ) {
123
125
super .onSizeChanged (w , h , oldw , oldh );
124
- zipBitmap (mImgId ,mBitmap ,mOnMeasureHeightMode );
126
+ mBitmap = ratio (mBitmap );
127
+ zipBitmap (mImgId , mBitmap , mOnMeasureHeightMode );
125
128
}
126
129
127
130
@ Override
@@ -151,7 +154,7 @@ private Bitmap createRoundConerImage(Bitmap source, int radius) {
151
154
Paint paint = weakPaint .get ();
152
155
paint .setAntiAlias (true );
153
156
paint .setDither (true );
154
- WeakReference <Bitmap > weakBitmap = new WeakReference <Bitmap >(Bitmap .createBitmap (getWidth () - mPadding * 2 , getHeight () - mPadding * 2 , Bitmap .Config .ARGB_8888 ));
157
+ WeakReference <Bitmap > weakBitmap = new WeakReference <Bitmap >(Bitmap .createBitmap (getWidth () - mPadding * 2 , getHeight () - mPadding * 2 , Bitmap .Config .ARGB_4444 ));
155
158
if (weakBitmap .get () == null ) return null ;
156
159
Bitmap target = weakBitmap .get ();
157
160
@@ -195,30 +198,30 @@ private void initShadow(Bitmap bitmap) {
195
198
}
196
199
197
200
198
-
199
- private void zipBitmap (int imgId ,Bitmap bitmap ,int heightNode ) {
201
+ private void zipBitmap (int imgId , Bitmap bitmap , int heightNode ) {
202
+ Log .e (TAG , "zip:" + bitmap .getWidth () + "/" + bitmap .getHeight ());
203
+ WeakReference <Matrix > weakMatrix = new WeakReference <Matrix >(new Matrix ());
204
+ if (weakMatrix .get () == null ) return ;
205
+ Matrix matrix = weakMatrix .get ();
206
+ int reqWidth = getWidth () - mPadding - mPadding ;
207
+ int reqHeight = getHeight () - mPadding - mPadding ;
208
+ if (reqHeight <= 0 || reqWidth <= 0 ) return ;
200
209
int rawWidth = 0 ;
201
210
int rawHeight = 0 ;
202
- if (imgId !=0 && bitmap == null ){
211
+ if (imgId != 0 && bitmap == null ) {
203
212
WeakReference <BitmapFactory .Options > weakOptions = new WeakReference <BitmapFactory .Options >(new BitmapFactory .Options ());
204
213
if (weakOptions .get () == null ) return ;
205
214
BitmapFactory .Options options = weakOptions .get ();
206
- options = new BitmapFactory .Options ();
207
215
BitmapFactory .decodeResource (getResources (), imgId , options );
216
+ options .inJustDecodeBounds = true ;
208
217
rawWidth = options .outWidth ;
209
218
rawHeight = options .outHeight ;
210
- bitmap = BitmapFactory .decodeResource (getResources (),mImgId );
211
- }else if (imgId == 0 && bitmap != null ){
219
+ options .inSampleSize = calculateInSampleSize (rawWidth , rawHeight , getWidth () - mPadding * 2 , getHeight () - mPadding * 2 );
220
+ options .inJustDecodeBounds = false ;
221
+ bitmap = BitmapFactory .decodeResource (getResources (), mImgId , options );
222
+ } else if (imgId == 0 && bitmap != null ) {
212
223
rawWidth = bitmap .getWidth ();
213
224
rawHeight = bitmap .getHeight ();
214
- }
215
- int reqWidth = getWidth () - mPadding - mPadding ;
216
- int reqHeight = getHeight () - mPadding - mPadding ;
217
- if (reqHeight <= 0 || reqWidth <= 0 ) return ;
218
- WeakReference <Matrix > weakMatrix = new WeakReference <Matrix >(new Matrix ());
219
- if (weakMatrix .get () == null ) return ;
220
- Matrix matrix = weakMatrix .get ();
221
- if (mBitmap != null ){
222
225
float scale = rawHeight * 1.0f / rawWidth ;
223
226
mRealBitmap = Bitmap .createScaledBitmap (bitmap , reqWidth , (int ) (reqWidth * scale ), true );
224
227
initShadow (mRealBitmap );
@@ -227,42 +230,63 @@ private void zipBitmap(int imgId,Bitmap bitmap,int heightNode) {
227
230
if (heightNode == 0 ) {
228
231
float scale = rawHeight * 1.0f / rawWidth ;
229
232
mRealBitmap = Bitmap .createScaledBitmap (bitmap , reqWidth , (int ) (reqWidth * scale ), true );
230
- }else {
233
+ } else {
231
234
int dx = 0 ;
232
235
int dy = 0 ;
233
236
int small = Math .min (rawHeight , rawWidth );
234
237
int big = Math .max (reqWidth , reqHeight );
235
238
float scale = big * 1.0f / small ;
236
- matrix .setScale (scale ,scale );
239
+ matrix .setScale (scale , scale );
237
240
if (rawHeight > rawWidth ) {
238
- dy = (rawHeight - rawWidth ) / 2 ;
241
+ dy = (rawHeight - rawWidth ) / 2 ;
239
242
} else if (rawHeight < rawWidth ) {
240
- dx = (rawWidth - rawHeight ) / 2 ;
243
+ dx = (rawWidth - rawHeight ) / 2 ;
241
244
}
242
- mRealBitmap = Bitmap .createBitmap (bitmap ,dx , dy , small , small , matrix , true );
245
+ mRealBitmap = Bitmap .createBitmap (bitmap , dx , dy , small , small , matrix , true );
243
246
}
247
+ if (mRealBitmap != null )
248
+ Log .e (TAG , "mRealBitap:" + mRealBitmap .getWidth () + "/" + mRealBitmap .getHeight () + "--" /*(mBitmap.getRowBytes()* mRealBitmap.getHeight() /1024/1024)*/ );
244
249
initShadow (mRealBitmap );
245
250
246
251
}
247
252
253
+ public Bitmap ratio (Bitmap image ) {
254
+ Bitmap bitmap = null ;
255
+ ByteArrayOutputStream os = new ByteArrayOutputStream ();
256
+ image .compress (Bitmap .CompressFormat .JPEG , 100 , os );
257
+ if (os .toByteArray ().length / 1024 > 1024 ) {//判断如果图片大于1M,进行压缩避免在生成图片(BitmapFactory.decodeStream)时溢出
258
+ os .reset ();//重置baos即清空baos
259
+ image .compress (Bitmap .CompressFormat .JPEG , 50 , os );//这里压缩50%,把压缩后的数据存放到baos中
260
+ }
261
+ ByteArrayInputStream is = new ByteArrayInputStream (os .toByteArray ());
262
+ BitmapFactory .Options newOpts = new BitmapFactory .Options ();
263
+ //开始读入图片,此时把options.inJustDecodeBounds 设回true了
264
+ newOpts .inJustDecodeBounds = true ;
265
+ newOpts .inPreferredConfig = Bitmap .Config .RGB_565 ;
266
+ bitmap = BitmapFactory .decodeStream (is , null , newOpts );
267
+ newOpts .inJustDecodeBounds = false ;
268
+ int w = newOpts .outWidth ;
269
+ int h = newOpts .outHeight ;
270
+ newOpts .inSampleSize = calculateInSampleSize (w , h , getWidth () - mPadding * 2 , getHeight () - mPadding * 2 );//设置缩放比例
271
+ //重新读入图片,注意此时已经把options.inJustDecodeBounds 设回false了
272
+ is = new ByteArrayInputStream (os .toByteArray ());
273
+ bitmap = BitmapFactory .decodeStream (is , null , newOpts );
274
+ return bitmap ;
275
+ }
276
+
248
277
/**
249
278
* 计算inSampleSize
250
279
*
251
280
* @return
252
281
*/
253
282
private int calculateInSampleSize (int rawWidth , int rawHeight , int reqWidth , int reqHeight ) {
254
- int inSampleSize = 0 ;
255
- try {
256
- inSampleSize = 1 ;
257
- if (rawHeight > reqHeight || rawWidth > reqWidth ) {
258
- int halfHeight = rawHeight / 2 ;
259
- int halfWidth = rawWidth / 2 ;
260
- while ((halfHeight / inSampleSize ) >= reqHeight && (halfWidth / inSampleSize ) >= reqWidth ) {
261
- inSampleSize *= 2 ;
262
- }
283
+ int inSampleSize = 1 ;
284
+ if (rawHeight > reqHeight || rawWidth > reqWidth ) {
285
+ int halfHeight = rawHeight / 2 ;
286
+ int halfWidth = rawWidth / 2 ;
287
+ while ((halfHeight / inSampleSize ) >= reqHeight && (halfWidth / inSampleSize ) >= reqWidth ) {
288
+ inSampleSize *= 12 ;
263
289
}
264
- } catch (Exception e ) {
265
- e .printStackTrace ();
266
290
}
267
291
return inSampleSize ;
268
292
}
@@ -317,20 +341,21 @@ public void onGenerated(Palette palette) {
317
341
mMainColor = palette .getDominantSwatch ().getRgb ();
318
342
mHandler .sendEmptyMessage (MSG );
319
343
if (mListener != null ) mListener .onComplete (mInstance );
320
- }else {
344
+ } else {
321
345
if (mListener != null ) mListener .onFail ();
322
346
}
323
347
}
324
348
};
325
349
326
350
private OnParseColorListener mListener ;
327
351
328
- public void setOnParseColorListener (OnParseColorListener listener ){
352
+ public void setOnParseColorListener (OnParseColorListener listener ) {
329
353
this .mListener = listener ;
330
354
}
331
355
332
- public interface OnParseColorListener {
356
+ public interface OnParseColorListener {
333
357
void onComplete (PaletteImageView paletteImageView );
358
+
334
359
void onFail ();
335
360
}
336
361
0 commit comments