From 497ca6b1272aff4106fcb84072dcc5060f3cb688 Mon Sep 17 00:00:00 2001 From: tibbi Date: Sat, 20 Feb 2016 22:54:41 +0100 Subject: [PATCH] display the selected image fullscreen --- app/src/main/AndroidManifest.xml | 5 ++ .../simplemobiletools/com/Constants.java | 1 + .../simplemobiletools/com/MyPagerAdapter.java | 48 ++++++++++++++++ .../simplemobiletools/com/PhotoActivity.java | 55 +++++++++++++++++++ .../simplemobiletools/com/PhotosActivity.java | 8 ++- app/src/main/res/layout/activity_photo.xml | 13 +++++ app/src/main/res/layout/pager_item.xml | 6 ++ app/src/main/res/values/styles.xml | 2 - 8 files changed, 134 insertions(+), 4 deletions(-) create mode 100644 app/src/main/java/gallery/simplemobiletools/com/MyPagerAdapter.java create mode 100644 app/src/main/java/gallery/simplemobiletools/com/PhotoActivity.java create mode 100644 app/src/main/res/layout/activity_photo.xml create mode 100644 app/src/main/res/layout/pager_item.xml diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 9add12392..6d74d5509 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -24,5 +24,10 @@ + + diff --git a/app/src/main/java/gallery/simplemobiletools/com/Constants.java b/app/src/main/java/gallery/simplemobiletools/com/Constants.java index 7fb01ae80..f4e2fa40d 100644 --- a/app/src/main/java/gallery/simplemobiletools/com/Constants.java +++ b/app/src/main/java/gallery/simplemobiletools/com/Constants.java @@ -2,4 +2,5 @@ package gallery.simplemobiletools.com; public class Constants { public static final String DIRECTORY = "directory"; + public static final String PHOTO = "photo"; } diff --git a/app/src/main/java/gallery/simplemobiletools/com/MyPagerAdapter.java b/app/src/main/java/gallery/simplemobiletools/com/MyPagerAdapter.java new file mode 100644 index 000000000..c6ee8b644 --- /dev/null +++ b/app/src/main/java/gallery/simplemobiletools/com/MyPagerAdapter.java @@ -0,0 +1,48 @@ +package gallery.simplemobiletools.com; + +import android.content.Context; +import android.support.v4.view.PagerAdapter; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.ImageView; + +import com.bumptech.glide.Glide; + +import java.util.List; + +public class MyPagerAdapter extends PagerAdapter { + private final Context context; + private final List paths; + private final LayoutInflater inflater; + + public MyPagerAdapter(Context context, List paths) { + this.context = context; + this.paths = paths; + this.inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); + } + + @Override + public int getCount() { + return paths.size(); + } + + @Override + public boolean isViewFromObject(View view, Object object) { + return view == object; + } + + @Override + public Object instantiateItem(ViewGroup container, int position) { + final View view = inflater.inflate(R.layout.pager_item, container, false); + final ImageView imageView = (ImageView) view.findViewById(R.id.photo); + Glide.with(context).load(paths.get(position)).fitCenter().crossFade().into(imageView); + container.addView(view); + return view; + } + + @Override + public void destroyItem(ViewGroup container, int position, Object object) { + container.removeView((View) object); + } +} diff --git a/app/src/main/java/gallery/simplemobiletools/com/PhotoActivity.java b/app/src/main/java/gallery/simplemobiletools/com/PhotoActivity.java new file mode 100644 index 000000000..9c5dc849b --- /dev/null +++ b/app/src/main/java/gallery/simplemobiletools/com/PhotoActivity.java @@ -0,0 +1,55 @@ +package gallery.simplemobiletools.com; + +import android.database.Cursor; +import android.net.Uri; +import android.os.Bundle; +import android.provider.MediaStore; +import android.support.v4.view.ViewPager; +import android.support.v7.app.AppCompatActivity; + +import java.io.File; +import java.util.ArrayList; +import java.util.List; + +public class PhotoActivity extends AppCompatActivity { + private int pos; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_photo); + + pos = 0; + final ViewPager pager = (ViewPager) findViewById(R.id.view_pager); + final MyPagerAdapter adapter = new MyPagerAdapter(this, getPhotos()); + pager.setAdapter(adapter); + pager.setCurrentItem(pos); + } + + private List getPhotos() { + final List photos = new ArrayList<>(); + final String path = getIntent().getStringExtra(Constants.PHOTO); + final String fileDir = new File(path).getParent().toLowerCase(); + final Uri uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI; + final String where = MediaStore.Images.Media.DATA + " like ? "; + final String[] args = new String[]{fileDir + "%"}; + final String[] columns = {MediaStore.Images.Media.DATA}; + final Cursor cursor = getContentResolver().query(uri, columns, where, args, null); + + int i = 0; + if (cursor != null && cursor.moveToFirst()) { + final int pathIndex = cursor.getColumnIndex(MediaStore.Images.Media.DATA); + do { + final String curPath = cursor.getString(pathIndex); + photos.add(curPath); + + if (curPath.equals(path)) + pos = i; + + i++; + } while (cursor.moveToNext()); + cursor.close(); + } + return photos; + } +} diff --git a/app/src/main/java/gallery/simplemobiletools/com/PhotosActivity.java b/app/src/main/java/gallery/simplemobiletools/com/PhotosActivity.java index 2ab280ac7..aff7ea72e 100644 --- a/app/src/main/java/gallery/simplemobiletools/com/PhotosActivity.java +++ b/app/src/main/java/gallery/simplemobiletools/com/PhotosActivity.java @@ -1,5 +1,6 @@ package gallery.simplemobiletools.com; +import android.content.Intent; import android.database.Cursor; import android.net.Uri; import android.os.Bundle; @@ -13,12 +14,14 @@ import java.util.ArrayList; import java.util.List; public class PhotosActivity extends AppCompatActivity implements AdapterView.OnItemClickListener { + private List photos; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_photos); + photos = new ArrayList<>(); final GridView gridView = (GridView) findViewById(R.id.photos_grid); final PhotosAdapter adapter = new PhotosAdapter(this, getPhotos()); gridView.setAdapter(adapter); @@ -26,7 +29,6 @@ public class PhotosActivity extends AppCompatActivity implements AdapterView.OnI } private List getPhotos() { - final List 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 ? "; @@ -46,6 +48,8 @@ public class PhotosActivity extends AppCompatActivity implements AdapterView.OnI @Override public void onItemClick(AdapterView parent, View view, int position, long id) { - + final Intent intent = new Intent(this, PhotoActivity.class); + intent.putExtra(Constants.PHOTO, photos.get(position)); + startActivity(intent); } } diff --git a/app/src/main/res/layout/activity_photo.xml b/app/src/main/res/layout/activity_photo.xml new file mode 100644 index 000000000..0638d871f --- /dev/null +++ b/app/src/main/res/layout/activity_photo.xml @@ -0,0 +1,13 @@ + + + + + + diff --git a/app/src/main/res/layout/pager_item.xml b/app/src/main/res/layout/pager_item.xml new file mode 100644 index 000000000..399c2e089 --- /dev/null +++ b/app/src/main/res/layout/pager_item.xml @@ -0,0 +1,6 @@ + + diff --git a/app/src/main/res/values/styles.xml b/app/src/main/res/values/styles.xml index 5885930df..b4390a216 100644 --- a/app/src/main/res/values/styles.xml +++ b/app/src/main/res/values/styles.xml @@ -1,8 +1,6 @@ -