Skip to content

Commit

Permalink
Improve demo
Browse files Browse the repository at this point in the history
  • Loading branch information
donglua committed Jan 9, 2017
1 parent cd76a52 commit 3b68d00
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 125 deletions.
8 changes: 0 additions & 8 deletions photopickerdemo/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,6 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="me.iwf.PhotoPickerDemo"
>
<!--<permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>-->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

<!--<permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>-->
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

<!--<permission android:name="android.permission.CAMERA"/>-->
<uses-permission android:name="android.permission.CAMERA" />

<!--
- Include all the "features" under the camera permission,
Expand Down
134 changes: 50 additions & 84 deletions photopickerdemo/src/main/java/me/iwf/PhotoPickerDemo/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ public class MainActivity extends AppCompatActivity {

private ArrayList<String> selectedPhotos = new ArrayList<>();

private int currentClickId = -1;

@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Fabric.with(this, new Crashlytics());
Expand All @@ -40,30 +38,65 @@ public class MainActivity extends AppCompatActivity {
recyclerView.setLayoutManager(new StaggeredGridLayoutManager(4, OrientationHelper.VERTICAL));
recyclerView.setAdapter(photoAdapter);

View.OnClickListener onClickListener = new View.OnClickListener() {
findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
MainActivity.this.onClick(view.getId());
public void onClick(View v) {
PhotoPicker.builder()
.setPhotoCount(9)
.setGridColumnCount(4)
.start(MainActivity.this);
}
};
});

findViewById(R.id.button).setOnClickListener(onClickListener);
findViewById(R.id.button_no_camera).setOnClickListener(onClickListener);
findViewById(R.id.button_one_photo).setOnClickListener(onClickListener);
findViewById(R.id.button_photo_gif).setOnClickListener(onClickListener);
findViewById(R.id.button_multiple_picked).setOnClickListener(onClickListener);
findViewById(R.id.button_no_camera).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
PhotoPicker.builder()
.setPhotoCount(7)
.setShowCamera(false)
.setPreviewEnabled(false)
.start(MainActivity.this);
}
});

findViewById(R.id.button_one_photo).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
PhotoPicker.builder()
.setPhotoCount(1)
.start(MainActivity.this);
}
});

findViewById(R.id.button_photo_gif).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
PhotoPicker.builder()
.setShowCamera(true)
.setShowGif(true)
.start(MainActivity.this);
}
});

recyclerView.addOnItemTouchListener(new RecyclerItemClickListener(this,
new RecyclerItemClickListener.OnItemClickListener() {
@Override
public void onItemClick(View view, int position) {
PhotoPreview.builder()
.setPhotos(selectedPhotos)
.setCurrentItem(position)
.start(MainActivity.this);
if (photoAdapter.getItemViewType(position) == PhotoAdapter.TYPE_ADD) {
PhotoPicker.builder()
.setPhotoCount(PhotoAdapter.MAX)
.setShowCamera(true)
.setPreviewEnabled(false)
.setSelected(selectedPhotos)
.start(MainActivity.this);
} else {
PhotoPreview.builder()
.setPhotos(selectedPhotos)
.setCurrentItem(position)
.start(MainActivity.this);
}
}
}
));
}));
}

@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Expand All @@ -86,71 +119,4 @@ public void onItemClick(View view, int position) {
}
}

@Override
public void onRequestPermissionsResult(int requestCode,
@NonNull String[] permissions,
@NonNull int[] grantResults) {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
if (currentClickId != -1) onClick(currentClickId);
} else {
// permission denied, boo! Disable the
// functionality that depends on this permission.
Toast.makeText(this, "No read storage permission! Cannot perform the action.", Toast.LENGTH_SHORT).show();
}
}

@Override
public boolean shouldShowRequestPermissionRationale(@NonNull String permission) {
// No need to explain to user as it is obvious
return false;
}

private void onClick(@IdRes int viewId) {
switch (viewId) {
case R.id.button: {
PhotoPicker.builder()
.setPhotoCount(9)
.setGridColumnCount(4)
.start(this);
break;
}

case R.id.button_no_camera: {
PhotoPicker.builder()
.setPhotoCount(7)
.setShowCamera(false)
.setPreviewEnabled(false)
.start(this);
break;
}

case R.id.button_one_photo: {
PhotoPicker.builder()
.setPhotoCount(1)
.start(this);
break;
}

case R.id.button_photo_gif : {
PhotoPicker.builder()
.setShowCamera(true)
.setShowGif(true)
.start(this);
break;
}

case R.id.button_multiple_picked:{
PhotoPicker.builder()
.setPhotoCount(4)
.setShowCamera(true)
.setSelected(selectedPhotos)
.start(this);
break;
}
}

currentClickId = viewId;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package me.iwf.PhotoPickerDemo;

import android.content.Context;
import android.graphics.Color;
import android.net.Uri;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
Expand All @@ -23,6 +24,10 @@ public class PhotoAdapter extends RecyclerView.Adapter<PhotoAdapter.PhotoViewHol

private Context mContext;

final static int TYPE_ADD = 1;
final static int TYPE_PHOTO = 2;

final static int MAX = 9;

public PhotoAdapter(Context mContext, ArrayList<String> photoPaths) {
this.photoPaths = photoPaths;
Expand All @@ -33,34 +38,52 @@ public PhotoAdapter(Context mContext, ArrayList<String> photoPaths) {


@Override public PhotoViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = inflater.inflate(R.layout.__picker_item_photo, parent, false);
View itemView = null;
switch (viewType) {
case TYPE_ADD:
itemView = inflater.inflate(me.iwf.PhotoPickerDemo.R.layout.item_add, parent, false);
break;
case TYPE_PHOTO:
itemView = inflater.inflate(R.layout.__picker_item_photo, parent, false);
break;
}
return new PhotoViewHolder(itemView);
}


@Override
public void onBindViewHolder(final PhotoViewHolder holder, final int position) {

Uri uri = Uri.fromFile(new File(photoPaths.get(position)));
if (getItemViewType(position) == TYPE_PHOTO) {
Uri uri = Uri.fromFile(new File(photoPaths.get(position)));

boolean canLoadImage = AndroidLifecycleUtils.canLoadImage(holder.ivPhoto.getContext());
boolean canLoadImage = AndroidLifecycleUtils.canLoadImage(holder.ivPhoto.getContext());

if (canLoadImage) {
Glide.with(mContext)
.load(uri)
.centerCrop()
.thumbnail(0.1f)
.placeholder(R.drawable.__picker_ic_photo_black_48dp)
.error(R.drawable.__picker_ic_broken_image_black_48dp)
.into(holder.ivPhoto);
if (canLoadImage) {
Glide.with(mContext)
.load(uri)
.centerCrop()
.thumbnail(0.1f)
.placeholder(R.drawable.__picker_ic_photo_black_48dp)
.error(R.drawable.__picker_ic_broken_image_black_48dp)
.into(holder.ivPhoto);
}
}
}


@Override public int getItemCount() {
return photoPaths.size();
int count = photoPaths.size() + 1;
if (count > MAX) {
count = MAX;
}
return count;
}

@Override
public int getItemViewType(int position) {
return (position == photoPaths.size() && position != MAX) ? TYPE_ADD : TYPE_PHOTO;
}

public static class PhotoViewHolder extends RecyclerView.ViewHolder {
private ImageView ivPhoto;
Expand All @@ -69,7 +92,7 @@ public PhotoViewHolder(View itemView) {
super(itemView);
ivPhoto = (ImageView) itemView.findViewById(R.id.iv_photo);
vSelected = itemView.findViewById(R.id.v_selected);
vSelected.setVisibility(View.GONE);
if (vSelected != null) vSelected.setVisibility(View.GONE);
}
}

Expand Down
7 changes: 7 additions & 0 deletions photopickerdemo/src/main/res/drawable/bg_item_photo.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">

<stroke
android:width="1dip"
android:color="@color/__picker_item_photo_border_n" />
</shape>
24 changes: 4 additions & 20 deletions photopickerdemo/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
<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"
tools:context=".MainActivity"
android:gravity="center_horizontal"
android:orientation="vertical"
>

<Button
Expand All @@ -11,54 +13,36 @@
android:layout_height="wrap_content"
android:text="@string/pick_photo"
android:id="@+id/button"
android:layout_centerHorizontal="true"
/>

<Button
android:layout_below="@id/button"
android:layout_marginTop="10dip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/pick_photo_without_camera"
android:id="@+id/button_no_camera"
android:layout_centerHorizontal="true"
/>

<Button
android:layout_below="@id/button_no_camera"
android:layout_marginTop="10dip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/pick_one_photo"
android:id="@+id/button_one_photo"
android:layout_centerHorizontal="true"
/>

<Button
android:layout_below="@id/button_one_photo"
android:layout_marginTop="10dip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/pick_photo_gif"
android:id="@+id/button_photo_gif"
android:layout_centerHorizontal="true"
/>

<Button
android:layout_below="@id/button_photo_gif"
android:layout_marginTop="10dip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/multiple_pick_photo"
android:id="@+id/button_multiple_picked"
android:layout_centerHorizontal="true"
/>

<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_below="@id/button_multiple_picked"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>

</RelativeLayout>
</LinearLayout>
18 changes: 18 additions & 0 deletions photopickerdemo/src/main/res/layout/item_add.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="utf-8"?>

<me.iwf.photopicker.widget.SquareItemLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="1dp"
android:background="@drawable/bg_item_photo">

<TextView
android:textSize="32dp"
android:text="+"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="match_parent" />

</me.iwf.photopicker.widget.SquareItemLayout>

0 comments on commit 3b68d00

Please sign in to comment.