2019年8月5号, 新增显示一寸照为原型头像时人脸显示不全问题:
Glide 4.0以上使用
依赖: implementation 'com.github.bumptech.glide:glide:4.8.0'
圆形头像: implementation 'de.hdodenhof:circleimageview:2.2.0'
//图片是在百度上随便找到小姐姐, 换成自己的图片地址, 如果发现bitmap为空, 注意你的图片地址是否正确且用glide是否能加载出来
final String iconUrl_3 = "https://ss0.bdstatic.com/94oJfD_bAAcT8t7mm9GUKT-xh_/timg?image&quality=100&size=b4000_4000&sec=1564996498&di=92e037c0e7ba668706ad7cd7f24cb3d4&src=http://tgi12.jia.com/119/529/19529864.jpg";
//网络请求要放在子线程
new Thread(new Runnable() {
@Override
public void run() {
final Bitmap bitmap = BitmapUtils.getBitmapFromeNetUrl(iconUrl);
final Bitmap bmp = BitmapUtils.cropSquareBitmap(bitmap);
runOnUiThread(new Runnable() {
@Override
public void run() {
if (bmp != null) {
Glide.with(mActivity)
.load(bmp)
.into(ciIcon);
}
}
});
}
}).start();
Glide 3.0使用:
依赖: compile 'com.github.bumptech.glide:glide:3.6.1'
圆形头像: compile 'de.hdodenhof:circleimageview:3.0.0'
//图片是在百度上随便找到小姐姐, 换成自己的图片地址, 如果发现bitmap为空, 注意你的图片地址是否正确且用glide是否能加载出来
final String iconUrl = "https://ss0.bdstatic.com/94oJfD_bAAcT8t7mm9GUKT-xh_/timg?image&quality=100&size=b4000_4000&sec=1564996498&di=92e037c0e7ba668706ad7cd7f24cb3d4&src=http://tgi12.jia.com/119/529/19529864.jpg";
//网络请求要放在子线程
new Thread(new Runnable() {
@Override
public void run() {
final Bitmap bitmap = BitmapUtils.getBitmapFromeNetUrl(iconUrl);
final Bitmap bmp = BitmapUtils.cropSquareBitmap(bitmap);
newPortalFragment.this.getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
if (bmp != null) {
//将bitmap数据转为bytes数组
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, baos);
byte[] bytes = baos.toByteArray();
//Glide加载bytes数组
Glide.with(newPortalFragment.this.getActivity())
.load(bytes)
.dontAnimate()
.diskCacheStrategy(DiskCacheStrategy.RESULT)
.placeholder(R.mipmap.mail_changyong_normal)
.error(R.mipmap.mail_changyong_normal)
.into(cirImg);
}
}
});
}
}).start();
BitmapUtils.java 类源码
/**
* des: Bitmap处理相关操作
* author: geaosu
*/
public class BitmapUtils {
/**
* 截取正方形 - 长图 - 中间位置
*/
public static void getCenterSquare() {
}
/**
* 截取正方形 - 长图 - 结束位置
*/
public static void getEndSquare() {
}
/**
* 按正方形剪裁图片
* 指定正方形边长
*/
public static Bitmap imageCrop(Bitmap bitmap, int width) {
// 得到图片的宽,高
int w = bitmap.getWidth();
int h = bitmap.getHeight();
//width最大不能超过长方形的短边
if (w < width || h < width) {
width = w > h ? h : w;
}
int retX = (w - width) / 2;
int retY = (h - width) / 2;
return Bitmap.createBitmap(bitmap, retX, retY, width, width, null, false);
}
/**
* 按正方形剪裁图片
* 截最大的正方形
*/
public static Bitmap imageCrop(Bitmap bitmap) {
// 得到图片的宽,高
int w = bitmap.getWidth();
int h = bitmap.getHeight();
int width = w > h ? h : w;
int retX = (w - width) / 2;
int retY = (h - width) / 2;
return Bitmap.createBitmap(bitmap, retX, retY, width, width, null, false);
}
/**
* 获取裁剪后的正方形图片的bitmap数据
* author: geaosu
*
* @param bitmap 原图
* @return 裁剪后的图像
*/
public static Bitmap cropSquareBitmap(Bitmap bitmap) {
if (bitmap == null) {
return null;
}
int w = bitmap.getWidth();
int h = bitmap.getHeight();
int width = w >= h ? h : w;
int height = width;
int x = 0;
int y = 40;
if (y + height > h) {
y = 0;
}
return Bitmap.createBitmap(bitmap, x, y, width, height, null, false);
}
/**
* 从图片的网络地址url中获取图片的bitmap数据
*
* @param 图片网络url
* @return bitmap
*/
public static Bitmap getBitmapFromeNetUrl(String imgUrl) {
InputStream inputStream = null;
ByteArrayOutputStream outputStream = null;
URL url = null;
try {
url = new URL(imgUrl);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod("GET");
httpURLConnection.setReadTimeout(2000);
httpURLConnection.connect();
if (httpURLConnection.getResponseCode() == 200) {
Log.d("geaosu", "网络连接成功" + httpURLConnection.getResponseCode());
inputStream = httpURLConnection.getInputStream();
outputStream = new ByteArrayOutputStream();
byte buffer[] = new byte[1024 * 8];
int len = -1;
while ((len = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, len);
}
byte[] bu = outputStream.toByteArray();
Bitmap bitmap = BitmapFactory.decodeByteArray(bu, 0, bu.length);
return bitmap;
} else {
Log.d("geaosu", "网络连接失败" + httpURLConnection.getResponseCode());
}
} catch (Exception e) {
} finally {
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (outputStream != null) {
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return null;
}
}
网友评论