涉及圆角与图片web加载,使用fresco
- 在build.gradle添加依赖
compile 'com.facebook.fresco:fresco:0.14.1'
compile 'com.facebook.fresco:animated-gif:0.14.1'
- 在使用imageview的地方使用自定义组件
<com.facebook.drawee.view.SimpleDraweeView
android:id="@+id/user_image"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_marginBottom="10dp"
android:layout_marginLeft="15dp"
android:layout_marginTop="10dp"
android:scaleType="fitXY"
fresco:roundedCornerRadius="5dp"
fresco:roundingBorderWidth="1dp"
/>
- 调用,传入uri即可
public ImageView userImage;
userImage = (ImageView) view.findViewById(R.id.user_image);
userImage.setImageURI(Uri.parse(userMessage.getAvatar()));
本地更换图片
点击图片更换
- 设置点击事件
userImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
((Activity) context).startActivityForResult(intent, 1);
}
});
- activity中回调
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
//获取图片路径
if (requestCode == 1 && resultCode == Activity.RESULT_OK && data != null) {
Uri selectedImage = data.getData();
String[] filePathColumns = {MediaStore.Images.Media.DATA};
Cursor c = getContentResolver().query(selectedImage, filePathColumns, null, null, null);
c.moveToFirst();
int columnIndex = c.getColumnIndex(filePathColumns[0]);
String imagePath = c.getString(columnIndex); //文件路径
showImage(imagePath);
c.close();
}
}
- 使用ok-go文件上传
private void showImage(String imagePath) {
OkGo.post(Constants.IMAGEHOSTIP+"/upload")//
.tag(this)//
.isMultipart(true) // 强制使用 multipart/form-data 表单上传(只是演示,不需要的话不要设置。默认就是false)
.params("myFile", new File(imagePath)) // 可以添加文件上传
.execute(new StringCallback() {
@Override
public void onSuccess(String s, Call call, Response response) {
//上传成功
//获取图片控件修改图片
FragmentManager fm = getSupportFragmentManager();
ContentFragment fragmentByTag = (ContentFragment) fm.findFragmentByTag(MAIN_CONTENT_TAG);
ContentUserPager basePager = (ContentUserPager) fragmentByTag.basePagers.get(1);
basePager.userMessage.setAvatar(s.substring(8,s.length()-2));
basePager.userImage.setImageURI(Uri.parse(s.substring(8,s.length()-2)));
Toast.makeText(MainActivity.this,"图片更换成功,请保存",Toast.LENGTH_SHORT).show();
}
@Override
public void upProgress(long currentSize, long totalSize, float progress, long networkSpeed) {
super.upProgress(currentSize, totalSize, progress, networkSpeed);
}
@Override
public void onError(Call call, Response response, Exception e) {
super.onError(call, response, e);
Toast.makeText(MainActivity.this,"图片更换失败",Toast.LENGTH_SHORT).show();
}
});
}