android 设置头像(PictureSelectorCircleImageView等库结合极光IM服务)
Posted 孤注一掷 、
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了android 设置头像(PictureSelectorCircleImageView等库结合极光IM服务)相关的知识,希望对你有一定的参考价值。
使用PictureSelector、CircleImageView等库结合极光IM服务,实现头像的设置和获取(仅图库选取照片)
PictureSelector的引入和使用可参考 https://github.com/frank5552012/PictureSelector
极光文档头像:
1.用到的依赖
implementation ('com.github.LuckSiege.PictureSelector:picture_library:v2.5.9')//图库选择
implementation ('de.hdodenhof:circleimageview:2.1.0')//放圆形头像的控件
implementation 'com.android.support:support-v4:29.4.1'//解决了报错问题,Failed resolution of: Landroidx/localbroadcastmanager/content/LocalBroadcastManager
implementation 'com.github.bumptech.glide:glide:4.11.0'
2.放头像的控件 CircleImageView,圆形遮罩,将图片显示成圆形。
<de.hdodenhof.circleimageview.CircleImageView
android:id="@+id/imageView_profile"
android:layout_width="80dp"
android:layout_height="80dp"
android:src="@drawable/icon_boy" />
3.对图片设置点击事件
private Integer Pic_OK=1;
circleImageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//选图片
PictureSelector.create(MineFragment.this)
.openGallery(PictureMimeType.ofImage())
.loadImageEngine(GlideEngine.createGlideEngine())// 外部传入图片加载引擎,必传项
.maxSelectNum(1)
.minSelectNum(1)
.selectionMode(PictureConfig.SINGLE)
.previewImage(true)
.compress(true)
.minimumCompressSize(100)// 小于100kb的图片不压缩
.forResult(Pic_OK);//结果回调onActivityResult code
}
});
3.这里用到了GlideEngine.java 图片加载引擎,省略的话图片会显示不出来,所以新建一个,代码如下:
public class GlideEngine implements ImageEngine {
/**
* 加载图片
*
* @param context
* @param url
* @param imageView
*/
@Override
public void loadImage(@NonNull Context context, @NonNull String url, @NonNull ImageView imageView) {
Glide.with(context)
.load(url)
.into(imageView);
}
/**
* 加载网络图片适配长图方案
* # 注意:此方法只有加载网络图片才会回调
*
* @param context
* @param url
* @param imageView
* @param longImageView
* @param callback 网络图片加载回调监听 {link after version 2.5.1 Please use the #OnImageCompleteCallback#}
*/
@Override
public void loadImage(@NonNull Context context, @NonNull String url,
@NonNull ImageView imageView,
SubsamplingScaleImageView longImageView, OnImageCompleteCallback callback) {
Glide.with(context)
.asBitmap()
.load(url)
.into(new ImageViewTarget<Bitmap>(imageView) {
@Override
public void onLoadStarted(@Nullable Drawable placeholder) {
super.onLoadStarted(placeholder);
if (callback != null) {
callback.onShowLoading();
}
}
@Override
public void onLoadFailed(@Nullable Drawable errorDrawable) {
super.onLoadFailed(errorDrawable);
if (callback != null) {
callback.onHideLoading();
}
}
@Override
protected void setResource(@Nullable Bitmap resource) {
if (callback != null) {
callback.onHideLoading();
}
if (resource != null) {
boolean eqLongImage = MediaUtils.isLongImg(resource.getWidth(),
resource.getHeight());
longImageView.setVisibility(eqLongImage ? View.VISIBLE : View.GONE);
imageView.setVisibility(eqLongImage ? View.GONE : View.VISIBLE);
if (eqLongImage) {
// 加载长图
longImageView.setQuickScaleEnabled(true);
longImageView.setZoomEnabled(true);
longImageView.setPanEnabled(true);
longImageView.setDoubleTapZoomDuration(100);
longImageView.setMinimumScaleType(SubsamplingScaleImageView.SCALE_TYPE_CENTER_CROP);
longImageView.setDoubleTapZoomDpi(SubsamplingScaleImageView.ZOOM_FOCUS_CENTER);
longImageView.setImage(ImageSource.bitmap(resource),
new ImageViewState(0, new PointF(0, 0), 0));
} else {
// 普通图片
imageView.setImageBitmap(resource);
}
}
}
});
}
/**
* 加载网络图片适配长图方案
* # 注意:此方法只有加载网络图片才会回调
*
* @param context
* @param url
* @param imageView
* @param longImageView
* @ 已废弃
*/
@Override
public void loadImage(@NonNull Context context, @NonNull String url,
@NonNull ImageView imageView,
SubsamplingScaleImageView longImageView) {
Glide.with(context)
.asBitmap()
.load(url)
.into(new ImageViewTarget<Bitmap>(imageView) {
@Override
protected void setResource(@Nullable Bitmap resource) {
if (resource != null) {
boolean eqLongImage = MediaUtils.isLongImg(resource.getWidth(),
resource.getHeight());
longImageView.setVisibility(eqLongImage ? View.VISIBLE : View.GONE);
imageView.setVisibility(eqLongImage ? View.GONE : View.VISIBLE);
if (eqLongImage) {
// 加载长图
longImageView.setQuickScaleEnabled(true);
longImageView.setZoomEnabled(true);
longImageView.setPanEnabled(true);
longImageView.setDoubleTapZoomDuration(100);
longImageView.setMinimumScaleType(SubsamplingScaleImageView.SCALE_TYPE_CENTER_CROP);
longImageView.setDoubleTapZoomDpi(SubsamplingScaleImageView.ZOOM_FOCUS_CENTER);
longImageView.setImage(ImageSource.bitmap(resource),
new ImageViewState(0, new PointF(0, 0), 0));
} else {
// 普通图片
imageView.setImageBitmap(resource);
}
}
}
});
}
/**
* 加载相册目录
*
* @param context 上下文
* @param url 图片路径
* @param imageView 承载图片ImageView
*/
@Override
public void loadFolderImage(@NonNull Context context, @NonNull String url, @NonNull ImageView imageView) {
Glide.with(context)
.asBitmap()
.load(url)
.override(180, 180)
.centerCrop()
.sizeMultiplier(0.5f)
.apply(new RequestOptions().placeholder(R.drawable.picture_image_placeholder))
.into(new BitmapImageViewTarget(imageView) {
@Override
protected void setResource(Bitmap resource) {
RoundedBitmapDrawable circularBitmapDrawable =
RoundedBitmapDrawableFactory.
create(context.getResources(), resource);
circularBitmapDrawable.setCornerRadius(8);
imageView.setImageDrawable(circularBitmapDrawable);
}
});
}
/**
* 加载gif
*
* @param context 上下文
* @param url 图片路径
* @param imageView 承载图片ImageView
*/
@Override
public void loadAsGifImage(@NonNull Context context, @NonNull String url,
@NonNull ImageView imageView) {
Glide.with(context)
.asGif()
.load(url)
.into(imageView);
}
/**
* 加载图片列表图片
*
* @param context 上下文
* @param url 图片路径
* @param imageView 承载图片ImageView
*/
@Override
public void loadGridImage(@NonNull Context context, @NonNull String url, @NonNull ImageView imageView) {
Glide.with(context)
.load(url)
.override(200, 200)
.centerCrop()
.apply(new RequestOptions().placeholder(R.drawable.picture_image_placeholder))
.into(imageView);
}
private GlideEngine() {
}
private static GlideEngine instance;
public static GlideEngine createGlideEngine() {
if (null == instance) {
synchronized (GlideEngine.class) {
if (null == instance) {
instance = new GlideEngine();
}
}
}
return instance;
}
}
4.回调结果,当请求码为Pic_OK时会执行逻辑代码,将图片的具体地址上传到极光服务器,同时进行头像获取和UI更新
private List<LocalMedia> selectList = new ArrayList<>();
@Override
public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {//回调结果
super.onActivityResult(requestCode, resultCode, data);
//当请求码是图库时
if(requestCode==Pic_OK){
selectList = PictureSelector.obtainMultipleResult(data);
if (selectList != null && selectList.size() == 1 && selectList.get(0) != null){
Log.i(TAG, "onActivityResult: "+selectList.get(0).getCompressPath());
JMessageClient.updateUserAvatar(new File(selectList.get(0).getCompressPath()), new BasicCallback() {
@Override
public void gotResult(int i, String s) {
if (i==0){
ToastUtil.showToast(getActivity(),"更新头像成功!");
Log.i(TAG, "头像gotResult: "+i);
UserInfo myInfo = JMessageClient.getMyInfo();
Log.i(TAG, "个人信息gotResult: "+myInfo.toString());
myInfo.getAvatarBitmap(new GetAvatarBitmapCallback() {
@Override
public void gotResult(int i, String s, Bitmap bitmap) {
if (i==0){
circleImageView.setImageBitmap(bitmap);
Log.i(TAG, "头像gotResult: UI更新成功");
}
}
});
//包括裁剪和压缩后的缓存,要在上传成功后调用,type 指的是图片or视频缓存取决于你设置的ofImage或ofVideo 注意:需要系统sd卡权限
PictureFileUtils.deleteCacheDirFile(getContext(),PictureMimeType.ofImage());
// 清除所有缓存 例如:压缩、裁剪、视频、音频所生成的临时文件
PictureFileUtils.deleteAllCacheDirFile(getContext());
}else {
ToastUtil.showToast(getActivity(),"更新头像失败!");
Log.i(TAG, "头像gotResult: "+i);
}
}
});
}
ToastUtil.showToast(getActivity(),"选择图片成功!");
}
}
5.在oncreate中初始化头像,就可以跳转此页面显示头像
//初始化显示用户头像
private void initAvatar() {
UserInfo myInfo = JMessageClient.getMyInfo();
Log.i(TAG, "个人信息gotResult: "+myInfo.toString());
myInfo.getAvatarBitmap(new GetAvatarBitmapCallback() {
@Override
public void gotResult(int i, String s, Bitmap bitmap) {
if (i==0){
circleImageView.setImageBitmap(bitmap);
Log.i(TAG, "头像gotResult: UI更新成功");
}
}
});
}
以上是关于android 设置头像(PictureSelectorCircleImageView等库结合极光IM服务)的主要内容,如果未能解决你的问题,请参考以下文章
Android自定义控件实例,圆形头像(图库 + 裁剪+设置),上传头像显示为圆形,附源码