本篇內(nèi)容介紹了“Android中如何利用ImageSelector實現(xiàn)微信圖片選擇器”的有關(guān)知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!

引入依賴
//在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ù)自己的項目去配置,不一定要這樣寫,只要不Activity的ActionBar去掉就可以了。 android:theme="@style/Theme.AppCompat.Light.NoActionBar" //橫豎屏切換處理。 //如果要支持橫豎屏切換,一定要加上這句,否則在切換橫豎屏的時候會發(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)用者自己定義的啟動Activity時的requestCode,這個相信大家都能明白。selected可以在再次打開選擇器時,把原來已經(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ù)組,只不過這時候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è)務(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中如何利用ImageSelector實現(xiàn)微信圖片選擇器”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識可以關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實用文章!
網(wǎng)站標(biāo)題:Android中如何利用ImageSelector實現(xiàn)微信圖片選擇器-創(chuàng)新互聯(lián)
當(dāng)前地址:http://chinadenli.net/article26/ccjpcg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站制作、網(wǎng)頁設(shè)計公司、手機網(wǎng)站建設(shè)、品牌網(wǎng)站設(shè)計、域名注冊、網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容