我們來先看效果圖
概述
關于手機圖片加載器蕴掏,在當今像素隨隨便便破千萬的時代,一張圖片占據(jù)的內(nèi)存都相當可觀伴奥,作為高大尚程序猿的我們,有必要掌握圖片的壓縮翼闽,緩存等處理拾徙,以到達縱使你有萬張照片,縱使你的像素再高感局,我們也能正確的顯示所有的圖片尼啡。
簡單描述一下
1.顯示手機上所有的圖片
2.選中時候圖片變暗。
3.可多選
實現(xiàn)
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
compile 'com.android.support:recyclerview-v7:26.0.0-alpha1'
compile 'com.github.bumptech.glide:glide:3.6.1'
這里我們沒有寫6.0獲取權限 自行代碼獲取
1.既然我們需要顯示手機上的所有圖片询微,那么我們首先就需要拿到手機上的所有圖片地址,當然這個過程在子線程理的崖瞭,當 獲取完成后通過handler發(fā)送完成消息
private void getImages() {
new Thread(new Runnable() {
@Override
public void run() {
fileNames = new ArrayList();
Cursor cursor = getContentResolver().query(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, null, null, null, null);
if (cursor == null) {
Log.i(TAG, "run: 空");
return;
}
while (cursor.moveToNext()) {
//獲取圖片的地址
byte[] data = cursor.getBlob(cursor.getColumnIndex(MediaStore.Images.Media.DATA));
fileNames.add(new String(data, 0, data.length - 1));
Log.i(TAG, "run: " + new String(data, 0, data.length - 1));
}
mHandler.sendMessage(mHandler.obtainMessage());
cursor.close();
}
}).start();
}
Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
Log.i(TAG, "handleMessage: 完成");
mRv.setAdapter(mPhotoPickerAdapter = new PhotoPickerAdapter(fileNames));
}
};
2.這個時候我們已經(jīng)有了所有圖片的地址接下來我們需要顯示圖片了,我們看到效果撑毛,每行有3個书聚,并且條目之間是有間距的,我這里使用的是RecyclerView藻雌,
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_photo);
mRv = findViewById(R.id.rv);
mRv.setLayoutManager(new GridLayoutManager(this, 3));
mRv.addItemDecoration(new SpaceItemDecoration(5));
getImages();
}
public class SpaceItemDecoration extends RecyclerView.ItemDecoration {
private int space;
public SpaceItemDecoration(int space) {
this.space = space;
}
/**這里定義的間距*/
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
outRect.left = space;
outRect.right = space;
outRect.bottom = space;
outRect.top = space;
}
}
3.這個時候我們RecyclerView 基本配置配置完成雌续。接下來我們看下adapter,從下列代碼我們可以看到,我這里直接采用的是谷歌推薦的圖片加載Glide加載圖片的蹦疑,圖片選中的時候調(diào)用setAlpha 設置透明度西雀。
這里提供了一個getImages 方法。 取到所有的已經(jīng)選中圖片的地址歉摧;
public class PhotoPickerAdapter extends RecyclerView.Adapter<PhotoPickerAdapter.MyViewHolder> {
private ArrayList<String> images;
private ArrayList<Boolean> cb;
private Context mContext;
public PhotoPickerAdapter(ArrayList<String> images){
this.images=images;
cb=new ArrayList<>();
for (int i = 0; i < images.size(); i++) {
cb.add(false);
}
}
public ArrayList<String> getImages(){
ArrayList<String> strings =new ArrayList<>();
for (int i = 0; i < cb.size(); i++) {
if(cb.get(i)){
strings.add(images.get(i));
}
}
return strings;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
mContext=parent.getContext();
MyViewHolder holder = new MyViewHolder(LayoutInflater.from(
mContext).inflate(R.layout.photo_item, parent,
false));
return holder;
}
@Override
public void onBindViewHolder(final MyViewHolder holder, final int position) {
Glide.with(mContext)
.load(images.get(position))
.crossFade()
.into(holder.iv);
holder.cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
cb.set(position,b);
holder.iv.setAlpha(cb.get(position)==true?0.5f:1f);
}
});
holder.cb.setChecked(cb.get(position));
holder.iv.setAlpha(cb.get(position)==true?0.5f:1f);
}
@Override
public int getItemCount() {
return images.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder{
public ImageView iv;
public CheckBox cb;
public MyViewHolder(View itemView) {
super(itemView);
iv=itemView.findViewById(R.id.iv);
cb=itemView.findViewById(R.id.cb);
}
}
}
完整代碼
PhotoPickerActivity
public class PhotoPickerActivity extends Activity implements View.OnClickListener {
private static final String TAG = "PhotoPickerActivity";
private ArrayList<String> fileNames;
private PhotoPickerAdapter mPhotoPickerAdapter;
Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
Log.i(TAG, "handleMessage: 完成");
mRv.setAdapter(mPhotoPickerAdapter = new PhotoPickerAdapter(fileNames));
}
};
private RecyclerView mRv;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_photo);
mRv = findViewById(R.id.rv);
mRv.setLayoutManager(new GridLayoutManager(this, 3));
mRv.addItemDecoration(new SpaceItemDecoration(5));
getImages();
}
private void getImages() {
new Thread(new Runnable() {
@Override
public void run() {
fileNames = new ArrayList();
Cursor cursor = getContentResolver().query(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, null, null, null, null);
if (cursor == null) {
Log.i(TAG, "run: 空");
return;
}
while (cursor.moveToNext()) {
//獲取圖片的地址
byte[] data = cursor.getBlob(cursor.getColumnIndex(MediaStore.Images.Media.DATA));
fileNames.add(new String(data, 0, data.length - 1));
Log.i(TAG, "run: " + new String(data, 0, data.length - 1));
}
mHandler.sendMessage(mHandler.obtainMessage());
cursor.close();
}
}).start();
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.confirm:
StringBuffer stringBuffer = new StringBuffer();
stringBuffer.append("選中的圖片"+"\r\n");
ArrayList<String> arrayList = mPhotoPickerAdapter.getImages();
for (int i = 0; i < arrayList.size(); i++) {
String s=arrayList.get(i);
String[] strs= s.split("/");
stringBuffer.append(strs[strs.length-1]+"\r\n");
}
Toast.makeText(PhotoPickerActivity.this,stringBuffer.toString(),Toast.LENGTH_SHORT).show();
break;
}
}
}
activity_photo
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:layout_weight="1"
android:background="#272727"
android:id="@+id/rv"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v7.widget.RecyclerView>
<RelativeLayout
android:background="@android:color/white"
android:layout_width="match_parent"
android:layout_height="40dp">
<TextView
android:onClick="onClick"
android:id="@+id/confirm"
android:background="#0094ff"
android:layout_alignParentRight="true"
android:layout_margin="5dp"
android:text="確定"
android:textColor="#844200"
android:gravity="center"
android:textSize="14sp"
android:layout_width="60dp"
android:layout_height="match_parent"/>
</RelativeLayout>
</LinearLayout>
SpaceItemDecoration
public class SpaceItemDecoration extends RecyclerView.ItemDecoration {
private int space;
public SpaceItemDecoration(int space) {
this.space = space;
}
@Override
public void getItemOffsets(Rect outRect, View view,
RecyclerView parent, RecyclerView.State state) {
outRect.left = space;
outRect.right = space;
outRect.bottom = space;
// Add top margin only for the first item to avoid double space between items
// if(parent.getChildPosition(view) != 0)
outRect.top = space;
}
}
PhotoPickerAdapter
public class PhotoPickerAdapter extends RecyclerView.Adapter<PhotoPickerAdapter.MyViewHolder> {
private ArrayList<String> images;
private ArrayList<Boolean> cb;
private Context mContext;
public PhotoPickerAdapter(ArrayList<String> images){
this.images=images;
cb=new ArrayList<>();
for (int i = 0; i < images.size(); i++) {
cb.add(false);
}
}
public ArrayList<String> getImages(){
ArrayList<String> strings =new ArrayList<>();
for (int i = 0; i < cb.size(); i++) {
if(cb.get(i)){
strings.add(images.get(i));
}
}
return strings;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
mContext=parent.getContext();
MyViewHolder holder = new MyViewHolder(LayoutInflater.from(
mContext).inflate(R.layout.photo_item, parent,
false));
return holder;
}
@Override
public void onBindViewHolder(final MyViewHolder holder, final int position) {
Glide.with(mContext)
.load(images.get(position))
.crossFade()
.into(holder.iv);
holder.cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
cb.set(position,b);
holder.iv.setAlpha(cb.get(position)==true?0.5f:1f);
}
});
holder.cb.setChecked(cb.get(position));
holder.iv.setAlpha(cb.get(position)==true?0.5f:1f);
}
@Override
public int getItemCount() {
return images.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder{
public ImageView iv;
public CheckBox cb;
public MyViewHolder(View itemView) {
super(itemView);
iv=itemView.findViewById(R.id.iv);
cb=itemView.findViewById(R.id.cb);
}
}
}
photo_item
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:descendantFocusability="blocksDescendants"
android:layout_width="match_parent"
android:layout_height="120dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:scaleType="centerCrop"
android:id="@+id/iv"
android:layout_width="match_parent"
android:layout_height="120dp"/>
<CheckBox
android:layout_marginTop="5dp"
android:layout_marginRight="5dp"
android:layout_alignParentRight="true"
android:id="@+id/cb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</RelativeLayout>
</LinearLayout>