解决微信分享图片太大无法分享和出现黑框问题,优化界面
This commit is contained in:
@ -4,7 +4,9 @@ import android.app.Activity;
|
||||
import android.content.ClipboardManager;
|
||||
import android.content.Context;
|
||||
import android.graphics.Bitmap;
|
||||
import android.graphics.Canvas;
|
||||
import android.graphics.Color;
|
||||
import android.graphics.Matrix;
|
||||
import android.os.Bundle;
|
||||
import android.support.v7.widget.GridLayoutManager;
|
||||
import android.support.v7.widget.RecyclerView;
|
||||
@ -34,6 +36,7 @@ import com.tencent.tauth.IUiListener;
|
||||
import com.tencent.tauth.Tencent;
|
||||
import com.tencent.tauth.UiError;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
|
||||
@ -455,7 +458,15 @@ public class ShareUtils {
|
||||
|
||||
@Override
|
||||
public void onLoadingComplete(String s, View view, Bitmap bitmap) {
|
||||
msg.thumbData = Util.bmpToByteArray(bitmap, false);
|
||||
String imgType = shareIcon.substring(shareIcon.length() - 3);
|
||||
Bitmap resultBp;
|
||||
if ("png".equals(imgType)){
|
||||
resultBp = addBackGround(bitmap);
|
||||
}else {
|
||||
resultBp = compressBitmap(bitmap);
|
||||
}
|
||||
|
||||
msg.thumbData = Util.bmpToByteArray(resultBp, true);
|
||||
api.sendReq(req);
|
||||
}
|
||||
|
||||
@ -466,4 +477,50 @@ public class ShareUtils {
|
||||
});
|
||||
}
|
||||
|
||||
//添加背景,防止图片格式为PNG的图片分享出现黑框问题
|
||||
private Bitmap addBackGround(Bitmap result) {
|
||||
Bitmap bgBitmap;
|
||||
int[] colors = new int[result.getWidth()*result.getHeight()];
|
||||
for (int i = 0; i < colors.length; i++) {
|
||||
colors[i] = Color.WHITE;
|
||||
}
|
||||
bgBitmap = Bitmap.createBitmap(colors, result.getWidth(), result.getHeight(), Bitmap.Config.ARGB_8888);
|
||||
|
||||
Bitmap newmap = Bitmap
|
||||
.createBitmap(result.getWidth(), result.getHeight(), Bitmap.Config.ARGB_8888);
|
||||
Canvas canvas = new Canvas(newmap);
|
||||
canvas.drawBitmap(bgBitmap, 0, 0, null);
|
||||
canvas.drawBitmap(result, (result.getHeight() - result.getWidth()) / 2,
|
||||
(result.getHeight() - result.getWidth()) / 2, null);
|
||||
canvas.save(Canvas.ALL_SAVE_FLAG);
|
||||
canvas.restore();
|
||||
return newmap;
|
||||
}
|
||||
|
||||
//压缩图片
|
||||
private Bitmap compressBitmap(Bitmap bitmap) {
|
||||
ByteArrayOutputStream bos = new ByteArrayOutputStream();
|
||||
bitmap.compress(Bitmap.CompressFormat.JPEG, 85, bos);
|
||||
float zoom = (float) Math.sqrt(10 * 1024 / (float) bos.toByteArray().length);
|
||||
|
||||
Matrix matrix = new Matrix();
|
||||
matrix.setScale(zoom, zoom);
|
||||
|
||||
Bitmap result = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
|
||||
bos.reset();
|
||||
|
||||
result.compress(Bitmap.CompressFormat.JPEG, 85, bos);
|
||||
|
||||
while(bos.toByteArray().length > 10 * 1024){
|
||||
System.out.println(bos.toByteArray().length);
|
||||
matrix.setScale(0.9f, 0.9f);
|
||||
result = Bitmap.createBitmap(result, 0, 0, result.getWidth(), result.getHeight(), matrix, true);
|
||||
bos.reset();
|
||||
result.compress(Bitmap.CompressFormat.JPEG, 85, bos);
|
||||
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user