溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

AndroidImageSelector微信圖片選擇器怎么用

發(fā)布時(shí)間:2021-10-13 15:45:21 來源:億速云 閱讀:131 作者:柒染 欄目:編程語言

本篇文章為大家展示了AndroidImageSelector微信圖片選擇器怎么用,內(nèi)容簡明扼要并且容易理解,絕對(duì)能使你眼前一亮,通過這篇文章的詳細(xì)介紹希望你能有所收獲。

前言

現(xiàn)在絕大多數(shù)的App都上傳圖片的功能,比如設(shè)置用戶頭像、聊天發(fā)送圖片、發(fā)表動(dòng)態(tài)、論壇帖子等。上傳圖片需要先從選擇手機(jī)中選擇要上傳的圖片,所以圖片選擇器在App中是很常見的組件,一般的手機(jī)都會(huì)自帶一個(gè)圖片選擇器。不過很多App并不喜歡用手機(jī)自帶的選擇器,而是自己實(shí)現(xiàn)一個(gè)圖片選擇器。

比如微信的圖片選擇器就做的很好。沒辦法,誰讓微信這么強(qiáng)大,我不超抄襲你,但是,我可以模仿你。

效果圖

是不是和真的一樣,哈哈,不過,作者的唯一缺陷就是沒有提供拍照,唉,有一點(diǎn)遺憾,但是,這個(gè)就夠用了!

思路

1.從手機(jī)存儲(chǔ)卡中掃描加載圖片。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

//儲(chǔ)存卡的讀取權(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í)候會(huì)發(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)容就是AndroidImageSelector微信圖片選擇器怎么用,你們學(xué)到知識(shí)或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識(shí)儲(chǔ)備,歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI