2014-03-24 09:48:19 -04:00
|
|
|
package com.example.hw4;
|
|
|
|
|
2014-03-25 16:08:10 -04:00
|
|
|
/*
|
|
|
|
* Bradlee Speice, Brandon Rodenmayer
|
|
|
|
* ITIS 4180
|
|
|
|
* Homework 4
|
|
|
|
* GalleryActivity.java
|
|
|
|
*/
|
|
|
|
|
2014-03-24 14:41:32 -04:00
|
|
|
import java.net.URL;
|
|
|
|
|
2014-03-24 10:02:07 -04:00
|
|
|
import android.os.AsyncTask;
|
2014-03-24 09:48:19 -04:00
|
|
|
import android.os.Bundle;
|
|
|
|
import android.app.Activity;
|
2014-03-24 14:41:32 -04:00
|
|
|
import android.app.ProgressDialog;
|
2014-03-24 10:02:07 -04:00
|
|
|
import android.graphics.Bitmap;
|
2014-03-24 14:41:32 -04:00
|
|
|
import android.graphics.BitmapFactory;
|
2014-03-25 17:23:57 -04:00
|
|
|
import android.util.Log;
|
|
|
|
import android.view.GestureDetector.SimpleOnGestureListener;
|
|
|
|
import android.view.GestureDetector;
|
2014-03-24 09:48:19 -04:00
|
|
|
import android.view.Menu;
|
2014-03-25 17:23:57 -04:00
|
|
|
import android.view.MotionEvent;
|
2014-03-24 10:02:07 -04:00
|
|
|
import android.view.View;
|
2014-03-24 14:41:32 -04:00
|
|
|
import android.widget.ImageView;
|
2014-03-24 09:48:19 -04:00
|
|
|
|
|
|
|
public class ImageViewerActivity extends Activity {
|
2014-03-24 10:02:07 -04:00
|
|
|
|
|
|
|
int urlsId;
|
|
|
|
String[] urls;
|
|
|
|
int currentIndex;
|
2014-03-24 14:41:32 -04:00
|
|
|
|
|
|
|
ProgressDialog dialog;
|
2014-03-24 09:48:19 -04:00
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onCreate(Bundle savedInstanceState) {
|
|
|
|
super.onCreate(savedInstanceState);
|
|
|
|
setContentView(R.layout.activity_image_viewer);
|
2014-03-24 10:02:07 -04:00
|
|
|
|
|
|
|
Bundle extras = getIntent().getExtras();
|
|
|
|
urlsId = extras.getInt("urls");
|
|
|
|
urls = getResources().getStringArray(urlsId);
|
|
|
|
currentIndex = extras.getInt("index");
|
2014-03-24 14:41:32 -04:00
|
|
|
|
2014-03-25 17:23:57 -04:00
|
|
|
// Code modified from below, updated to use non-deprecated parts of API
|
|
|
|
// http://stackoverflow.com/questions/4098198/adding-fling-gesture-to-an-image-view-android
|
|
|
|
final GestureDetector gdt = new GestureDetector(this, new ImageGestures());
|
|
|
|
findViewById(R.id.imgViewer).setOnTouchListener(new View.OnTouchListener() {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onTouch(View arg0, MotionEvent arg1) {
|
|
|
|
// To be honest, reading documentation at:
|
|
|
|
// http://developer.android.com/reference/android/view/GestureDetector.OnGestureListener.html
|
|
|
|
// http://developer.android.com/reference/android/view/View.OnTouchListener.html
|
|
|
|
// would lead me to think that we should just return gdt.onTouchEvent(arg1).
|
|
|
|
// However, as someone pointed out on StackOverflow above, they need to be inverted. Going
|
|
|
|
// to ask professor about that tonight.
|
|
|
|
return !gdt.onTouchEvent(arg1);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2014-03-24 14:41:32 -04:00
|
|
|
displayImage(currentIndex);
|
2014-03-24 09:48:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onCreateOptionsMenu(Menu menu) {
|
|
|
|
// Inflate the menu; this adds items to the action bar if it is present.
|
|
|
|
getMenuInflater().inflate(R.menu.image_viewer, menu);
|
|
|
|
return true;
|
|
|
|
}
|
2014-03-24 10:02:07 -04:00
|
|
|
|
|
|
|
public void onClickPrev(View v) {
|
2014-03-24 10:07:09 -04:00
|
|
|
displayPrev();
|
2014-03-24 10:02:07 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
public void onClickNext(View v) {
|
2014-03-24 10:07:09 -04:00
|
|
|
displayNext();
|
2014-03-24 10:02:07 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
public void onClickBack(View v) {
|
|
|
|
finish();
|
|
|
|
}
|
|
|
|
|
2014-03-24 10:07:09 -04:00
|
|
|
private void displayPrev() {
|
2014-03-24 14:41:32 -04:00
|
|
|
// Wrapping backwards requires more than just modulo
|
|
|
|
if (currentIndex == 0)
|
|
|
|
currentIndex = urls.length - 1;
|
|
|
|
else
|
|
|
|
currentIndex -= 1;
|
|
|
|
|
2014-03-24 10:07:09 -04:00
|
|
|
displayImage(currentIndex);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void displayNext() {
|
2014-03-24 14:41:32 -04:00
|
|
|
currentIndex = (currentIndex + 1) % urls.length;
|
2014-03-24 10:07:09 -04:00
|
|
|
displayImage(currentIndex);
|
|
|
|
}
|
|
|
|
|
2014-03-24 10:02:07 -04:00
|
|
|
public void displayImage(int indexToDisplay) {
|
2014-03-24 14:41:32 -04:00
|
|
|
ImageView view = (ImageView)findViewById(R.id.imgViewer);
|
|
|
|
view.setImageBitmap(null);
|
|
|
|
new ImageDownloader().execute(urls[indexToDisplay]);
|
|
|
|
dialog = new ProgressDialog(this);
|
|
|
|
dialog.setCancelable(false);
|
2014-03-25 17:23:57 -04:00
|
|
|
dialog.setMessage("Loading...");
|
2014-03-24 14:41:32 -04:00
|
|
|
dialog.show();
|
2014-03-24 10:02:07 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
private class ImageDownloader extends AsyncTask<String, Void, Bitmap> {
|
|
|
|
@Override
|
|
|
|
protected Bitmap doInBackground(String... params) {
|
2014-03-24 14:41:32 -04:00
|
|
|
try
|
|
|
|
{
|
|
|
|
URL imageUrl = new URL(params[0]);
|
|
|
|
return BitmapFactory.decodeStream(imageUrl.openStream());
|
|
|
|
}
|
|
|
|
catch(Exception e)
|
|
|
|
{
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
|
2014-03-24 10:02:07 -04:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onPostExecute(Bitmap result) {
|
2014-03-24 14:41:32 -04:00
|
|
|
dialog.cancel();
|
|
|
|
ImageView view = (ImageView)findViewById(R.id.imgViewer);
|
|
|
|
view.setImageBitmap(result);
|
2014-03-24 10:02:07 -04:00
|
|
|
}
|
|
|
|
}
|
2014-03-25 17:23:57 -04:00
|
|
|
|
|
|
|
// Gesture detection from:
|
|
|
|
// https://developer.android.com/training/gestures/detector.html
|
|
|
|
// http://stackoverflow.com/questions/4098198/adding-fling-gesture-to-an-image-view-android
|
|
|
|
// Please note: Gesture recognition code borrowed heavily from StackOverflow. Because there's so
|
|
|
|
// little actual code, not sure there's any way to re-do this code that won't look like plagiarism.
|
|
|
|
|
|
|
|
private static final int SWIPE_MIN_DISTANCE = 120;
|
|
|
|
private static final int SWIPE_THRESHOLD_VELOCITY = 200;
|
|
|
|
|
|
|
|
private class ImageGestures extends SimpleOnGestureListener {
|
|
|
|
@Override
|
|
|
|
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
|
|
|
|
if(e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
|
|
|
|
displayPrev();
|
|
|
|
return true;
|
|
|
|
} else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
|
|
|
|
displayNext();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2014-03-24 09:48:19 -04:00
|
|
|
|
|
|
|
}
|