使用方法
- 在项目的build.gradle添加如下代码
allprojects {
repositories {
...
maven { url 'https://www.jitpack.io' }
}
}
- 在app的build.gradle天下如下代码
implementation 'com.github.luqihua:L-Picture:1.0'
- 配置图片显示器(根据不同的图片加载框架配置,可能是
glide
,picasso
,imageloader
),demo使用简单的picasso
public class MyApplication extends Application{
@Override
public void onCreate() {
super.onCreate();
//使用picasso,glide等框架的配置都类似
LPicture.getInstance().init(new IPictureLoader() {
@Override
public ImageView createImageView(Context context) {
return new ImageView(context);
}
@Override
public void displayImage(Context context, ImageView view, String path) {
Picasso.get().load("file://"+path).into(view);
}
});
}
}
- 如果项目中使用的是
fresco
作为图片加载框架,配置代码如下
LPicture.getInstance().init(new IPictureLoader() {
@Override
public ImageView createImageView(Context context) {
return new SimpleDraweeView(context);
}
@Override
public void displayImage(Context context, final ImageView view, String path) {
Uri uri = null;
if (!path.startsWith("http")) {
if (!path.startsWith("file://") && !path.startsWith("content://")) {
path = "file://" + path;
}
}
uri = Uri.parse(path);
if (view instanceof SimpleDraweeView) {
((SimpleDraweeView) view).setImageURI(path);
} else {
ImageRequest request = ImageRequest.fromUri(uri);
ImagePipelineFactory.getInstance()
.getImagePipeline()
.fetchDecodedImage(request, null)
.subscribe(new BaseBitmapDataSubscriber() {
@Override
protected void onNewResultImpl(@Nullable Bitmap bitmap) {
view.setImageBitmap(bitmap);
}
@Override
protected void onFailureImpl(DataSource<CloseableReference<CloseableImage>> dataSource) {
}
}, UiThreadImmediateExecutorService.getInstance());
}
}
});
- 在activity中使用
public class MainActivity extends AppCompatActivity {
private RecyclerView rvImageListV;
private ImageAdapter mAdapter;
private PictureChooseDialog mPhotoDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
if (Build.VERSION.SDK_INT >= 23) {
requestPermissions(new String[]{Manifest.permission.CAMERA,
Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
}
}
private void initView() {
rvImageListV = findViewById(R.id.rv_image_list);
rvImageListV.setLayoutManager(new GridLayoutManager(this, 3));
mAdapter = new ImageAdapter(this);
rvImageListV.setAdapter(mAdapter);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (mPhotoDialog != null) {
mPhotoDialog.onActivityResult(requestCode, resultCode, data);
}
}
public void choose(View v) {
PhotoOptions options = new PhotoOptions.Builder()
.setMaxCount(6)//目标数量
.setCurrentCount(mAdapter.getItemCount())//当前已有数量
.setCompress(true)//选中图片后是否进行压缩
.build();
mPhotoDialog = LPicture.getInstance().createPicDialog(this, options, new IPicCallback() {
@Override
public void success(List<String> paths) {
mAdapter.update(paths);
}
@Override
public void error(String msg) {
Toast.makeText(MainActivity.this, msg, Toast.LENGTH_SHORT).show();
}
});
mPhotoDialog.show();
}
}
github地址
源码和demo在github可查看,整体来说使用还是挺简单的