是基于开源项目实现的
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="fly.MainActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:text="返回结果(Results)"
android:textColor="@android:color/black"
android:textStyle="bold" />
<cn.finalteam.galleryfinal.widget.HorizontalListView
android:id="@+id/lv_photo"
android:layout_width="wrap_content"
android:layout_height="100dp"
android:cacheColorHint="@android:color/transparent"
android:clipToPadding="false"
android:fadingEdge="none"
android:horizontalSpacing="2dp"
android:numColumns="3"
android:verticalSpacing="2dp" />
</LinearLayout>
适配器的布局adapter_photo_list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="@+id/iv_photo"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="4dp"
android:src="@drawable/ic_gf_default_photo"
android:scaleType="centerCrop" />
<ImageView
android:id="@+id/iv_photo_delete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top|right"
android:src="@drawable/delete"/>
</FrameLayout>
MainActivity
public class MainActivity extends AppCompatActivity implements ChoosePhotoListAdapter.OnClickPhotoListener{
private final int REQUEST_CODE_CAMERA = 1000;
private final int REQUEST_CODE_GALLERY = 1001;
HorizontalListView mLvPhoto; // 显示图片的ListView
List<PhotoInfo> mPhotoList = new ArrayList<>();
ChoosePhotoListAdapter mChoosePhotoListAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mLvPhoto = (HorizontalListView) findViewById(R.id.lv_photo);
mPhotoList.add(new PhotoInfo());
mChoosePhotoListAdapter = new ChoosePhotoListAdapter(this, mPhotoList,this);
mLvPhoto.setAdapter(mChoosePhotoListAdapter);
// GalleryFinal.cleanCacheFile(); // 清理缓存的代码
}
boolean first = true; // 是否是第一次点击选择图片
private GalleryFinal.OnHanlderResultCallback mOnHanlderResultCallback = new GalleryFinal.OnHanlderResultCallback() {
@Override
public void onHanlderSuccess(int reqeustCode, List<PhotoInfo> resultList) {
if (resultList != null) {
if (resultList.size() > 0) { // 这里是清除刚开始默认的图片
if (first) {
first = false;
mPhotoList.clear();
}
}
Iterator<PhotoInfo> sListIterator = mPhotoList.iterator(); // 删除集合中特定的元素
while(sListIterator.hasNext()){
PhotoInfo e = sListIterator.next();
if(e.getPhotoPath().equals("fly")){
sListIterator.remove();
}
}
mPhotoList.addAll(resultList);
PhotoInfo photoInfo = new PhotoInfo();
photoInfo.setPhotoPath("fly");
mPhotoList.add(photoInfo);
mChoosePhotoListAdapter.notifyDataSetChanged();
}
}
@Override
public void onHanlderFailure(int requestCode, String errorMsg) {
Toast.makeText(MainActivity.this, errorMsg, Toast.LENGTH_SHORT).show();
}
};
@Override
public void onClickPhotoListener() {
ActionSheet.createBuilder(MainActivity.this, getSupportFragmentManager())
.setCancelButtonTitle("取消")
.setOtherButtonTitles("打开相册", "拍照")
.setCancelableOnTouchOutside(true)
.setListener(new ActionSheet.ActionSheetListener() {
@Override
public void onDismiss(ActionSheet actionSheet, boolean isCancel) {}
@Override
public void onOtherButtonClick(ActionSheet actionSheet, int index) {
switch (index) {
case 0:
GalleryFinal.openGalleryMuti(REQUEST_CODE_GALLERY, 4, mOnHanlderResultCallback); // 多选
// GalleryFinal.openGallerySingle(REQUEST_CODE_GALLERY, mOnHanlderResultCallback); // 单选
break;
case 1:
GalleryFinal.openCamera(REQUEST_CODE_CAMERA, mOnHanlderResultCallback); // 打开相机
break;
}
}
})
.show();
}
}
适配器
package com.example.imageselected;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import java.util.List;
import cn.finalteam.galleryfinal.model.PhotoInfo;
import cn.finalteam.toolsfinal.DeviceUtils;
public class ChoosePhotoListAdapter extends BaseAdapter {
private List<PhotoInfo> mList;
private Activity activity;
private int mScreenWidth;
private OnClickPhotoListener mOnClickPhotoListener;
public ChoosePhotoListAdapter(Activity activity, List<PhotoInfo> list, OnClickPhotoListener mOnClickPhotoListener) {
this.mList = list;
this.activity = activity;
this.mOnClickPhotoListener = mOnClickPhotoListener;
this.mScreenWidth = DeviceUtils.getScreenPix(activity).widthPixels;
}
// 这里设置界面最多显示4张
@Override
public int getCount() {
if (mList != null && mList.size() > 0) {
if (mList.size() < 5) {
return mList.size();
}else{
return 4;
}
}else{
return 0;
}
}
@Override
public Object getItem(int position) {
return mList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(activity).inflate(R.layout.adapter_photo_list_item, parent,false);
}
ImageView imageView = (ImageView) convertView.findViewById(R.id.iv_photo);
ImageView delete = (ImageView) convertView.findViewById(R.id.iv_photo_delete);
if (mList != null && mList.size() > 0) {
if (position == mList.size()-1) { // 集合的最后一张显示默认图片,可点击添加图片
delete.setVisibility(View.GONE);
Glide.with(activity)
.load(R.drawable.ic_gf_default_photo)
.placeholder(R.drawable.ic_gf_default_photo)
.error(R.drawable.ic_gf_default_photo)
.diskCacheStrategy(DiskCacheStrategy.RESULT)
.into(imageView);
imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mOnClickPhotoListener.onClickPhotoListener();
}
});
}else{
PhotoInfo photoInfo = mList.get(position);
delete.setOnClickListener(new DeleteClick(position));
Glide.with(activity)
.load("file://" + photoInfo.getPhotoPath())
.placeholder(R.drawable.ic_gf_default_photo)
.error(R.drawable.ic_gf_default_photo)
.diskCacheStrategy(DiskCacheStrategy.RESULT)
.into(imageView);
}
}
return convertView;
}
class DeleteClick implements View.OnClickListener {
private int position;
DeleteClick(int position) {
this.position = position;
}
public void onClick(View v) {
mList.remove(mList.get(position));
notifyDataSetChanged();
}
}
public interface OnClickPhotoListener{
void onClickPhotoListener();
}
}
GlideImageLoader
public class GlideImageLoader implements cn.finalteam.galleryfinal.ImageLoader {
@Override
public void displayImage(Activity activity, String path, final GFImageView imageView, Drawable defaultDrawable, int width, int height) {
Glide.with(activity)
.load("file://" + path)
.placeholder(defaultDrawable)
.error(defaultDrawable)
.override(width, height)
.diskCacheStrategy(DiskCacheStrategy.NONE) //不缓存到SD卡
.skipMemoryCache(true)
//.centerCrop()
.into(new ImageViewTarget<GlideDrawable>(imageView) {
@Override
protected void setResource(GlideDrawable resource) {
imageView.setImageDrawable(resource);
}
@Override
public void setRequest(Request request) {
imageView.setTag(R.id.adapter_item_tag_key,request);
}
@Override
public Request getRequest() {
return (Request) imageView.getTag(R.id.adapter_item_tag_key);
}
});
}
@Override
public void clearMemoryCache() {
}
}
GlidePauseOnScrollListener
package com.example.imageselected.listener;
import com.bumptech.glide.Glide;
import cn.finalteam.galleryfinal.PauseOnScrollListener;
public class GlidePauseOnScrollListener extends PauseOnScrollListener {
public GlidePauseOnScrollListener(boolean pauseOnScroll, boolean pauseOnFling) {
super(pauseOnScroll, pauseOnFling);
}
@Override
public void resume() {
Glide.with(getActivity()).resumeRequests();
}
@Override
public void pause() {
Glide.with(getActivity()).pauseRequests();
}
}
MyApplication
package com.example.imageselected.app;
import android.app.Application;
import com.example.imageselected.listener.GlidePauseOnScrollListener;
import com.example.imageselected.loader.GlideImageLoader;
import cn.finalteam.galleryfinal.CoreConfig;
import cn.finalteam.galleryfinal.FunctionConfig;
import cn.finalteam.galleryfinal.GalleryFinal;
import cn.finalteam.galleryfinal.ThemeConfig;
/**
* 类名:
* 类描述:MyApplication
* 创建人:fly
* 创建日期: 2017/1/9.
* 版本:V1.0
*/
public class MyApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
ThemeConfig theme = new ThemeConfig.Builder().build();
//配置功能
FunctionConfig functionConfig = new FunctionConfig.Builder()
.setEnableCamera(true) // 设置相机
.setEnableEdit(false) // 设置是否可编辑
.setEnableCrop(true) // 设置是否可裁剪
.setEnableRotate(true) // 设置是否可旋转
.setCropSquare(true)
.setEnablePreview(true) // 设置是否可预览
.build();
CoreConfig coreConfig = new CoreConfig.Builder(this, new GlideImageLoader(), theme)
.setFunctionConfig(functionConfig)
.setPauseOnScrollListener(new GlidePauseOnScrollListener(false, true))
.setNoAnimcation(true) // 设置是否显示动画
.build();
GalleryFinal.init(coreConfig);
}
}
清单文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.imageselected">
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<application
android:name=".app.MyApplication"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
build文件
apply plugin: 'com.android.application'
android {
compileSdkVersion 25
buildToolsVersion "25.0.2"
defaultConfig {
applicationId "com.example.imageselected"
minSdkVersion 11
targetSdkVersion 25
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:25.1.0'
testCompile 'junit:junit:4.12'
compile project(':galleryfinal')
compile 'com.github.bumptech.glide:glide:3.7.0'
compile 'com.baoyz.actionsheet:library:1.1.4'
}