Android ImageSelector微信圖片選擇器
前言
現(xiàn)在絕大多數(shù)的App都上傳圖片的功能,比如設置用戶頭像、聊天發(fā)送圖片、發(fā)表動態(tài)、論壇帖子等。上傳圖片需要先從選擇手機中選擇要上傳的圖片,所以圖片選擇器在App中是很常見的組件,一般的手機都會自帶一個圖片選擇器。不過很多App并不喜歡用手機自帶的選擇器,而是自己實現(xiàn)一個圖片選擇器。
比如微信的圖片選擇器就做的很好。沒辦法,誰讓微信這么強大,我不超抄襲你,但是,我可以模仿你。
效果圖

是不是和真的一樣,哈哈,不過,作者的唯一缺陷就是沒有提供拍照,唉,有一點遺憾,但是,這個就夠用了!
思路
1.從手機存儲卡中掃描加載圖片。
2.用一個列表將圖片顯示出來。
3.選擇圖片。
4.把選中的圖片返回給調(diào)用者。
準備工作
引入依賴
//在Project的build.gradle在添加以下代碼
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
// 如果你使用的是1.4.0或更早的版本,這句可以不用。
maven { url 'https://maven.google.com' }
}
}
//在Module的build.gradle在添加以下代碼 compile 'com.github.donkingliang:ImageSelector:1.5.0'
配置AndroidManifest.xml
//儲存卡的讀取權限 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> //圖片選擇Activity <activity android:name="com.donkingliang.imageselector.ImageSelectorActivity" //去掉Activity的ActionBar。 //使用者可以根據(jù)自己的項目去配置,不一定要這樣寫,只要不Activity的ActionBar去掉就可以了。 android:theme="@style/Theme.AppCompat.Light.NoActionBar" //橫豎屏切換處理。 //如果要支持橫豎屏切換,一定要加上這句,否則在切換橫豎屏的時候會發(fā)生異常。 android:configChanges="orientation|keyboardHidden|screenSize"/> //圖片預覽Activity <activity android:name="com.donkingliang.imageselector.PreviewActivity" android:theme="@style/Theme.AppCompat.Light.NoActionBar" android:configChanges="orientation|keyboardHidden|screenSize"/> //圖片剪切Activity <activity android:name="com.donkingliang.imageselector.ClipImageActivity" android:theme="@style/Theme.AppCompat.Light.NoActionBar" />
調(diào)起圖片選擇器
//單選 ImageSelectorUtils.openPhoto(MainActivity.this, REQUEST_CODE, true, 0); //限數(shù)量的多選(比喻最多9張) ImageSelectorUtils.openPhoto(MainActivity.this, REQUEST_CODE, false, 9); ImageSelectorUtils.openPhoto(MainActivity.this, REQUEST_CODE, false, 9, selected); // 把已選的傳入。 //不限數(shù)量的多選 ImageSelectorUtils.openPhoto(MainActivity.this, REQUEST_CODE); ImageSelectorUtils.openPhoto(MainActivity.this, REQUEST_CODE, selected); // 把已選的傳入。 //或者 ImageSelectorUtils.openPhoto(MainActivity.this, REQUEST_CODE, false, 0); ImageSelectorUtils.openPhoto(MainActivity.this, REQUEST_CODE, false, 0, selected); // 把已選的傳入。 //單選并剪裁 ImageSelectorUtils.openPhotoAndClip(MainActivity.this, REQUEST_CODE);
REQUEST_CODE就是調(diào)用者自己定義的啟動Activity時的requestCode,這個相信大家都能明白。selected可以在再次打開選擇器時,把原來已經(jīng)選擇過的圖片傳入,使這些圖片默認為選中狀態(tài)。
接收選擇器返回的數(shù)據(jù)
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_CODE && data != null) {
//獲取選擇器返回的數(shù)據(jù)
ArrayList<String> images = data.getStringArrayListExtra(
ImageSelectorUtils.SELECT_RESULT);
}
}
ImageSelectorUtils.SELECT_RESULT是接收數(shù)據(jù)的key。數(shù)據(jù)是以ArrayList的字符串數(shù)組返回的,就算是單選,返回的也是ArrayList數(shù)組,只不過這時候ArrayList只有一條數(shù)據(jù)而已。ArrayList里面的數(shù)據(jù)就是選中的圖片的文件路徑。
是不是有點懵了,我附上實際操作代碼
1. adapter_image.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="wrap_content" android:layout_gravity="center" android:layout_margin="1dp"> <com.donkingliang.imageselector.view.SquareImageView android:id="@+id/iv_image" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center" android:background="#010101" android:scaleType="centerCrop" /> </FrameLayout>
2.主布局
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <Button android:id="@+id/btn_single" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="10dp" android:text="單選" /> <Button android:id="@+id/btn_limit" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignTop="@+id/btn_single" android:layout_toRightOf="@+id/btn_single" android:text="多選(最多9張)" /> <Button android:id="@+id/btn_unlimited" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/btn_single" android:layout_below="@+id/btn_single" android:text="多選(不限數(shù)量)" /> <Button android:id="@+id/btn_clip" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignTop="@+id/btn_unlimited" android:layout_marginLeft="10dp" android:layout_toRightOf="@+id/btn_unlimited" android:text="單選并剪裁" /> <android.support.v7.widget.RecyclerView android:id="@+id/rv_image" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_below="@+id/btn_unlimited" android:layout_marginTop="10dp" /> </RelativeLayout>
3.ImageAdapter(圖片選擇器工具類)
public class ImageAdapter extends RecyclerView.Adapter<ImageAdapter.ViewHolder> {
private Context mContext;
private ArrayList<String> mImages;
private LayoutInflater mInflater;
public ImageAdapter(Context context) {
mContext = context;
this.mInflater = LayoutInflater.from(mContext);
}
public ArrayList<String> getImages() {
return mImages;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = mInflater.inflate(R.layout.adapter_image, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
final String image = mImages.get(position);
Glide.with(mContext).load(new File(image)).into(holder.ivImage);
}
@Override
public int getItemCount() {
return mImages == null ? 0 : mImages.size();
}
public void refresh(ArrayList<String> images) {
mImages = images;
notifyDataSetChanged();
}
static class ViewHolder extends RecyclerView.ViewHolder {
ImageView ivImage;
public ViewHolder(View itemView) {
super(itemView);
ivImage = itemView.findViewById(R.id.iv_image);
}
}
}
4.業(yè)務邏輯
package com.example.imageselector;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import com.donkingliang.imageselector.utils.ImageSelectorUtils;
import java.util.ArrayList;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
public class MainActivity extends AppCompatActivity {
@BindView(R.id.btn_single)
Button btnSingle;
@BindView(R.id.btn_limit)
Button btnLimit;
@BindView(R.id.btn_unlimited)
Button btnUnlimited;
@BindView(R.id.btn_clip)
Button btnClip;
@BindView(R.id.rv_image)
RecyclerView rvImage;
private static final int REQUEST_CODE = 0x00000011;
private ImageAdapter mAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
rvImage.setLayoutManager(new GridLayoutManager(this, 3));
mAdapter = new ImageAdapter(this);
rvImage.setAdapter(mAdapter);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_CODE && data != null) {
ArrayList<String> images = data.getStringArrayListExtra(ImageSelectorUtils.SELECT_RESULT);
mAdapter.refresh(images);
}
}
@OnClick({R.id.btn_single, R.id.btn_limit, R.id.btn_unlimited, R.id.btn_clip})
public void onViewClicked(View view) {
switch (view.getId()) {
case R.id.btn_single:
//單選
ImageSelectorUtils.openPhoto(MainActivity.this, REQUEST_CODE, true, 0);
break;
case R.id.btn_limit:
//多選(最多9張)
ImageSelectorUtils.openPhoto(MainActivity.this, REQUEST_CODE, false, 10);
//ImageSelectorUtils.openPhoto(MainActivity.this, REQUEST_CODE, false, 9, mAdapter.getImages()); // 把已選的傳入。
break;
case R.id.btn_unlimited:
//多選(不限數(shù)量)
ImageSelectorUtils.openPhoto(MainActivity.this, REQUEST_CODE);
//ImageSelectorUtils.openPhoto(MainActivity.this, REQUEST_CODE, mAdapter.getImages()); // 把已選的傳入。
//或者
//ImageSelectorUtils.openPhoto(MainActivity.this, REQUEST_CODE, false, 0);
//ImageSelectorUtils.openPhoto(MainActivity.this, REQUEST_CODE, false, 0, mAdapter.getImages()); // 把已選的傳入。
break;
case R.id.btn_clip:
//單選并剪裁
ImageSelectorUtils.openPhotoAndClip(MainActivity.this, REQUEST_CODE);
break;
}
}
}
最后,感謝大牛提供的源碼框架,我真的非常喜歡。
Android圖片選擇器,仿微信的圖片選擇器的樣式和效果。支持圖片的單選、限數(shù)量的多選和不限數(shù)量的多選。支持圖片預覽和圖片文件夾的切換。
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
Android開發(fā)中PopupWindow用法實例分析
這篇文章主要介紹了Android開發(fā)中PopupWindow用法,結(jié)合實例形式分析了PopupWindow彈出窗口效果的使用技巧,需要的朋友可以參考下2016-02-02
Android利用廣播接收器實現(xiàn)自動填充短信驗證碼
這篇文章主要為大家詳細介紹了Android利用廣播接收器實現(xiàn)自動填充短信驗證碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-12-12
快速解決fragment中onActivityResult不調(diào)用的問題
下面小編就為大家?guī)硪黄焖俳鉀Qfragment中onActivityResult不調(diào)用的問題。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-04-04
Android中模仿抖音加載框之兩顆小球轉(zhuǎn)動效果
這篇文章主要介紹了Android仿抖音加載框之兩顆小球轉(zhuǎn)動控件,本文通過實例代碼給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2018-09-09

