Android ImageSelector微信圖片選擇器
前言
現(xiàn)在絕大多數(shù)的App都上傳圖片的功能,比如設(shè)置用戶頭像、聊天發(fā)送圖片、發(fā)表動(dòng)態(tài)、論壇帖子等。上傳圖片需要先從選擇手機(jī)中選擇要上傳的圖片,所以圖片選擇器在App中是很常見的組件,一般的手機(jī)都會自帶一個(gè)圖片選擇器。不過很多App并不喜歡用手機(jī)自帶的選擇器,而是自己實(shí)現(xiàn)一個(gè)圖片選擇器。
比如微信的圖片選擇器就做的很好。沒辦法,誰讓微信這么強(qiáng)大,我不超抄襲你,但是,我可以模仿你。
效果圖
是不是和真的一樣,哈哈,不過,作者的唯一缺陷就是沒有提供拍照,唉,有一點(diǎn)遺憾,但是,這個(gè)就夠用了!
思路
1.從手機(jī)存儲卡中掃描加載圖片。
2.用一個(gè)列表將圖片顯示出來。
3.選擇圖片。
4.把選中的圖片返回給調(diào)用者。
準(zhǔn)備工作
引入依賴
//在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
//儲存卡的讀取權(quán)限 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> //圖片選擇Activity <activity android:name="com.donkingliang.imageselector.ImageSelectorActivity" //去掉Activity的ActionBar。 //使用者可以根據(jù)自己的項(xiàng)目去配置,不一定要這樣寫,只要不Activity的ActionBar去掉就可以了。 android:theme="@style/Theme.AppCompat.Light.NoActionBar" //橫豎屏切換處理。 //如果要支持橫豎屏切換,一定要加上這句,否則在切換橫豎屏的時(shí)候會發(fā)生異常。 android:configChanges="orientation|keyboardHidden|screenSize"/> //圖片預(yù)覽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)用者自己定義的啟動(dòng)Activity時(shí)的requestCode,這個(gè)相信大家都能明白。selected可以在再次打開選擇器時(shí),把原來已經(jīng)選擇過的圖片傳入,使這些圖片默認(rèn)為選中狀態(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的字符串?dāng)?shù)組返回的,就算是單選,返回的也是ArrayList數(shù)組,只不過這時(shí)候ArrayList只有一條數(shù)據(jù)而已。ArrayList里面的數(shù)據(jù)就是選中的圖片的文件路徑。
是不是有點(diǎn)懵了,我附上實(shí)際操作代碼
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è)務(wù)邏輯
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ù)量的多選。支持圖片預(yù)覽和圖片文件夾的切換。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
android實(shí)現(xiàn)查詢公交車還有幾站的功能
這篇文章主要為大家詳細(xì)介紹了android實(shí)現(xiàn)查詢公交車還有幾站的功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-05-05Android基于訊飛語音SDK實(shí)現(xiàn)語音識別
本例子是一個(gè)調(diào)用訊飛語音識別SDK的例子源碼是一個(gè)最純凈的Demo比較容易看懂。實(shí)現(xiàn)的是點(diǎn)擊按鈕開始語音監(jiān)聽,手機(jī)需要聯(lián)網(wǎng),2/3G的均可,希望本文對大家學(xué)習(xí)Android有所幫助2016-06-06Android開發(fā)中PopupWindow用法實(shí)例分析
這篇文章主要介紹了Android開發(fā)中PopupWindow用法,結(jié)合實(shí)例形式分析了PopupWindow彈出窗口效果的使用技巧,需要的朋友可以參考下2016-02-02簡單實(shí)現(xiàn)Android計(jì)算器功能
這篇文章主要為大家詳細(xì)介紹了自己動(dòng)手實(shí)現(xiàn)的Android計(jì)算器功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-01-01Android利用廣播接收器實(shí)現(xiàn)自動(dòng)填充短信驗(yàn)證碼
這篇文章主要為大家詳細(xì)介紹了Android利用廣播接收器實(shí)現(xiàn)自動(dòng)填充短信驗(yàn)證碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-12-12Android編程之高效開發(fā)App的10個(gè)建議
這篇文章主要介紹了Android編程之高效開發(fā)App的10個(gè)建議,較為詳細(xì)的分析了Android開發(fā)中的常見問題與注意事項(xiàng),具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-10-10快速解決fragment中onActivityResult不調(diào)用的問題
下面小編就為大家?guī)硪黄焖俳鉀Qfragment中onActivityResult不調(diào)用的問題。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-04-04Android中模仿抖音加載框之兩顆小球轉(zhuǎn)動(dòng)效果
這篇文章主要介紹了Android仿抖音加載框之兩顆小球轉(zhuǎn)動(dòng)控件,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-09-09android開機(jī)自動(dòng)啟動(dòng)app的解決方法
這篇文章主要為大家詳細(xì)介紹了android開機(jī)自動(dòng)啟動(dòng)app的解決方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-05-05