Skip to content

Commit 0b7f079

Browse files
committed
commit
1 parent 2b1a26d commit 0b7f079

File tree

6 files changed

+74
-47
lines changed

6 files changed

+74
-47
lines changed

app/src/main/AndroidManifest.xml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,16 @@
99
android:supportsRtl="true"
1010
android:theme="@style/AppTheme">
1111
<activity android:name=".MainActivity">
12-
<intent-filter>
13-
<action android:name="android.intent.action.MAIN" />
1412

15-
<category android:name="android.intent.category.LAUNCHER" />
16-
</intent-filter>
1713
</activity>
1814
<activity android:name=".SampleOneActivity"
1915
android:theme="@style/SampleOneStyle"
2016
>
17+
<intent-filter>
18+
<action android:name="android.intent.action.MAIN" />
2119

20+
<category android:name="android.intent.category.LAUNCHER" />
21+
</intent-filter>
2222
</activity>
2323
<activity android:name=".SampleTwoActivity"
2424
android:theme="@style/SampleTwoStyle"

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
4242
super.onCreate(savedInstanceState);
4343
setContentView(R.layout.activity_sample_one);
4444
window = SampleOneActivity.this.getWindow();
45+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
46+
window.setStatusBarColor(getResources().getColor(R.color.md_teal_A400));
47+
}
4548
initView();
4649
initData();
4750
}
@@ -64,7 +67,6 @@ public void onPageScrolled(int position, float positionOffset, int positionOffse
6467
public void onPageSelected(int position) {
6568
int color = mAdapter.getList().get(position).paletteImageView.mMainColor;
6669
mActionBar.setBackgroundColor(color);
67-
6870
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
6971
window.setStatusBarColor(color);
7072
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public void onActivityCreated(@Nullable Bundle savedInstanceState) {
4040
super.onActivityCreated(savedInstanceState);
4141
if (getArguments() != null){
4242
int imgId = getArguments().getInt(IMG_ID);
43-
paletteImageView.setBitmap(zipBitmap(imgId));
43+
paletteImageView.setBitmap(BitmapFactory.decodeResource(getResources(),imgId));
4444
}
4545
}
4646

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@
77
>
88
<com.dingmouren.paletteimageview.PaletteImageView
99
android:id="@+id/palette"
10-
android:layout_width="match_parent"
10+
android:layout_width="300dp"
1111
android:layout_height="wrap_content"
1212
app:palettePadding="20dp"
1313
app:paletteRadius="10dp"
14+
android:layout_gravity="center_horizontal"
1415
/>
1516
</LinearLayout>

app/src/main/res/values/styles.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
<item name="colorAccent">@color/md_teal_A400</item>
99
</style>
1010
<style name="SampleOneStyle" parent="Theme.AppCompat.Light.NoActionBar">
11-
<item name="android:statusBarColor" tools:ignore="NewApi">@color/md_teal_A400</item>
1211
</style>
1312
<style name="SampleTwoStyle" parent="Theme.AppCompat.Light.NoActionBar">
1413
<item name="android:statusBarColor" tools:ignore="NewApi">@color/md_teal_A400</item>

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

Lines changed: 64 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
import android.support.v7.widget.AppCompatImageView;
2424
import android.widget.Toast;
2525

26+
import java.io.ByteArrayInputStream;
27+
import java.io.ByteArrayOutputStream;
2628
import java.lang.ref.WeakReference;
2729

2830
/**
@@ -53,7 +55,6 @@ public class PaletteImageView extends View implements ViewTreeObserver.OnGlobalL
5355
public PaletteImageView mInstance;
5456

5557

56-
5758
private Handler mHandler = new Handler() {
5859
@Override
5960
public void handleMessage(Message msg) {
@@ -106,22 +107,24 @@ protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
106107
mOnMeasureHeightMode = MeasureSpec.getMode(heightMeasureSpec);
107108
if (mOnMeasureHeightMode == MeasureSpec.UNSPECIFIED) {
108109
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;
110111
}
111-
if (mImgId != 0 && mRealBitmap != null){
112+
if (mImgId != 0 && mRealBitmap != null) {
112113
height = mRealBitmap.getHeight() + mPadding * 2;
113114
}
114115
}
115116
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));
117119
}
118120
setMeasuredDimension(width, height);
119121
}
120122

121123
@Override
122124
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
123125
super.onSizeChanged(w, h, oldw, oldh);
124-
zipBitmap(mImgId,mBitmap,mOnMeasureHeightMode);
126+
mBitmap = ratio(mBitmap);
127+
zipBitmap(mImgId, mBitmap, mOnMeasureHeightMode);
125128
}
126129

127130
@Override
@@ -151,7 +154,7 @@ private Bitmap createRoundConerImage(Bitmap source, int radius) {
151154
Paint paint = weakPaint.get();
152155
paint.setAntiAlias(true);
153156
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));
155158
if (weakBitmap.get() == null) return null;
156159
Bitmap target = weakBitmap.get();
157160

@@ -195,30 +198,30 @@ private void initShadow(Bitmap bitmap) {
195198
}
196199

197200

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;
200209
int rawWidth = 0;
201210
int rawHeight = 0;
202-
if (imgId !=0 && bitmap == null){
211+
if (imgId != 0 && bitmap == null) {
203212
WeakReference<BitmapFactory.Options> weakOptions = new WeakReference<BitmapFactory.Options>(new BitmapFactory.Options());
204213
if (weakOptions.get() == null) return;
205214
BitmapFactory.Options options = weakOptions.get();
206-
options = new BitmapFactory.Options();
207215
BitmapFactory.decodeResource(getResources(), imgId, options);
216+
options.inJustDecodeBounds = true;
208217
rawWidth = options.outWidth;
209218
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) {
212223
rawWidth = bitmap.getWidth();
213224
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){
222225
float scale = rawHeight * 1.0f / rawWidth;
223226
mRealBitmap = Bitmap.createScaledBitmap(bitmap, reqWidth, (int) (reqWidth * scale), true);
224227
initShadow(mRealBitmap);
@@ -227,42 +230,63 @@ private void zipBitmap(int imgId,Bitmap bitmap,int heightNode) {
227230
if (heightNode == 0) {
228231
float scale = rawHeight * 1.0f / rawWidth;
229232
mRealBitmap = Bitmap.createScaledBitmap(bitmap, reqWidth, (int) (reqWidth * scale), true);
230-
}else {
233+
} else {
231234
int dx = 0;
232235
int dy = 0;
233236
int small = Math.min(rawHeight, rawWidth);
234237
int big = Math.max(reqWidth, reqHeight);
235238
float scale = big * 1.0f / small;
236-
matrix.setScale(scale,scale);
239+
matrix.setScale(scale, scale);
237240
if (rawHeight > rawWidth) {
238-
dy = (rawHeight - rawWidth) / 2 ;
241+
dy = (rawHeight - rawWidth) / 2;
239242
} else if (rawHeight < rawWidth) {
240-
dx = (rawWidth - rawHeight) / 2 ;
243+
dx = (rawWidth - rawHeight) / 2;
241244
}
242-
mRealBitmap = Bitmap.createBitmap(bitmap,dx , dy, small, small, matrix, true);
245+
mRealBitmap = Bitmap.createBitmap(bitmap, dx, dy, small, small, matrix, true);
243246
}
247+
if (mRealBitmap != null)
248+
Log.e(TAG, "mRealBitap:" + mRealBitmap.getWidth() + "/" + mRealBitmap.getHeight() + "--"/*(mBitmap.getRowBytes()* mRealBitmap.getHeight() /1024/1024)*/);
244249
initShadow(mRealBitmap);
245250

246251
}
247252

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+
248277
/**
249278
* 计算inSampleSize
250279
*
251280
* @return
252281
*/
253282
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;
263289
}
264-
} catch (Exception e) {
265-
e.printStackTrace();
266290
}
267291
return inSampleSize;
268292
}
@@ -317,20 +341,21 @@ public void onGenerated(Palette palette) {
317341
mMainColor = palette.getDominantSwatch().getRgb();
318342
mHandler.sendEmptyMessage(MSG);
319343
if (mListener != null) mListener.onComplete(mInstance);
320-
}else {
344+
} else {
321345
if (mListener != null) mListener.onFail();
322346
}
323347
}
324348
};
325349

326350
private OnParseColorListener mListener;
327351

328-
public void setOnParseColorListener(OnParseColorListener listener){
352+
public void setOnParseColorListener(OnParseColorListener listener) {
329353
this.mListener = listener;
330354
}
331355

332-
public interface OnParseColorListener{
356+
public interface OnParseColorListener {
333357
void onComplete(PaletteImageView paletteImageView);
358+
334359
void onFail();
335360
}
336361

0 commit comments

Comments
 (0)