Skip to content

Commit

Permalink
安卓X ------增加系统拍照入口 增加可配置裁剪
Browse files Browse the repository at this point in the history
  • Loading branch information
chenli committed Dec 2, 2019
1 parent 95db0c0 commit 530a685
Show file tree
Hide file tree
Showing 30 changed files with 639 additions and 137 deletions.
28 changes: 9 additions & 19 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,43 +1,33 @@
apply plugin: 'com.android.application'


android {

compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}

compileSdkVersion 28
compileSdkVersion 29
defaultConfig {
applicationId "com.cl.picure"
minSdkVersion 15
targetSdkVersion 28
targetSdkVersion 29
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}

}


dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
implementation 'androidx.appcompat:appcompat:1.0.2'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
androidTestImplementation 'androidx.test:runner:1.2.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'

implementation 'com.github.bumptech.glide:glide:4.9.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.9.0'

implementation project(':picture_selector')


implementation 'androidx.recyclerview:recyclerview:1.1.0'
}
147 changes: 147 additions & 0 deletions app/src/main/java/com/cl/picure/GoodsMainFigureAdapter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
package com.cl.picure;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;

import java.util.ArrayList;
import java.util.List;

import static com.cl.picure.MainActivity.TAG_NEGATIVE;

/**
* 项目:hbtGit
* 版权:蒲公英公司 版权所有
* 作者:Arry
* 版本:1.0
* 创建日期:2019-11-08
* 描述:
* 修订历史:
*/
public class GoodsMainFigureAdapter extends RecyclerView.Adapter<GoodsMainFigureAdapter.SelectedPicViewHolder>{

private int maxImgCount;
private Context mContext;
private List<String> mData = new ArrayList<>();

private LayoutInflater mInflater;
private OnRecyclerViewItemClickListener listener;
private boolean isAdded; //是否额外添加了最后一个图片

public interface OnRecyclerViewItemClickListener {
void onItemClick(View view, int position);
}

public void setOnItemClickListener(OnRecyclerViewItemClickListener listener) {
this.listener = listener;
}

public void setImages(List<String> data) {
mData = new ArrayList<>(data);
if (getItemCount() < maxImgCount) {
mData.add(new String());
isAdded = true;
} else {
isAdded = false;
}
notifyDataSetChanged();
}

public List<String> getImages() {
//由于图片未选满时,最后一张显示添加图片,因此这个方法返回真正的已选图片
if (isAdded) return new ArrayList<>(mData.subList(0, mData.size() - 1));
else return mData;
}

public GoodsMainFigureAdapter(Context mContext, List<String> data, int maxImgCount) {
this.mContext = mContext;
this.maxImgCount = maxImgCount;
this.mInflater = LayoutInflater.from(mContext);
setImages(data);
}

@Override
public SelectedPicViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return new SelectedPicViewHolder(mInflater.inflate(R.layout.item_list_image, parent, false));
}

@Override
public void onBindViewHolder(SelectedPicViewHolder holder, int position) {
holder.bind(position);
}

@Override
public int getItemCount() {
return mData.size();
}

public class SelectedPicViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

private final ImageView mIvDelete;
private ImageView iv_img;
private int clickPosition;

public SelectedPicViewHolder(View itemView) {
super(itemView);
iv_img = (ImageView) itemView.findViewById(R.id.iv_img);
mIvDelete = (ImageView) itemView.findViewById(R.id.iv_delete);
}

public void bind(final int position) {
//设置条目的点击事件
itemView.setOnClickListener(this);
//根据条目位置设置图片
String item = mData.get(position);
if (isAdded && position == getItemCount() - 1) {
iv_img.setImageResource(R.mipmap.ic_add_pic);
clickPosition = TAG_NEGATIVE;
mIvDelete.setVisibility(View.GONE);
} else {
Glide.with(mContext).load(item).into(iv_img);
clickPosition = position;
mIvDelete.setVisibility(View.VISIBLE);
}
mIvDelete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// if(mData!=null){
// remove(position);
// }
if (onImage != null) {
onImage.OnItemImg(position);
}
}
});
}

@Override
public void onClick(View v) {
if (listener != null) listener.onItemClick(v, clickPosition);
}
}

public void remove(int position) {
mData.remove(position);
notifyItemRemoved(position);
}

/**
* 监听器
*/
public interface OnImage {
public void OnItemImg(int position);
}

public OnImage onImage;

public void setOnImage(OnImage onImage) {
this.onImage = onImage;
}

}
94 changes: 66 additions & 28 deletions app/src/main/java/com/cl/picure/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,35 @@
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;

import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.cl.picture_selector.ImagePicker;
import com.cl.picture_selector.manager.CameraConfig;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
public class MainActivity extends AppCompatActivity implements GoodsMainFigureAdapter
.OnRecyclerViewItemClickListener {


private TextView mTextView;
private ImageView tox;
private RecyclerView rvList;
private GoodsMainFigureAdapter mainFigureAdapter;
private int maxImgCount = 8;
public static final int TAG_NEGATIVE = -1;
List dataList = new ArrayList<String>();

private List<String> listImg = new ArrayList<>();


private static final int REQUEST_SELECT_IMAGES_CODE = 0x01;
Expand All @@ -33,34 +46,32 @@ protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_main);

mTextView = findViewById(R.id.tv_select_images);
tox = findViewById(R.id.img_tox);
findViewById(R.id.bt_select_images).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
imagePicker.getInstance()
.setTitle("标题")//设置标题
.showCamera(true)//设置是否显示拍照按钮
.showImage(true)//设置是否展示图片
.showVideo(true)//设置是否展示视频
.showLoading(true, "ssssssssssss")
.setMaxCount(9)//设置最大选择图片数目(默认为1,单选)
.setSingleType(true)//设置图片视频不能同时选择
.setImagePaths(mImagePaths)//设置历史选择记录
.setImageLoader(new GlideLoader())//设置自定义图片加载器
.start(MainActivity.this, REQUEST_SELECT_IMAGES_CODE);//REQEST_SELECT_IMAGES_CODE为Intent调用的requestCode
}
});

rvList = findViewById(R.id.rv_img);

findViewById(R.id.bt_camera).setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
//todo 一定要先做权限判断
imagePicker.getInstance().startCamera(MainActivity.this, 200);//设置是否展示视频setSingleType(true).startCamera();
//imagePicker.getInstance().startCamera(MainActivity.this, 200);//设置是否展示视频setSingleType(true).startCamera();

CameraConfig config = new CameraConfig.Builder()
.needCrop(false)
.cropSize(1, 1, 200, 200)
.build();

imagePicker.getInstance().toCameraActivity(MainActivity.this, config, REQUEST_SELECT_IMAGES_CODE);

}
});


rvList.setLayoutManager(new GridLayoutManager(this, 4));
rvList.setHasFixedSize(true);
mainFigureAdapter = new GoodsMainFigureAdapter(this, dataList, maxImgCount);
rvList.setAdapter(mainFigureAdapter);
mainFigureAdapter.setOnItemClickListener(this);

}

@Override
Expand All @@ -73,13 +84,40 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) {
stringBuffer.append(mImagePaths.get(i) + "\n\n");
}
mTextView.setText(stringBuffer.toString());
} else if (requestCode == 200 && resultCode == Activity.RESULT_OK) {
Bundle bundle = data.getExtras();
Bitmap bm = (Bitmap) bundle.get("data");
if (bm != null) {
tox.setImageBitmap(bm);
if (mImagePaths.size() != 0) {
listImg.addAll(mImagePaths);
mainFigureAdapter.setImages(listImg);
}
}
}

@Override
public void onItemClick(View view, int position) {
switch (position) {
case TAG_NEGATIVE:
imagePicker.getInstance()
.setTitle("标题")//设置标题
// .showCamera(true)//设置是否显示拍照按钮
.showImage(true)//设置是否展示图片
.showVideo(true)//设置是否展示视频
.showLoading(true, "ssssssssssss")
.setMaxCount(9)//设置最大选择图片数目(默认为1,单选)
.setSingleType(true)//设置图片视频不能同时选择
.setImagePaths(mImagePaths)//设置历史选择记录
.setImageLoader(new GlideLoader())//设置自定义图片加载器
.start(MainActivity.this, REQUEST_SELECT_IMAGES_CODE);//REQEST_SELECT_IMAGES_CODE为Intent调用的requestCode
break;
default:
//打开预览
// Intent intentPreview = new Intent(this, ImagePreviewDelActivity.class);
// intentPreview.putExtra(ImagePicker.EXTRA_IMAGE_ITEMS, (ArrayList<ImageItem>)
// adapter.getImages());
// intentPreview.putExtra(ImagePicker.EXTRA_SELECTED_IMAGE_POSITION, position);
// intentPreview.putExtra(ImagePicker.EXTRA_FROM_ITEMS, true);
// startActivityForResult(intentPreview, REQUEST_CODE_PREVIEW);
// XLog.e(TAG, "查看图片");
break;
}
}
}

21 changes: 6 additions & 15 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,36 +7,27 @@
android:orientation="vertical"
tools:context=".MainActivity">

<Button
android:id="@+id/bt_select_images"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="40dp"
android:text="点击跳转图片选择器" />


<Button
android:id="@+id/bt_camera"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="40dp"
android:layout_marginTop="20dp"
android:text="相机跳转" />


<TextView
android:id="@+id/tv_select_images"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_marginTop="10dp"
android:textColor="@color/text_bg_gray" />


<ImageView
android:id="@+id/img_tox"
android:layout_width="200dp"
android:layout_height="200dp" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rv_img"
android:layout_width="match_parent"
android:layout_height="match_parent" />


</LinearLayout>
Loading

0 comments on commit 530a685

Please sign in to comment.