最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android ImageSelector微信圖片選擇器

 更新時(shí)間:2019年06月15日 13:47:57   作者:AND_Devil  
這篇文章主要為大家詳細(xì)介紹了Android ImageSelector微信圖片選擇器,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

前言

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

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

效果圖



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

思路

1.從手機(jī)存儲(chǔ)卡中掃描加載圖片。
2.用一個(gè)列表將圖片顯示出來(lái)。
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)目去配置,不一定要這樣寫(xiě),只要不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可以在再次打開(kāi)選擇器時(shí),把原來(lái)已經(jīng)選擇過(guò)的圖片傳入,使這些圖片默認(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ù)組,只不過(guò)這時(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ù)覽和圖片文件夾的切換。

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Android編程設(shè)計(jì)模式之原型模式實(shí)例詳解

    Android編程設(shè)計(jì)模式之原型模式實(shí)例詳解

    這篇文章主要介紹了Android編程設(shè)計(jì)模式之原型模式,結(jié)合實(shí)例形式詳細(xì)分析了Android設(shè)計(jì)模式之原型模式的概念、原理、定義、使用方法及相關(guān)注意事項(xiàng),需要的朋友可以參考下
    2017-12-12
  • Android App中使用Pull解析XML格式數(shù)據(jù)的使用示例

    Android App中使用Pull解析XML格式數(shù)據(jù)的使用示例

    這篇文章主要介紹了Android App中使用Pull解析XML格式數(shù)據(jù)的使用示例,Pull是Android中自帶的XML解析器,Java里也是一樣用:D需要的朋友可以參考下
    2016-04-04
  • Android實(shí)現(xiàn)左右滑動(dòng)切換圖片

    Android實(shí)現(xiàn)左右滑動(dòng)切換圖片

    這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)左右滑動(dòng)切換圖片,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-05-05
  • Android自定義View實(shí)現(xiàn)開(kāi)關(guān)按鈕

    Android自定義View實(shí)現(xiàn)開(kāi)關(guān)按鈕

    android 自定義view知識(shí)非常廣泛,難以讓人掌握。但是也是andoroid進(jìn)階學(xué)習(xí)的必經(jīng)之路。下面通過(guò)本文給大家介紹Android自定義View實(shí)現(xiàn)開(kāi)關(guān)按鈕的知識(shí),非常不錯(cuò),感興趣的朋友一起看看吧
    2016-11-11
  • Android Native 內(nèi)存泄漏系統(tǒng)化解決方案

    Android Native 內(nèi)存泄漏系統(tǒng)化解決方案

    這篇文章主要介紹了Android Native 內(nèi)存泄漏系統(tǒng)化解決方案,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-07-07
  • Android UI效果之繪圖篇(三)

    Android UI效果之繪圖篇(三)

    這篇文章主要介紹了Android UI效果之繪圖篇,針對(duì)Android開(kāi)發(fā)中的UI效果設(shè)計(jì)模塊進(jìn)行講解,感興趣的小伙伴們可以參考一下
    2016-02-02
  • Android手勢(shì)操作示例(上/下/左/右的判斷)

    Android手勢(shì)操作示例(上/下/左/右的判斷)

    這篇文章主要介紹了Android手勢(shì)操作方法,包含了針對(duì)上、下、左、右等方向的判斷,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2016-06-06
  • 如何在Android studio 中使用單例模式

    如何在Android studio 中使用單例模式

    這篇文章主要介紹了如何在Android studio 中使用單例模式,幫助大家更好的理解和學(xué)習(xí)Android開(kāi)發(fā),感興趣的朋友可以了解下
    2021-03-03
  • android 網(wǎng)絡(luò)請(qǐng)求庫(kù)volley方法詳解

    android 網(wǎng)絡(luò)請(qǐng)求庫(kù)volley方法詳解

    這篇文章主要介紹了android 網(wǎng)絡(luò)請(qǐng)求庫(kù)volley方法詳解的相關(guān)資料,需要的朋友可以參考下
    2016-09-09
  • Android仿IOS系統(tǒng)懸浮窗效果

    Android仿IOS系統(tǒng)懸浮窗效果

    這篇文章主要為大家詳細(xì)介紹了Android仿IOS系統(tǒng)懸浮窗效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-09-09

最新評(píng)論

日土县| 横山县| 界首市| 微山县| 永平县| 东山县| 香港 | 南丰县| 大方县| 温宿县| 西林县| 益阳市| 广东省| 同德县| 大方县| 磐安县| 繁峙县| 梅州市| 荣昌县| 佛学| 金平| 巨野县| 虹口区| 铜陵市| 博罗县| 灵寿县| 罗江县| 凤翔县| 建平县| 聊城市| 邢台县| 永宁县| 东城区| 磐石市| 新津县| 黔西县| 桦川县| 南昌市| 山东| 永康市| 宁远县|