fixed some image cannot display

This commit is contained in:
Nereo 2015-05-05 15:24:03 +08:00
parent 45d1ec1fc8
commit fdc51aa637
12 changed files with 216 additions and 70 deletions

View File

@ -106,9 +106,10 @@ class CustomerActivity extends Activity implements MultiImageSelectorFragment.Ca
###Change Log
* 2015-4-9
1. Fixed. When set `EXTRA_SHOW_CAMERA` to `true`, the first grid item onclick event were messed.
2. Add. Support initial selected image list.
* 2015-5-5
1. Fixed. Can't display some images. (Issue by[sd6352051](https://github.com/sd6352051), [larry](https://github.com/18611480882))
2. Fixed. `ListPopupWindow` can not fill parent
3. Added. Add checked mask.
* 2015-4-16
1. Fixed. Crack when rotate device. (Issue by [@Leminity](https://github.com/Leminity))
@ -116,6 +117,10 @@ class CustomerActivity extends Activity implements MultiImageSelectorFragment.Ca
3. Change. Demo application shortcut.
4. Change. Readme file.
* 2015-4-9
1. Fixed. When set `EXTRA_SHOW_CAMERA` to `true`, the first grid item onclick event were messed.
2. Add. Support initial selected image list.
-------------------
###Thanks

View File

@ -106,9 +106,10 @@ class CustomerActivity extends Activity implements MultiImageSelectorFragment.Ca
###更新日志
* 2015-4-9
1. 修复. 当设置 `EXTRA_SHOW_CAMERA``true` 时, 点击第一个Item会混乱的问题.
2. 新增. 支持初始化图片选择设定。
* 2015-5-5
1. 修复. 某些图片无法显示. (Issue by[sd6352051](https://github.com/sd6352051), [larry](https://github.com/18611480882))
2. 修复. `ListPopupWindow` 无法填充父控件
3. 新增. 选中的遮罩效果.
* 2015-4-16
1. 修复. 旋转设备时,程序会崩溃. (Issue by [@Leminity](https://github.com/Leminity))
@ -116,6 +117,11 @@ class CustomerActivity extends Activity implements MultiImageSelectorFragment.Ca
3. 更改. 演示程序截图.
4. 更改. Readme 文件.
* 2015-4-9
1. 修复. 当设置 `EXTRA_SHOW_CAMERA``true` 时, 点击第一个Item会混乱的问题.
2. 新增. 支持初始化图片选择设定。
-------------------
###感谢

View File

@ -21,5 +21,5 @@ android {
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:22.0.0'
compile 'com.squareup.picasso:picasso:2.5.2'
compile 'com.squareup.picasso:picasso:2.4.0'
}

View File

@ -90,6 +90,6 @@
<orderEntry type="library" exported="" name="support-v4-22.0.0" level="project" />
<orderEntry type="library" exported="" name="support-annotations-22.0.0" level="project" />
<orderEntry type="library" exported="" name="appcompat-v7-22.0.0" level="project" />
<orderEntry type="library" exported="" name="picasso-2.5.2" level="project" />
<orderEntry type="library" exported="" name="picasso-2.4.0" level="project" />
</component>
</module>

View File

@ -5,9 +5,12 @@ import android.app.Activity;
import android.content.Intent;
import android.content.res.Configuration;
import android.database.Cursor;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.provider.MediaStore;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
@ -93,6 +96,9 @@ public class MultiImageSelectorFragment extends Fragment {
private int mDesireImageCount;
private boolean hasFolderGened = false;
private boolean mIsShowCamera = false;
private int mGridWidth, mGridHeight;
private File mTmpFile;
@ -130,16 +136,11 @@ public class MultiImageSelectorFragment extends Fragment {
}
// 是否显示照相机
final boolean showCamera = getArguments().getBoolean(EXTRA_SHOW_CAMERA, true);
mImageAdapter = new ImageGridAdapter(getActivity(), showCamera);
mIsShowCamera = getArguments().getBoolean(EXTRA_SHOW_CAMERA, true);
mImageAdapter = new ImageGridAdapter(getActivity(), mIsShowCamera);
// 是否显示选择指示器
mImageAdapter.showSelectIndicator(mode == MODE_MULTI);
// 如果显示了照相机则创建临时文件
if(showCamera){
mTmpFile = FileUtils.createTmpFile(getActivity());
}
mPopupAnchorView = view.findViewById(R.id.footer);
mTimeLineText = (TextView) view.findViewById(R.id.timeline_area);
@ -152,9 +153,14 @@ public class MultiImageSelectorFragment extends Fragment {
mCategoryText.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(mFolderPopupWindow.isShowing()){
if(mFolderPopupWindow == null){
createPopupFolderList(mGridWidth, mGridHeight);
}
if (mFolderPopupWindow.isShowing()) {
mFolderPopupWindow.dismiss();
}else {
} else {
mFolderPopupWindow.show();
int index = mFolderAdapter.getSelectIndex();
index = index == 0 ? index : index - 1;
@ -215,16 +221,15 @@ public class MultiImageSelectorFragment extends Fragment {
final int width = mGridView.getWidth();
final int height = mGridView.getHeight();
mGridWidth = width;
mGridHeight = height;
final int desireSize = getResources().getDimensionPixelOffset(R.dimen.image_size);
final int numCount = width / desireSize;
final int columnSpace = getResources().getDimensionPixelOffset(R.dimen.space_size);
int columnWidth = (width - columnSpace*(numCount-1)) / numCount;
mImageAdapter.setItemSize(columnWidth);
if(mFolderPopupWindow == null){
createPopupFolderList(width, height);
}
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN){
mGridView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
}else{
@ -260,33 +265,49 @@ public class MultiImageSelectorFragment extends Fragment {
*/
private void createPopupFolderList(int width, int height) {
mFolderPopupWindow = new ListPopupWindow(getActivity());
mFolderPopupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
mFolderPopupWindow.setAdapter(mFolderAdapter);
mFolderPopupWindow.setContentWidth(width);
mFolderPopupWindow.setHeight(height * 5/8);
mFolderPopupWindow.setWidth(width);
mFolderPopupWindow.setHeight(height * 5 / 8);
mFolderPopupWindow.setAnchorView(mPopupAnchorView);
mFolderPopupWindow.setModal(true);
mFolderPopupWindow.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
if (i == 0) {
getActivity().getSupportLoaderManager().restartLoader(LOADER_ALL, null, mLoaderCallback);
mCategoryText.setText(R.string.folder_all);
mImageAdapter.setShowCamera(true);
} else {
Folder folder = (Folder) adapterView.getAdapter().getItem(i);
if (null != folder) {
Bundle args = new Bundle();
args.putString("path", folder.path);
getActivity().getSupportLoaderManager().restartLoader(LOADER_CATEGORY, args, mLoaderCallback);
mCategoryText.setText(folder.name);
}
mImageAdapter.setShowCamera(false);
}
mFolderAdapter.setSelectIndex(i);
mFolderPopupWindow.dismiss();
// 滑动到最初始位置
mGridView.smoothScrollToPosition(0);
mFolderAdapter.setSelectIndex(i);
final int index = i;
final AdapterView v = adapterView;
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
mFolderPopupWindow.dismiss();
if (index == 0) {
getActivity().getSupportLoaderManager().restartLoader(LOADER_ALL, null, mLoaderCallback);
mCategoryText.setText(R.string.folder_all);
if (mIsShowCamera) {
mImageAdapter.setShowCamera(true);
} else {
mImageAdapter.setShowCamera(false);
}
} else {
Folder folder = (Folder) v.getAdapter().getItem(index);
if (null != folder) {
mImageAdapter.setData(folder.images);
mCategoryText.setText(folder.name);
}
mImageAdapter.setShowCamera(false);
}
// 滑动到最初始位置
mGridView.smoothScrollToPosition(0);
}
}, 100);
}
});
}
@ -303,13 +324,17 @@ public class MultiImageSelectorFragment extends Fragment {
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// 相机拍照完成后返回图片路径
if(resultCode == Activity.RESULT_OK) {
if(requestCode == REQUEST_CAMERA){
if(requestCode == REQUEST_CAMERA){
if(resultCode == Activity.RESULT_OK) {
if (mTmpFile != null) {
if (mCallback != null) {
mCallback.onCameraShot(mTmpFile);
}
}
}else{
if(mTmpFile != null && mTmpFile.exists()){
mTmpFile.delete();
}
}
}
}
@ -318,8 +343,6 @@ public class MultiImageSelectorFragment extends Fragment {
public void onConfigurationChanged(Configuration newConfig) {
Log.d(TAG, "on change");
final int orientation = newConfig.orientation;
if(mFolderPopupWindow != null){
if(mFolderPopupWindow.isShowing()){
mFolderPopupWindow.dismiss();
@ -327,27 +350,28 @@ public class MultiImageSelectorFragment extends Fragment {
}
mGridView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
@Override
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
public void onGlobalLayout() {
final int height = mGridView.getHeight();
final int desireSize = getResources().getDimensionPixelOffset(R.dimen.image_size);
Log.d(TAG, "Desire Size = "+desireSize);
Log.d(TAG, "Desire Size = " + desireSize);
final int numCount = mGridView.getWidth() / desireSize;
Log.d(TAG, "Grid Size = "+mGridView.getWidth());
Log.d(TAG, "num count = "+numCount);
Log.d(TAG, "Grid Size = " + mGridView.getWidth());
Log.d(TAG, "num count = " + numCount);
final int columnSpace = getResources().getDimensionPixelOffset(R.dimen.space_size);
int columnWidth = (mGridView.getWidth() - columnSpace*(numCount-1)) / numCount;
int columnWidth = (mGridView.getWidth() - columnSpace * (numCount - 1)) / numCount;
mImageAdapter.setItemSize(columnWidth);
if(mFolderPopupWindow != null){
mFolderPopupWindow.setHeight(height * 5/8);
if (mFolderPopupWindow != null) {
mFolderPopupWindow.setHeight(height * 5 / 8);
}
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN){
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
mGridView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
}else{
} else {
mGridView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
}
}
@ -365,6 +389,8 @@ public class MultiImageSelectorFragment extends Fragment {
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if(cameraIntent.resolveActivity(getActivity().getPackageManager()) != null){
// 设置系统相机拍照后的输出路径
// 创建临时文件
mTmpFile = FileUtils.createTmpFile(getActivity());
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(mTmpFile));
startActivityForResult(cameraIntent, REQUEST_CAMERA);
}else{
@ -500,9 +526,9 @@ public class MultiImageSelectorFragment extends Fragment {
* 回调接口
*/
public interface Callback{
public void onSingleImageSelected(String path);
public void onImageSelected(String path);
public void onImageUnselected(String path);
public void onCameraShot(File imageFile);
void onSingleImageSelected(String path);
void onImageSelected(String path);
void onImageUnselected(String path);
void onCameraShot(File imageFile);
}
}

View File

@ -95,7 +95,7 @@ public class FolderAdapter extends BaseAdapter {
if(lastSelected == i){
holder.indicator.setVisibility(View.VISIBLE);
}else{
holder.indicator.setVisibility(View.GONE);
holder.indicator.setVisibility(View.INVISIBLE);
}
}
return view;

View File

@ -208,10 +208,12 @@ public class ImageGridAdapter extends BaseAdapter {
class ViewHolde {
ImageView image;
ImageView indicator;
View mask;
ViewHolde(View view){
image = (ImageView) view.findViewById(R.id.image);
indicator = (ImageView) view.findViewById(R.id.checkmark);
mask = view.findViewById(R.id.mask);
view.setTag(this);
}
@ -223,9 +225,11 @@ public class ImageGridAdapter extends BaseAdapter {
if(mSelectedImages.contains(data)){
// 设置选中状态
indicator.setImageResource(R.drawable.btn_selected);
mask.setVisibility(View.VISIBLE);
}else{
// 未选择
indicator.setImageResource(R.drawable.btn_unselected);
mask.setVisibility(View.GONE);
}
}else{
indicator.setVisibility(View.GONE);

View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="#CDCECE" android:state_pressed="false" />
<item android:color="#232323" android:state_pressed="true" />
<item android:color="#232323" android:state_selected="true" />
<item android:color="#232323" android:state_checked="true" />
<item android:color="#CDCECE" />
</selector>

View File

@ -0,0 +1,82 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:background="@android:color/black"
android:layout_width="match_parent"
android:layout_height="match_parent">
<GridView
android:id="@+id/grid"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:horizontalSpacing="@dimen/space_size"
android:verticalSpacing="@dimen/space_size"
android:paddingBottom="?android:attr/actionBarSize"
android:clipToPadding="false"
android:numColumns="auto_fit"
android:columnWidth="@dimen/image_size"/>
<TextView
android:id="@+id/timeline_area"
tools:text="2015年4月1日"
android:textColor="#CDCECE"
android:textSize="14sp"
android:paddingLeft="10sp"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:background="#cc000000"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<RelativeLayout
android:clickable="true"
android:id="@+id/footer"
android:background="#cc000000"
android:layout_alignParentBottom="true"
android:layout_width="match_parent"
android:layout_height="?android:attr/actionBarSize">
<Button
android:id="@+id/category_btn"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:layout_centerVertical="true"
android:textColor="@color/folder_text_color"
tools:text="所有图片"
android:textSize="16sp"
android:gravity="center_vertical"
android:drawableRight="@drawable/text_indicator"
android:drawablePadding="5dp"
android:background="@null"
android:singleLine="true"
android:ellipsize="end"
android:textAllCaps="false"
android:layout_width="wrap_content"
android:layout_height="match_parent" />
<View
android:visibility="gone"
android:layout_toLeftOf="@+id/preview"
android:layout_width="1dp"
android:background="#8828292A"
android:layout_height="match_parent" />
<Button
android:visibility="gone"
android:id="@+id/preview"
tools:text="预览(1)"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:minHeight="1dp"
android:minWidth="1dp"
android:background="@null"
android:textColor="@color/default_text_color"
android:textSize="14sp"
android:layout_width="wrap_content"
android:layout_height="match_parent" />
</RelativeLayout>
</RelativeLayout>

View File

@ -31,28 +31,30 @@
<RelativeLayout
android:clickable="true"
android:id="@+id/footer"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:background="#cc000000"
android:layout_alignParentBottom="true"
android:layout_width="match_parent"
android:layout_height="?android:attr/actionBarSize">
<TextView
<Button
android:id="@+id/category_btn"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:layout_centerVertical="true"
android:textColor="#CDCECE"
android:textColor="@color/folder_text_color"
tools:text="所有图片"
android:textSize="16sp"
android:gravity="center_vertical"
android:drawableRight="@drawable/text_indicator"
android:drawablePadding="5dp"
android:background="@null"
android:singleLine="true"
android:ellipsize="end"
android:layout_width="wrap_content"
android:layout_height="match_parent" />
<View
android:visibility="gone"
android:layout_toLeftOf="@+id/preview"
android:layout_width="1dp"
android:background="#8828292A"

View File

@ -1,16 +1,16 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:background="@android:color/white"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
android:layout_height="wrap_content">
<ImageView
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:id="@+id/cover"
tools:src="@drawable/btn_back"
android:layout_gravity="center_vertical"
@ -20,11 +20,13 @@
android:layout_height="@dimen/folder_cover_size" />
<LinearLayout
android:layout_toRightOf="@+id/cover"
android:layout_toLeftOf="@+id/indicator"
android:layout_centerVertical="true"
android:layout_marginLeft="16dp"
android:orientation="vertical"
android:layout_gravity="center_vertical"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
@ -36,6 +38,8 @@
android:layout_height="wrap_content" />
<TextView
android:singleLine="true"
android:ellipsize="end"
android:id="@+id/size"
tools:text="1张"
android:layout_marginTop="5dp"
@ -47,12 +51,14 @@
</LinearLayout>
<ImageView
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:id="@+id/indicator"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginRight="20dp"
android:src="@drawable/default_check"
android:layout_gravity="center_vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</RelativeLayout>

View File

@ -9,6 +9,13 @@
android:layout_width="match_parent"
android:layout_height="match_parent" />
<View
android:visibility="gone"
android:id="@+id/mask"
android:background="#88000000"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<ImageView
android:id="@+id/checkmark"
android:layout_gravity="top|right"