@@ -47,9 +47,11 @@ public class PaletteImageView extends View implements ViewTreeObserver.OnGlobalL
47
47
private int mOffsetY = DEFAULT_OFFSET ;
48
48
private int mShadowRadius = DEFAULT_SHADOW_RADIUS ;
49
49
private Palette mPalette ;
50
- private BitmapFactory .Options options ;
51
- private int mWeightWidth , mWeightHeight ;
52
50
private RectF mRectFShadow ;
51
+ private Bitmap mRealBitmap ;
52
+ private int mOnMeasureHeightMode = -1 ;
53
+
54
+
53
55
54
56
private Handler mHandler = new Handler () {
55
57
@ Override
@@ -99,35 +101,27 @@ protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
99
101
int width = MeasureSpec .getSize (widthMeasureSpec );
100
102
int widthMode = MeasureSpec .getMode (widthMeasureSpec );
101
103
int height = MeasureSpec .getSize (heightMeasureSpec );
102
- int heightMode = MeasureSpec .getMode (heightMeasureSpec );
103
-
104
- if (mBitmap != null && options != null ) {
105
- int rawWidth = options .outWidth ;
106
- int rawHeight = options .outHeight ;
107
-
108
- if (heightMode == MeasureSpec .UNSPECIFIED ) {
109
- height = (int ) (mWeightWidth * (rawHeight * 1.0f / rawWidth ));
110
- }
111
- if (widthMode == MeasureSpec .EXACTLY ) {
112
- }
113
-
114
- } else {
104
+ mOnMeasureHeightMode = MeasureSpec .getMode (heightMeasureSpec );
105
+ if (mOnMeasureHeightMode == MeasureSpec .UNSPECIFIED ) {
115
106
if (mBitmap != null ) {
116
- zipBitmapFromBitmap (mBitmap );
107
+ height = (int ) ((width -mPadding * 2 ) * (mBitmap .getHeight () * 1.0f / mBitmap .getWidth ())) + mPadding * 2 ;
108
+ }
109
+ if (mImgId != 0 && mRealBitmap != null ){
110
+ height = mRealBitmap .getHeight () + mPadding * 2 ;
117
111
}
118
- int rawWidth = mBitmap .getWidth ();
119
- int rawHeight = mBitmap .getHeight ();
120
- Log .e (TAG ,"onMeasure:" +rawWidth +"/" +rawHeight +"--" +width +"/" +height );
121
- height = (int ) ((width - mPadding * 2 ) * rawHeight * 1.0f / rawWidth ) + mPadding * 2 ;
122
112
}
123
113
setMeasuredDimension (width , height );
124
-
125
114
}
126
115
116
+ @ Override
117
+ protected void onSizeChanged (int w , int h , int oldw , int oldh ) {
118
+ super .onSizeChanged (w , h , oldw , oldh );
119
+ zipBitmap (mImgId ,mBitmap ,mOnMeasureHeightMode );
120
+ }
127
121
128
122
@ Override
129
123
protected void onDraw (Canvas canvas ) {
130
- if (mBitmap != null ) {
124
+ if (mRealBitmap != null ) {
131
125
if (mRectFShadow == null ) {
132
126
WeakReference <RectF > weakRectF = new WeakReference <RectF >(new RectF (mPadding , mPadding , getWidth () - mPadding , getHeight () - mPadding ));
133
127
if (weakRectF .get () == null ) return ;
@@ -137,12 +131,16 @@ protected void onDraw(Canvas canvas) {
137
131
int id1 = canvas .saveLayer (0 , 0 , canvas .getWidth (), canvas .getHeight (), null , Canvas .ALL_SAVE_FLAG );
138
132
canvas .drawRoundRect (mRectFShadow , mRadius , mRadius , mPaintShadow );
139
133
canvas .restoreToCount (id1 );
140
- canvas .drawBitmap (createRoundConerImage (mBitmap , mRadius ), mPadding , mPadding , null );
134
+ canvas .drawBitmap (createRoundConerImage (mRealBitmap , mRadius ), mPadding , mPadding , null );
141
135
if (mMainColor != -1 ) mAsyncTask .cancel (true );
142
136
}
143
137
144
138
}
145
139
140
+ @ Override
141
+ public void onGlobalLayout () {
142
+ }
143
+
146
144
private Bitmap createRoundConerImage (Bitmap source , int radius ) {
147
145
WeakReference <Paint > weakPaint = new WeakReference <Paint >(new Paint ());
148
146
if (weakPaint .get () == null ) return null ;
@@ -157,7 +155,6 @@ private Bitmap createRoundConerImage(Bitmap source, int radius) {
157
155
if (weakCanvas .get () == null ) return null ;
158
156
Canvas canvas = weakCanvas .get ();
159
157
WeakReference <RectF > weakRectF = new WeakReference <RectF >(new RectF (0 , 0 , source .getWidth (), source .getHeight ()));
160
- Log .e (TAG ,"roundRect:" +source .getWidth ()+"/" +source .getHeight ()+" mRadius:" +radius );
161
158
if (weakRectF .get () == null ) return null ;
162
159
RectF rect = weakRectF .get ();
163
160
canvas .drawRoundRect (rect , radius , radius , paint );
@@ -167,17 +164,6 @@ private Bitmap createRoundConerImage(Bitmap source, int radius) {
167
164
return target ;
168
165
}
169
166
170
- @ Override
171
- public void onGlobalLayout () {
172
- mWeightWidth = getWidth ();
173
- mWeightHeight = getHeight ();
174
- if (mImgId != 0 ) {
175
- zipBitmap (mImgId );
176
- initShadow (mBitmap );
177
- } else {
178
- // zipBitmapFromBitmap(mBitmap);
179
- }
180
- }
181
167
182
168
@ Override
183
169
protected void onAttachedToWindow () {
@@ -193,119 +179,59 @@ protected void onAttachedToWindow() {
193
179
*/
194
180
public void setBitmap (Bitmap bitmap ) {
195
181
this .mBitmap = bitmap ;
196
- initShadow (mBitmap );
197
182
}
198
183
199
184
/**
200
185
* 初始化阴影颜色
201
186
*/
202
187
private void initShadow (Bitmap bitmap ) {
203
- if (mBitmap != null ) {
188
+ if (bitmap != null ) {
204
189
mAsyncTask = Palette .from (bitmap ).generate (paletteAsyncListener );
205
190
}
206
191
}
207
192
208
193
209
- /**
210
- * 压缩bitmap
211
- */
212
- private void zipBitmap (int imgId ) {
213
- options = new BitmapFactory .Options ();
214
- BitmapFactory .decodeResource (getResources (), imgId , options );
215
-
216
- int rawWidth = options .outWidth ;
217
- int rawHeight = options .outHeight ;
218
- int reqWidth = mWeightWidth - mPadding - mPadding ;
219
- int reqHeight = mWeightHeight - mPadding - mPadding ;
220
- options .inPreferredConfig = Bitmap .Config .RGB_565 ;
221
- options .inJustDecodeBounds = true ;
222
- options .inSampleSize = calculateInSampleSize (rawWidth , rawHeight , reqWidth , reqHeight );
223
- options .inJustDecodeBounds = false ;
224
- WeakReference <Matrix > weakMatrix = new WeakReference <Matrix >(new Matrix ());
225
- if (weakMatrix .get () == null ) return ;
226
- Matrix matrix = weakMatrix .get ();
227
- if (reqHeight == reqWidth ) {
228
- if (rawWidth == rawHeight ) {
229
- int least = Math .min (rawHeight , reqHeight );
230
- int big = Math .max (rawHeight , reqHeight );
231
- float scale = 0 ;
232
- if (rawHeight > reqHeight ) {
233
- scale = least * 1.0f / big ;
234
- } else {
235
- scale = big * 1.0f / least ;
236
- }
237
- matrix .setScale (scale , scale );
238
- mBitmap = Bitmap .createBitmap (BitmapFactory .decodeResource (getResources (), imgId , options ),
239
- 0 , 0 , rawWidth , rawWidth , matrix , true );
240
- } else if (rawHeight > reqHeight || rawWidth > reqWidth ) {
241
- int dx = 0 ;
242
- int dy = 0 ;
243
- int small = Math .min (rawHeight , rawWidth );
244
- int least = Math .min (small , reqHeight );
245
- float scale = reqHeight * 1.0f / small ;
246
- matrix .setScale (scale , scale );
247
- if (rawHeight > rawWidth ) {
248
- dy = (rawHeight - reqHeight ) / 2 ;
249
- } else {
250
- dx = (rawWidth - reqWidth ) / 2 ;
251
- }
252
- mBitmap = Bitmap .createBitmap (BitmapFactory .decodeResource (getResources (), imgId , options ),
253
- dx , dy , least , least , matrix , true );
254
- }
255
194
256
- } else if (rawHeight < reqHeight && rawWidth < reqWidth && rawHeight < rawWidth ) {
257
- float scale = rawHeight * 1.0f / rawWidth ;
258
- mBitmap = Bitmap .createScaledBitmap (BitmapFactory .decodeResource (getResources (), imgId , options ),
259
- reqWidth , (int ) (reqWidth * scale ), true );
260
- } else {
261
- float scale = rawHeight * 1.0f / rawWidth ;
262
- mBitmap = Bitmap .createScaledBitmap (BitmapFactory .decodeResource (getResources (), imgId , options ),
263
- reqWidth , (int ) (reqWidth * scale ) + mPadding * 2 , true );
195
+ private void zipBitmap (int imgId ,Bitmap bitmap ,int heightNode ) {
196
+ int rawWidth = 0 ;
197
+ int rawHeight = 0 ;
198
+ if (imgId !=0 && bitmap == null ){
199
+ WeakReference <BitmapFactory .Options > weakOptions = new WeakReference <BitmapFactory .Options >(new BitmapFactory .Options ());
200
+ if (weakOptions .get () == null ) return ;
201
+ BitmapFactory .Options options = weakOptions .get ();
202
+ options = new BitmapFactory .Options ();
203
+ BitmapFactory .decodeResource (getResources (), imgId , options );
204
+ rawWidth = options .outWidth ;
205
+ rawHeight = options .outHeight ;
206
+ bitmap = BitmapFactory .decodeResource (getResources (),mImgId );
207
+ }else if (imgId == 0 && bitmap != null ){
208
+ rawWidth = bitmap .getWidth ();
209
+ rawHeight = bitmap .getHeight ();
264
210
}
265
-
266
- }
267
-
268
- private void zipBitmapFromBitmap (Bitmap bitmap ) {
269
- int rawWidth = bitmap .getWidth ();
270
- int rawHeight = bitmap .getHeight ();
271
- int reqWidth = mWeightWidth - mPadding - mPadding ;
272
- int reqHeight = mWeightHeight - mPadding - mPadding ;
211
+ int reqWidth = getWidth () - mPadding - mPadding ;
212
+ int reqHeight = getHeight () - mPadding - mPadding ;
273
213
if (reqHeight <= 0 || reqWidth <= 0 ) return ;
274
214
WeakReference <Matrix > weakMatrix = new WeakReference <Matrix >(new Matrix ());
275
215
if (weakMatrix .get () == null ) return ;
276
216
Matrix matrix = weakMatrix .get ();
277
- if (reqHeight == reqWidth ) {
278
- if (rawWidth == rawHeight ) {
279
- int least = Math .min (rawHeight , reqHeight );
280
- int big = Math .max (rawHeight , reqHeight );
281
- float scale = 0 ;
282
- if (rawHeight > reqHeight ) {
283
- scale = least * 1.0f / big ;
284
- } else {
285
- scale = big * 1.0f / least ;
286
- }
287
- matrix .setScale (scale , scale );
288
- mBitmap = Bitmap .createBitmap (mBitmap , 0 , 0 , rawWidth , rawWidth , matrix , true );
289
- } else if (rawHeight > reqHeight || rawWidth > reqWidth ) {
290
- int dx = 0 ;
291
- int dy = 0 ;
292
- int small = Math .min (rawHeight , rawWidth );
293
- int least = Math .min (small , reqHeight );
294
- float scale = reqHeight * 1.0f / small ;
295
- matrix .setScale (scale , scale );
296
- if (rawHeight > rawWidth ) {
297
- dy = (rawHeight - reqHeight ) / 2 ;
298
- } else {
299
- dx = (rawWidth - reqWidth ) / 2 ;
300
- }
301
- mBitmap = Bitmap .createBitmap (mBitmap , dx , dy , least , least , matrix , true );
302
- }
303
-
304
- } else {
217
+ if (heightNode == 0 ) {
305
218
float scale = rawHeight * 1.0f / rawWidth ;
306
- Log .e (TAG ,"ZIP--scale:" +scale +"--" +rawWidth +"/" +rawHeight +"--" +reqWidth +"/" +reqHeight );
307
- mBitmap = Bitmap .createScaledBitmap (mBitmap , reqWidth , (int ) (reqWidth * scale ), true );
219
+ mRealBitmap = Bitmap .createScaledBitmap (bitmap , reqWidth , (int ) (reqWidth * scale ), true );
220
+ }else {
221
+ int dx = 0 ;
222
+ int dy = 0 ;
223
+ int small = Math .min (rawHeight , rawWidth );
224
+ int big = Math .max (reqWidth , reqHeight );
225
+ float scale = big * 1.0f / small ;
226
+ matrix .setScale (scale ,scale );
227
+ if (rawHeight > rawWidth ) {
228
+ dy = (rawHeight - rawWidth ) / 2 ;
229
+ } else if (rawHeight < rawWidth ) {
230
+ dx = (rawWidth - rawHeight ) / 2 ;
231
+ }
232
+ mRealBitmap = Bitmap .createBitmap (bitmap ,dx , dy , small , small , matrix , true );
308
233
}
234
+ initShadow (bitmap );
309
235
310
236
}
311
237
@@ -475,7 +401,7 @@ public int getLightMutedContentTextColor() {
475
401
return mPalette .getLightMutedSwatch () == null ? 0 : mPalette .getLightMutedSwatch ().getBodyTextColor ();
476
402
}
477
403
478
- public Palette getPalette (){
404
+ public Palette getPalette () {
479
405
return mPalette ;
480
406
}
481
407
0 commit comments