Skip to content

Commit

Permalink
增加系统拍照入口 可配置裁剪
Browse files Browse the repository at this point in the history
  • Loading branch information
chenli committed Dec 2, 2019
1 parent 4ac4f54 commit b2d2f30
Show file tree
Hide file tree
Showing 13 changed files with 549 additions and 50 deletions.
1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ dependencies {
annotationProcessor 'com.github.bumptech.glide:compiler:4.9.0'

implementation project(':picture_selector')
implementation 'com.android.support:recyclerview-v7:28.0.0'


}
146 changes: 146 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,146 @@
package com.cl.picure;

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

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: 62 additions & 32 deletions app/src/main/java/com/cl/picure/MainActivity.java
Original file line number Diff line number Diff line change
@@ -1,28 +1,37 @@
package com.cl.picure;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;

import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;


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;
private ArrayList<String> mImagePaths;

private ImagePicker imagePicker;


Expand All @@ -32,34 +41,28 @@ 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();
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 @@ -72,13 +75,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;
}
}
}

23 changes: 7 additions & 16 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" />
<android.support.v7.widget.RecyclerView
android:id="@+id/rv_img"
android:layout_width="match_parent"
android:layout_height="match_parent" />


</LinearLayout>
</LinearLayout>
28 changes: 28 additions & 0 deletions app/src/main/res/layout/item_list_image.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical">

<RelativeLayout
android:layout_width="80dp"
android:layout_height="80dp">

<ImageView
android:id="@+id/iv_img"
android:layout_width="70dp"
android:layout_height="70dp"
android:scaleType="centerCrop"
android:layout_alignParentBottom="true"
android:src="@mipmap/ic_add_pic" />

<ImageView
android:id="@+id/iv_delete"
android:layout_width="18dp"
android:layout_height="18dp"
android:layout_alignParentRight="true"
android:src="@mipmap/ic_del_pic" />

</RelativeLayout>
</LinearLayout>
Binary file added app/src/main/res/mipmap-xhdpi/ic_add_pic.webp
Binary file not shown.
Binary file added app/src/main/res/mipmap-xhdpi/ic_del_pic.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions picture_selector/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ android {
defaultConfig {
minSdkVersion 15
targetSdkVersion 28
versionCode 2
versionName "1.2.20"
versionCode 3
versionName "1.2.22"


testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
Expand Down
4 changes: 4 additions & 0 deletions picture_selector/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
android:name="com.cl.picture_selector.activity.ImagePreActivity"
android:screenOrientation="portrait" />

<activity
android:name=".activity.ImageCameraActivity"
android:screenOrientation="portrait" />

<!-- Android 7.0 文件共享配置,必须配置 -->
<provider
android:name=".provider.ImagePickerProvider"
Expand Down
Loading

0 comments on commit b2d2f30

Please sign in to comment.