本文主要是介绍【图片选择】Android 仿微信图片选择器疾风加载 (单选,多选,相机)--唯剑做伴且随疾风前行,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
请下载必要的配置文件按里面的目录copy导入使用
效果图
配置
- app graild加依赖:
compile 'com.squareup.picasso:picasso:2.4.0'
- 权限:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
- 声明Activity
<activity
android:configChanges="orientation|screenSize"android:name=".mvp.main.act.gongneng.imgselected.MultiImageSelectorActivity" />
- 修改布局文件:
mis_list_item_camera
中的SquareFrameLayout改为自己目录下的,mis_list_item_image
里面的自定义view都改为自己的 - 导入上面下载的文件改变导入类的包名就可以了ok
使用步骤
代码是最好的老师
MainActivity
package me.nereo.multiimageselector;import android.Manifest;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.TextView;import java.util.ArrayList;import me.nereo.multi_image_selector.MultiImageSelector;public class MainActivity extends AppCompatActivity {private static final int REQUEST_IMAGE = 2;protected static final int REQUEST_STORAGE_READ_ACCESS_PERMISSION = 101;private TextView mResultText;private RadioGroup mChoiceMode, mShowCamera;private EditText mRequestNum;private ArrayList<String> mSelectPath;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);mResultText = (TextView) findViewById(R.id.result);mChoiceMode = (RadioGroup) findViewById(R.id.choice_mode);mShowCamera = (RadioGroup) findViewById(R.id.show_camera);mRequestNum = (EditText) findViewById(R.id.request_num);mChoiceMode.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {@Overridepublic void onCheckedChanged(RadioGroup radioGroup, int checkedId) {if (checkedId == R.id.multi) {mRequestNum.setEnabled(true);} else {mRequestNum.setEnabled(false);mRequestNum.setText("");}}});View button = findViewById(R.id.button);if (button != null) {button.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {pickImage();}});}}private void pickImage() {if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN // Permission was added in API Level 16&& ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE)!= PackageManager.PERMISSION_GRANTED) {requestPermission(Manifest.permission.READ_EXTERNAL_STORAGE,getString(R.string.mis_permission_rationale),REQUEST_STORAGE_READ_ACCESS_PERMISSION);} else {boolean showCamera = mShowCamera.getCheckedRadioButtonId() == R.id.show;int maxNum = 9;if (!TextUtils.isEmpty(mRequestNum.getText())) {try {maxNum = Integer.valueOf(mRequestNum.getText().toString());} catch (NumberFormatException e) {e.printStackTrace();}}MultiImageSelector selector = MultiImageSelector.create(MainActivity.this);selector.showCamera(showCamera);selector.count(maxNum);if (mChoiceMode.getCheckedRadioButtonId() == R.id.single) {selector.single();} else {selector.multi();}selector.origin(mSelectPath);selector.start(MainActivity.this, REQUEST_IMAGE);}}private void requestPermission(final String permission, String rationale, final int requestCode) {if (ActivityCompat.shouldShowRequestPermissionRationale(this, permission)) {new AlertDialog.Builder(this).setTitle(R.string.mis_permission_dialog_title).setMessage(rationale).setPositiveButton(R.string.mis_permission_dialog_ok, new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {ActivityCompat.requestPermissions(MainActivity.this, new String[]{permission}, requestCode);}}).setNegativeButton(R.string.mis_permission_dialog_cancel, null).create().show();} else {ActivityCompat.requestPermissions(this, new String[]{permission}, requestCode);}}@Overridepublic void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {if (requestCode == REQUEST_STORAGE_READ_ACCESS_PERMISSION) {if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {pickImage();}} else {super.onRequestPermissionsResult(requestCode, permissions, grantResults);}}@Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent data) {super.onActivityResult(requestCode, resultCode, data);if (requestCode == REQUEST_IMAGE) {if (resultCode == RESULT_OK) {mSelectPath = data.getStringArrayListExtra(MultiImageSelector.EXTRA_RESULT);StringBuilder sb = new StringBuilder();for (String p : mSelectPath) {sb.append(p);sb.append("\n");}mResultText.setText(sb.toString());}}}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {getMenuInflater().inflate(R.menu.menu_main, menu);return true;}@Overridepublic boolean onOptionsItemSelected(MenuItem item) {int id = item.getItemId();if (id == R.id.action_settings) {return true;}return super.onOptionsItemSelected(item);}
}
主布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"android:paddingBottom="@dimen/activity_vertical_margin"android:paddingLeft="@dimen/activity_horizontal_margin"android:paddingRight="@dimen/activity_horizontal_margin"android:paddingTop="@dimen/activity_vertical_margin"tools:context=".MainActivity"><TextView
android:layout_width="match_parent"android:layout_height="wrap_content"android:focusable="true"android:focusableInTouchMode="true"android:text="选择模式"android:textColor="@android:color/black"android:textSize="16sp"android:textStyle="bold"/><RadioGroup
android:id="@+id/choice_mode"android:layout_width="match_parent"android:layout_height="wrap_content"android:checkedButton="@+id/multi"android:orientation="vertical"><RadioButton
android:id="@+id/single"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="单选"/><RadioButton
android:id="@+id/multi"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="多选"/></RadioGroup><TextView
android:layout_width="match_parent"android:layout_height="wrap_content"android:text="最大选择数量"android:textColor="@android:color/black"android:textSize="16sp"android:textStyle="bold"/><EditText
android:id="@+id/request_num"android:layout_width="match_parent"android:layout_height="wrap_content"android:enabled="false"android:hint="请输入(默认9)"android:inputType="number"/><TextView
android:layout_width="match_parent"android:layout_height="wrap_content"android:text="是否使用相机"android:textColor="@android:color/black"android:textSize="16sp"android:textStyle="bold"/><RadioGroup
android:id="@+id/show_camera"android:layout_width="match_parent"android:layout_height="wrap_content"android:checkedButton="@+id/show"android:orientation="vertical"><RadioButton
android:id="@+id/show"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="使用"/><RadioButton
android:id="@+id/no_show"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="不使用"/></RadioGroup><Button
android:id="@+id/button"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="选择图片"/><ScrollView
android:layout_width="match_parent"android:layout_height="match_parent"><LinearLayout
android:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="vertical"><TextView
android:layout_width="match_parent"android:layout_height="wrap_content"android:text="选择结果如下:"/><TextView
android:id="@+id/result"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginTop="10dp"/></LinearLayout></ScrollView></LinearLayout>
这篇关于【图片选择】Android 仿微信图片选择器疾风加载 (单选,多选,相机)--唯剑做伴且随疾风前行的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!