Skip to content

Commit 89da558

Browse files
committed
commit
1 parent 7c1f957 commit 89da558

File tree

5 files changed

+64
-133
lines changed

5 files changed

+64
-133
lines changed

app/src/main/java/com/dingmouren/smaple/paletteimageview/Constant.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
public class Constant {
88
public static int[] mImgs = new int[]{
9+
// R.mipmap.ic_launcher,
10+
// R.mipmap.test2
911
R.drawable.img1,
1012
R.drawable.img2,
1113
R.drawable.img3,

app/src/main/java/com/dingmouren/smaple/paletteimageview/SampleTwoActivity.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ public void bindData(int imgId){
7474
mLinearLayout.setBackgroundColor(mPaletteImageView.mMainColor);
7575
title.setTextColor(mPaletteImageView.getLightMutedTitleTextColor());
7676
content.setTextColor(mPaletteImageView.getLightMutedContentTextColor());
77+
Log.e("check:",mPaletteImageView.mMainColor+"--"+mPaletteImageView.getLightMutedTitleTextColor()+"--"+mPaletteImageView.getLightMutedContentTextColor());
7778
}
7879
}
7980
}

app/src/main/res/layout/activity_main.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@
1717
<com.dingmouren.paletteimageview.PaletteImageView
1818
android:id="@+id/palette"
1919
android:layout_width="300dp"
20-
android:layout_height="wrap_content"
20+
android:layout_height="300dp"
2121
android:layout_gravity="center_horizontal"
2222
app:palettePadding="20dp"
23-
app:paletteSrc="@drawable/img2" />
23+
app:paletteSrc="@mipmap/ic_launcher" />
2424

2525
<LinearLayout
2626
android:layout_width="match_parent"

app/src/main/res/layout/item_sample_two.xml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,22 @@
55
android:layout_height="wrap_content"
66
xmlns:app="http://schemas.android.com/apk/res-auto"
77
android:orientation="vertical"
8+
android:padding="5dp"
89
>
910

1011
<com.dingmouren.paletteimageview.PaletteImageView
1112
android:id="@+id/palette"
1213
android:layout_width="match_parent"
1314
android:layout_height="wrap_content"
14-
app:palettePadding="10dp"
15+
app:palettePadding="0dp"
1516
/>
1617
<LinearLayout
1718
android:id="@+id/linear"
1819
android:layout_width="match_parent"
1920
android:layout_height="wrap_content"
2021
android:orientation="vertical"
2122
android:padding="10dp"
23+
android:visibility="visible"
2224
>
2325
<TextView
2426
android:id="@+id/tv1"

paletteimageview/src/main/java/com/dingmouren/paletteimageview/PaletteImageView.java

Lines changed: 56 additions & 130 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,11 @@ public class PaletteImageView extends View implements ViewTreeObserver.OnGlobalL
4747
private int mOffsetY = DEFAULT_OFFSET;
4848
private int mShadowRadius = DEFAULT_SHADOW_RADIUS;
4949
private Palette mPalette;
50-
private BitmapFactory.Options options;
51-
private int mWeightWidth, mWeightHeight;
5250
private RectF mRectFShadow;
51+
private Bitmap mRealBitmap;
52+
private int mOnMeasureHeightMode = -1;
53+
54+
5355

5456
private Handler mHandler = new Handler() {
5557
@Override
@@ -99,35 +101,27 @@ protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
99101
int width = MeasureSpec.getSize(widthMeasureSpec);
100102
int widthMode = MeasureSpec.getMode(widthMeasureSpec);
101103
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) {
115106
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;
117111
}
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;
122112
}
123113
setMeasuredDimension(width, height);
124-
125114
}
126115

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+
}
127121

128122
@Override
129123
protected void onDraw(Canvas canvas) {
130-
if (mBitmap != null) {
124+
if (mRealBitmap != null) {
131125
if (mRectFShadow == null) {
132126
WeakReference<RectF> weakRectF = new WeakReference<RectF>(new RectF(mPadding, mPadding, getWidth() - mPadding, getHeight() - mPadding));
133127
if (weakRectF.get() == null) return;
@@ -137,12 +131,16 @@ protected void onDraw(Canvas canvas) {
137131
int id1 = canvas.saveLayer(0, 0, canvas.getWidth(), canvas.getHeight(), null, Canvas.ALL_SAVE_FLAG);
138132
canvas.drawRoundRect(mRectFShadow, mRadius, mRadius, mPaintShadow);
139133
canvas.restoreToCount(id1);
140-
canvas.drawBitmap(createRoundConerImage(mBitmap, mRadius), mPadding, mPadding, null);
134+
canvas.drawBitmap(createRoundConerImage(mRealBitmap, mRadius), mPadding, mPadding, null);
141135
if (mMainColor != -1) mAsyncTask.cancel(true);
142136
}
143137

144138
}
145139

140+
@Override
141+
public void onGlobalLayout() {
142+
}
143+
146144
private Bitmap createRoundConerImage(Bitmap source, int radius) {
147145
WeakReference<Paint> weakPaint = new WeakReference<Paint>(new Paint());
148146
if (weakPaint.get() == null) return null;
@@ -157,7 +155,6 @@ private Bitmap createRoundConerImage(Bitmap source, int radius) {
157155
if (weakCanvas.get() == null) return null;
158156
Canvas canvas = weakCanvas.get();
159157
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);
161158
if (weakRectF.get() == null) return null;
162159
RectF rect = weakRectF.get();
163160
canvas.drawRoundRect(rect, radius, radius, paint);
@@ -167,17 +164,6 @@ private Bitmap createRoundConerImage(Bitmap source, int radius) {
167164
return target;
168165
}
169166

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-
}
181167

182168
@Override
183169
protected void onAttachedToWindow() {
@@ -193,119 +179,59 @@ protected void onAttachedToWindow() {
193179
*/
194180
public void setBitmap(Bitmap bitmap) {
195181
this.mBitmap = bitmap;
196-
initShadow(mBitmap);
197182
}
198183

199184
/**
200185
* 初始化阴影颜色
201186
*/
202187
private void initShadow(Bitmap bitmap) {
203-
if (mBitmap != null) {
188+
if (bitmap != null) {
204189
mAsyncTask = Palette.from(bitmap).generate(paletteAsyncListener);
205190
}
206191
}
207192

208193

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-
}
255194

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();
264210
}
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;
273213
if (reqHeight <= 0 || reqWidth <= 0) return;
274214
WeakReference<Matrix> weakMatrix = new WeakReference<Matrix>(new Matrix());
275215
if (weakMatrix.get() == null) return;
276216
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) {
305218
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);
308233
}
234+
initShadow(bitmap);
309235

310236
}
311237

@@ -475,7 +401,7 @@ public int getLightMutedContentTextColor() {
475401
return mPalette.getLightMutedSwatch() == null ? 0 : mPalette.getLightMutedSwatch().getBodyTextColor();
476402
}
477403

478-
public Palette getPalette(){
404+
public Palette getPalette() {
479405
return mPalette;
480406
}
481407

0 commit comments

Comments
 (0)