display a grid of photos on a directory selection
This commit is contained in:
parent
6ee539eb52
commit
0ab046c8d5
9 changed files with 142 additions and 28 deletions
|
@ -24,7 +24,7 @@ public class MainActivity extends AppCompatActivity implements OnItemClickListen
|
|||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.activity_main);
|
||||
final GridView gridView = (GridView) findViewById(R.id.photo_grid);
|
||||
final GridView gridView = (GridView) findViewById(R.id.directories_grid);
|
||||
|
||||
dirs = new ArrayList<>(getDirectories().values());
|
||||
final DirectoryAdapter adapter = new DirectoryAdapter(this, dirs);
|
||||
|
@ -41,7 +41,8 @@ public class MainActivity extends AppCompatActivity implements OnItemClickListen
|
|||
if (cursor != null && cursor.moveToFirst()) {
|
||||
final int pathIndex = cursor.getColumnIndex(MediaStore.Images.Media.DATA);
|
||||
do {
|
||||
final File file = new File(cursor.getString(pathIndex));
|
||||
final String path = cursor.getString(pathIndex);
|
||||
final File file = new File(path);
|
||||
final String fileDir = file.getParent().toLowerCase();
|
||||
|
||||
if (directories.containsKey(fileDir)) {
|
||||
|
@ -49,9 +50,8 @@ public class MainActivity extends AppCompatActivity implements OnItemClickListen
|
|||
final int newImageCnt = directory.getPhotoCnt() + 1;
|
||||
directory.setPhotoCnt(newImageCnt);
|
||||
} else {
|
||||
final String thumbnail = file.getAbsolutePath();
|
||||
final String dirName = fileDir.substring(fileDir.lastIndexOf("/") + 1);
|
||||
directories.put(fileDir, new Directory(fileDir, thumbnail, dirName, 1));
|
||||
directories.put(fileDir, new Directory(fileDir, path, dirName, 1));
|
||||
}
|
||||
} while (cursor.moveToNext());
|
||||
cursor.close();
|
||||
|
|
|
@ -1,15 +1,51 @@
|
|||
package gallery.simplemobiletools.com;
|
||||
|
||||
import android.database.Cursor;
|
||||
import android.net.Uri;
|
||||
import android.os.Bundle;
|
||||
import android.provider.MediaStore;
|
||||
import android.support.v7.app.AppCompatActivity;
|
||||
import android.view.View;
|
||||
import android.widget.AdapterView;
|
||||
import android.widget.GridView;
|
||||
|
||||
public class PhotosActivity extends AppCompatActivity {
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class PhotosActivity extends AppCompatActivity implements AdapterView.OnItemClickListener {
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.activity_photos);
|
||||
|
||||
final GridView gridView = (GridView) findViewById(R.id.photos_grid);
|
||||
final PhotosAdapter adapter = new PhotosAdapter(this, getPhotos());
|
||||
gridView.setAdapter(adapter);
|
||||
gridView.setOnItemClickListener(this);
|
||||
}
|
||||
|
||||
private List<String> getPhotos() {
|
||||
final List<String> photos = new ArrayList<>();
|
||||
final String path = getIntent().getStringExtra(Constants.DIRECTORY);
|
||||
final Uri uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
|
||||
final String where = MediaStore.Images.Media.DATA + " like ? ";
|
||||
final String[] args = new String[]{path + "%"};
|
||||
final String[] columns = {MediaStore.Images.Media.DATA};
|
||||
final Cursor cursor = getContentResolver().query(uri, columns, where, args, null);
|
||||
|
||||
if (cursor != null && cursor.moveToFirst()) {
|
||||
final int pathIndex = cursor.getColumnIndex(MediaStore.Images.Media.DATA);
|
||||
do {
|
||||
photos.add(cursor.getString(pathIndex));
|
||||
} while (cursor.moveToNext());
|
||||
cursor.close();
|
||||
}
|
||||
return photos;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,64 @@
|
|||
package gallery.simplemobiletools.com;
|
||||
|
||||
import android.content.Context;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.BaseAdapter;
|
||||
import android.widget.ImageView;
|
||||
|
||||
import com.bumptech.glide.Glide;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class PhotosAdapter extends BaseAdapter {
|
||||
private final Context context;
|
||||
private final List<String> photos;
|
||||
private final LayoutInflater inflater;
|
||||
|
||||
public PhotosAdapter(Context context, List<String> photos) {
|
||||
this.context = context;
|
||||
this.photos = photos;
|
||||
this.inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
|
||||
}
|
||||
|
||||
@Override
|
||||
public View getView(int position, View view, ViewGroup parent) {
|
||||
ViewHolder holder;
|
||||
if (view == null) {
|
||||
view = inflater.inflate(R.layout.photo_item, parent, false);
|
||||
holder = new ViewHolder(view);
|
||||
view.setTag(holder);
|
||||
} else {
|
||||
holder = (ViewHolder) view.getTag();
|
||||
}
|
||||
|
||||
String path = photos.get(position);
|
||||
Glide.with(context).load(path).centerCrop().crossFade().into(holder.photoThumbnail);
|
||||
|
||||
return view;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getCount() {
|
||||
return photos.size();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object getItem(int position) {
|
||||
return photos.get(position);
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getItemId(int position) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
static class ViewHolder {
|
||||
ImageView photoThumbnail;
|
||||
|
||||
public ViewHolder(View view) {
|
||||
photoThumbnail = (ImageView) view.findViewById(R.id.photo_thumbnail);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,18 +1,12 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<RelativeLayout
|
||||
<GridView
|
||||
android:id="@+id/directories_grid"
|
||||
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:background="@android:color/black">
|
||||
|
||||
<GridView
|
||||
android:id="@+id/photo_grid"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:columnWidth="@dimen/photo_size"
|
||||
android:horizontalSpacing="2dp"
|
||||
android:numColumns="auto_fit"
|
||||
android:stretchMode="columnWidth"
|
||||
android:verticalSpacing="2dp"/>
|
||||
|
||||
</RelativeLayout>
|
||||
android:layout_height="wrap_content"
|
||||
android:background="@android:color/black"
|
||||
android:columnWidth="@dimen/dir_tmb_size"
|
||||
android:horizontalSpacing="1dp"
|
||||
android:numColumns="auto_fit"
|
||||
android:stretchMode="columnWidth"
|
||||
android:verticalSpacing="1dp"/>
|
||||
|
|
|
@ -1,8 +1,12 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<RelativeLayout
|
||||
<GridView
|
||||
android:id="@+id/photos_grid"
|
||||
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:background="@android:color/black">
|
||||
|
||||
</RelativeLayout>
|
||||
android:background="@android:color/black"
|
||||
android:columnWidth="@dimen/photo_tmb_size"
|
||||
android:horizontalSpacing="1dp"
|
||||
android:numColumns="auto_fit"
|
||||
android:stretchMode="columnWidth"
|
||||
android:verticalSpacing="1dp"/>
|
||||
|
|
|
@ -2,13 +2,13 @@
|
|||
<RelativeLayout
|
||||
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent">
|
||||
android:layout_height="match_parent"
|
||||
android:background="@color/tmb_background">
|
||||
|
||||
<gallery.simplemobiletools.com.MyImageView
|
||||
android:id="@+id/dir_thumbnail"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:background="@android:color/white"/>
|
||||
android:layout_height="match_parent"/>
|
||||
|
||||
<RelativeLayout
|
||||
android:layout_width="match_parent"
|
||||
|
@ -33,5 +33,6 @@
|
|||
android:layout_height="wrap_content"
|
||||
android:layout_below="@id/dir_name"
|
||||
android:textColor="@android:color/white"/>
|
||||
|
||||
</RelativeLayout>
|
||||
</RelativeLayout>
|
||||
|
|
13
app/src/main/res/layout/photo_item.xml
Normal file
13
app/src/main/res/layout/photo_item.xml
Normal file
|
@ -0,0 +1,13 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<FrameLayout
|
||||
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:background="@color/tmb_background">
|
||||
|
||||
<gallery.simplemobiletools.com.MyImageView
|
||||
android:id="@+id/photo_thumbnail"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"/>
|
||||
|
||||
</FrameLayout>
|
|
@ -3,4 +3,5 @@
|
|||
<color name="colorPrimary">#3F51B5</color>
|
||||
<color name="colorPrimaryDark">#303F9F</color>
|
||||
<color name="colorAccent">#FF4081</color>
|
||||
<color name="tmb_background">#ff222222</color>
|
||||
</resources>
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
<resources>
|
||||
<dimen name="activity_margin">16dp</dimen>
|
||||
<dimen name="photo_size">150dp</dimen>
|
||||
<dimen name="dir_tmb_size">150dp</dimen>
|
||||
<dimen name="photo_tmb_size">100dp</dimen>
|
||||
</resources>
|
||||
|
|
Loading…
Reference in a new issue