From 70bd2502c34e9852301400b68873740f005be4b7 Mon Sep 17 00:00:00 2001 From: Joe Robinson Date: Sun, 3 Aug 2014 04:09:01 +0100 Subject: Fixed link markdown in readme --- README.md | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 7f575f5..a0f19f9 100644 --- a/README.md +++ b/README.md @@ -23,15 +23,14 @@ Reasons Libraries Used ============== -- (Otto)[http://square.github.io/otto/] - Event Bus library for passing - data between threads +- [Otto](http://square.github.io/otto/) - Event Bus library for passing data between threads Tutorials/Resources Used ======================== -- (Javatechig - Image GridView)[http://javatechig.com/android/android-gridview-example-building-image-gallery-in-android] -- (Vogella - AsyncTask)[http://www.vogella.com/tutorials/AndroidBackgroundProcessing/article.html#concurrency_asynchtask3] -- (Volegga - JSON in Android)[http://www.vogella.com/tutorials/AndroidJSON/article.html] -- (Volegga - ListViews and Adapters)[http://www.vogella.com/tutorials/AndroidListView/article.html] -- (Baeldung - Apache HttpClient cookies)[http://www.baeldung.com/httpclient-4-cookies] -- (SimonVT - Using Otto for AsyncTask)[http://simonvt.net/2014/04/17/asynctask-is-bad-and-you-should-feel-bad/] +- [Javatechig - Image GridView](http://javatechig.com/android/android-gridview-example-building-image-gallery-in-android) +- [Vogella - AsyncTask](http://www.vogella.com/tutorials/AndroidBackgroundProcessing/article.html#concurrency_asynchtask3) +- [Volegga - JSON in Android](http://www.vogella.com/tutorials/AndroidJSON/article.html) +- [Volegga - ListViews and Adapters](http://www.vogella.com/tutorials/AndroidListView/article.html) +- [Baeldung - Apache HttpClient cookies](http://www.baeldung.com/httpclient-4-cookies) +- [SimonVT - Using Otto for AsyncTask](http://simonvt.net/2014/04/17/asynctask-is-bad-and-you-should-feel-bad/) \ No newline at end of file -- cgit v1.2.3 From a08d58be07cb5e5ed4b0265824af2d69e29a2bf0 Mon Sep 17 00:00:00 2001 From: Joe Robinson Date: Mon, 4 Aug 2014 02:17:22 +0100 Subject: Added partially working image viewer with paging --- .idea/libraries/Android_BitmapCache_2_3.xml | 9 + .idea/libraries/disklrucache_2_0_2.xml | 11 + app/app.iml | 4 +- app/build.gradle | 5 +- app/src/main/AndroidManifest.xml | 16 +- .../co/blatech/blaupload/BlauploadApplication.java | 50 ++++ .../co/blatech/blaupload/HomeScreenFragment.java | 21 +- .../java/uk/co/blatech/blaupload/ImagePager.java | 85 +++++++ .../blatech/blaupload/ui/DepthPageTransformer.java | 41 ++++ .../co/blatech/blaupload/ui/ImagePagerAdapter.java | 66 +++++ .../blaupload/ui/NetworkCacheableImageView.java | 271 +++++++++++++++++++++ .../uk/co/blatech/blaupload/ui/ZoomViewPager.java | 75 ++++++ .../uk/co/blatech/blaupload/util/ImageLoader.java | 54 +++- .../uk/co/blatech/blaupload/util/JSONLoader.java | 2 +- app/src/main/res/layout/activity_image_pager.xml | 16 ++ app/src/main/res/layout/activity_image_viewer.xml | 7 + app/src/main/res/layout/fragment_image_viewer.xml | 13 + app/src/main/res/menu/image_pager.xml | 8 + app/src/main/res/menu/image_viewer.xml | 8 + app/src/main/res/values/strings.xml | 3 + 20 files changed, 749 insertions(+), 16 deletions(-) create mode 100644 .idea/libraries/Android_BitmapCache_2_3.xml create mode 100644 .idea/libraries/disklrucache_2_0_2.xml create mode 100644 app/src/main/java/uk/co/blatech/blaupload/BlauploadApplication.java create mode 100644 app/src/main/java/uk/co/blatech/blaupload/ImagePager.java create mode 100644 app/src/main/java/uk/co/blatech/blaupload/ui/DepthPageTransformer.java create mode 100644 app/src/main/java/uk/co/blatech/blaupload/ui/ImagePagerAdapter.java create mode 100644 app/src/main/java/uk/co/blatech/blaupload/ui/NetworkCacheableImageView.java create mode 100644 app/src/main/java/uk/co/blatech/blaupload/ui/ZoomViewPager.java create mode 100644 app/src/main/res/layout/activity_image_pager.xml create mode 100644 app/src/main/res/layout/activity_image_viewer.xml create mode 100644 app/src/main/res/layout/fragment_image_viewer.xml create mode 100644 app/src/main/res/menu/image_pager.xml create mode 100644 app/src/main/res/menu/image_viewer.xml diff --git a/.idea/libraries/Android_BitmapCache_2_3.xml b/.idea/libraries/Android_BitmapCache_2_3.xml new file mode 100644 index 0000000..34c06f6 --- /dev/null +++ b/.idea/libraries/Android_BitmapCache_2_3.xml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/disklrucache_2_0_2.xml b/.idea/libraries/disklrucache_2_0_2.xml new file mode 100644 index 0000000..45145e6 --- /dev/null +++ b/.idea/libraries/disklrucache_2_0_2.xml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/app/app.iml b/app/app.iml index ad43377..1556047 100644 --- a/app/app.iml +++ b/app/app.iml @@ -59,11 +59,13 @@ - + + + diff --git a/app/build.gradle b/app/build.gradle index cf01a63..d099f61 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -2,7 +2,7 @@ apply plugin: 'com.android.application' android { compileSdkVersion 19 - buildToolsVersion "20.0.0" + buildToolsVersion '20.0.0' defaultConfig { applicationId "uk.co.blatech.blaupload" @@ -25,5 +25,6 @@ dependencies { compile 'com.android.support:support-v4:19.+' compile 'com.squareup:otto:1.3.5' compile 'com.github.chrisbanes.photoview:library:1.2.3' - + compile 'com.jakewharton:disklrucache:2.0.2' + compile files('libs/Android-BitmapCache-2.3.jar') } diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 2700bb3..0b63b69 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -1,8 +1,11 @@ - + + + + + + + + + + diff --git a/app/src/main/java/uk/co/blatech/blaupload/BlauploadApplication.java b/app/src/main/java/uk/co/blatech/blaupload/BlauploadApplication.java new file mode 100644 index 0000000..518eff8 --- /dev/null +++ b/app/src/main/java/uk/co/blatech/blaupload/BlauploadApplication.java @@ -0,0 +1,50 @@ +package uk.co.blatech.blaupload; + +import android.app.Application; +import android.content.Context; +import android.os.Environment; +import android.util.Log; + +import java.io.File; + +import uk.co.senab.bitmapcache.BitmapLruCache; + +public class BlauploadApplication extends Application { + + private BitmapLruCache mCache; + + @Override + public void onCreate() { + super.onCreate(); + + File cacheLocation; + + // If we have external storage use it for the disk cache. Otherwise we use + // the cache dir + if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) { + cacheLocation = new File( + Environment.getExternalStorageDirectory() + "/blaupload"); + } else { + cacheLocation = new File(getFilesDir() + "/blaupload"); + } + cacheLocation.mkdirs(); + + BitmapLruCache.Builder builder = new BitmapLruCache.Builder(this); + builder.setMemoryCacheEnabled(true).setMemoryCacheMaxSizeUsingHeapSize(); + builder.setDiskCacheEnabled(true).setDiskCacheLocation(cacheLocation); + + + mCache = builder.build(); + + Log.d(BlauploadApplication.class.toString(), cacheLocation.toString()); + } + + public BitmapLruCache getBitmapCache() { + return mCache; + } + + public static BlauploadApplication getApplication(Context context) { + return (BlauploadApplication) context.getApplicationContext(); + } + +} \ No newline at end of file diff --git a/app/src/main/java/uk/co/blatech/blaupload/HomeScreenFragment.java b/app/src/main/java/uk/co/blatech/blaupload/HomeScreenFragment.java index 6e6369b..ee51908 100644 --- a/app/src/main/java/uk/co/blatech/blaupload/HomeScreenFragment.java +++ b/app/src/main/java/uk/co/blatech/blaupload/HomeScreenFragment.java @@ -1,6 +1,7 @@ package uk.co.blatech.blaupload; import android.app.Activity; +import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.app.Fragment; @@ -40,6 +41,8 @@ public class HomeScreenFragment extends Fragment { private ArrayList thumbnails; private GridViewAdapter gridAdapter; private GridView gridView; + private ArrayList filenames; + private JSONArray json; /** * Returns a new instance of this fragment */ @@ -73,12 +76,17 @@ public class HomeScreenFragment extends Fragment { new GridViewAdapter(getActivity(), R.layout.grid_item, thumbnails); gridView.setAdapter(gridAdapter); - //Display a message when an item is clicked - //TODO: Make it display the full image + filenames = new ArrayList(); + //Open the full image in a pager when clicked gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() { public void onItemClick(AdapterView parent, View v, int position, long id) { - Toast.makeText(getActivity(), "Clicked image #"+position, Toast.LENGTH_SHORT).show(); + + Toast.makeText(getActivity(), "Clicked image #"+position, Toast.LENGTH_SHORT).show(); + Intent mIntent = new Intent(getActivity(), ImagePager.class); + mIntent.putExtra("json", json.toString()); + mIntent.putExtra("position", position); + startActivity(mIntent); } @@ -90,7 +98,7 @@ public class HomeScreenFragment extends Fragment { super.onAttach(activity); //Start loading the JSON file list in a thread as soon as we start - new JSONLoader().execute("http://www.blaupload.co.uk/?format=json&last=9"); + new JSONLoader().execute("http://www.blaupload.co.uk/?format=json&last=6"); } @@ -125,8 +133,7 @@ public class HomeScreenFragment extends Fragment { @Subscribe public void onAsyncTaskResult(JSONLoaderResultEvent event) { - JSONArray json = event.getResult(); - ArrayList filenames = new ArrayList(); + json = event.getResult(); if (json != null) { @@ -146,7 +153,7 @@ public class HomeScreenFragment extends Fragment { //Load the thumbnail for each item on a thread int id = 0; for (String filename : filenames) { - new ImageLoader(getResources()).execute("http://www.lc8n.com/blathumbs/", filename, String.valueOf(id)); + new ImageLoader(getResources()).execute("http://www.lc8n.com/blathumbs/", filename+".png", String.valueOf(id)); id++; } diff --git a/app/src/main/java/uk/co/blatech/blaupload/ImagePager.java b/app/src/main/java/uk/co/blatech/blaupload/ImagePager.java new file mode 100644 index 0000000..0191c52 --- /dev/null +++ b/app/src/main/java/uk/co/blatech/blaupload/ImagePager.java @@ -0,0 +1,85 @@ +package uk.co.blatech.blaupload; + +import android.app.Activity; +import android.os.Bundle; +import android.support.v4.view.ViewPager; +import android.util.Log; +import android.view.Menu; +import android.view.MenuItem; + +import com.squareup.otto.Subscribe; + +import org.json.JSONArray; +import org.json.JSONException; +import org.json.JSONObject; + +import java.util.ArrayList; + +import uk.co.blatech.blaupload.R; +import uk.co.blatech.blaupload.data.ImageItem; +import uk.co.blatech.blaupload.ui.ImagePagerAdapter; +import uk.co.blatech.blaupload.util.EventBus; +import uk.co.blatech.blaupload.util.ImageLoader; +import uk.co.blatech.blaupload.util.ImageLoaderResultEvent; + +public class ImagePager extends Activity { + + + private ViewPager mViewPager; + private JSONArray json; + private ArrayList urls; + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + mViewPager = new ViewPager(this); + setContentView(mViewPager); + EventBus.getInstance().register(this); + urls = new ArrayList(); + String jsonText = getIntent().getStringExtra("json"); + int id = getIntent().getIntExtra("position", 0); + + try { + json = new JSONArray(jsonText); + } catch (JSONException e) { + //TODO: Error handling if we got something other than JSON + Log.e(ImagePager.class.toString(), "Failed to parse JSON"); + e.printStackTrace(); + } + + for (int i = 0; i < json.length(); i++) { + try { + JSONObject item = json.getJSONObject(i); + urls.add("http://www.blaupload.co.uk/"+item.getString("filename")); + } catch (JSONException e) { + e.printStackTrace(); + } + + } + + ImagePagerAdapter adapter = new ImagePagerAdapter(ImagePager.this, urls); + mViewPager.setAdapter(adapter); + + } + + + @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_pager, menu); + return true; + } + + @Override + public boolean onOptionsItemSelected(MenuItem item) { + // Handle action bar item clicks here. The action bar will + // automatically handle clicks on the Home/Up button, so long + // as you specify a parent activity in AndroidManifest.xml. + int id = item.getItemId(); + if (id == R.id.action_settings) { + return true; + } + return super.onOptionsItemSelected(item); + } + + +} diff --git a/app/src/main/java/uk/co/blatech/blaupload/ui/DepthPageTransformer.java b/app/src/main/java/uk/co/blatech/blaupload/ui/DepthPageTransformer.java new file mode 100644 index 0000000..d2a5af3 --- /dev/null +++ b/app/src/main/java/uk/co/blatech/blaupload/ui/DepthPageTransformer.java @@ -0,0 +1,41 @@ +package uk.co.blatech.blaupload.ui; + +import android.support.v4.view.ViewPager; +import android.view.View; + +public class DepthPageTransformer implements ViewPager.PageTransformer { + private static final float MIN_SCALE = 0.75f; + + public void transformPage(View view, float position) { + int pageWidth = view.getWidth(); + + if (position < -1) { // [-Infinity,-1) + // This page is way off-screen to the left. + view.setAlpha(0); + + } else if (position <= 0) { // [-1,0] + // Use the default slide transition when moving to the left page + view.setAlpha(1); + view.setTranslationX(0); + view.setScaleX(1); + view.setScaleY(1); + + } else if (position <= 1) { // (0,1] + // Fade the page out. + view.setAlpha(1 - position); + + // Counteract the default slide transition + view.setTranslationX(pageWidth * -position); + + // Scale the page down (between MIN_SCALE and 1) + float scaleFactor = MIN_SCALE + + (1 - MIN_SCALE) * (1 - Math.abs(position)); + view.setScaleX(scaleFactor); + view.setScaleY(scaleFactor); + + } else { // (1,+Infinity] + // This page is way off-screen to the right. + view.setAlpha(0); + } + } +} \ No newline at end of file diff --git a/app/src/main/java/uk/co/blatech/blaupload/ui/ImagePagerAdapter.java b/app/src/main/java/uk/co/blatech/blaupload/ui/ImagePagerAdapter.java new file mode 100644 index 0000000..4f5dba0 --- /dev/null +++ b/app/src/main/java/uk/co/blatech/blaupload/ui/ImagePagerAdapter.java @@ -0,0 +1,66 @@ +/******************************************************************************* + * Copyright (c) 2013 Chris Banes. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + ******************************************************************************/ +package uk.co.blatech.blaupload.ui; + +import android.content.Context; +import android.support.v4.view.PagerAdapter; +import android.view.View; +import android.view.ViewGroup; +import android.view.ViewGroup.LayoutParams; +import android.widget.ImageView.ScaleType; + +import java.util.ArrayList; + +public class ImagePagerAdapter extends PagerAdapter { + + private final ArrayList mUrls; + + private final Context mContext; + + public ImagePagerAdapter(Context context, ArrayList urls) { + mUrls = urls; + mContext = context; + } + + @Override + public int getCount() { + return null != mUrls ? mUrls.size() : 0; + } + + @Override + public View instantiateItem(ViewGroup container, int position) { + NetworkCacheableImageView imageView = new NetworkCacheableImageView(mContext, null); + + String url = mUrls.get(position); + imageView.loadImage(url, true, null); + + imageView.setScaleType(ScaleType.FIT_CENTER); + container.addView(imageView, LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT); + + return imageView; + } + + @Override + public void destroyItem(ViewGroup container, int position, Object object) { + container.removeView((View) object); + } + + @Override + public boolean isViewFromObject(View view, Object object) { + return view == object; + } + +} diff --git a/app/src/main/java/uk/co/blatech/blaupload/ui/NetworkCacheableImageView.java b/app/src/main/java/uk/co/blatech/blaupload/ui/NetworkCacheableImageView.java new file mode 100644 index 0000000..043caae --- /dev/null +++ b/app/src/main/java/uk/co/blatech/blaupload/ui/NetworkCacheableImageView.java @@ -0,0 +1,271 @@ +/******************************************************************************* + * Copyright (c) 2013 Chris Banes. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + ******************************************************************************/ + +package uk.co.blatech.blaupload.ui; + +import android.content.Context; +import android.content.res.Resources; +import android.graphics.Bitmap; +import android.graphics.BitmapFactory; +import android.graphics.Matrix; +import android.graphics.drawable.BitmapDrawable; +import android.opengl.GLES10; +import android.os.AsyncTask; +import android.os.Build; +import android.util.AttributeSet; +import android.util.Log; +import android.widget.ImageView; + +import org.apache.http.HttpEntity; +import org.apache.http.HttpResponse; +import org.apache.http.StatusLine; +import org.apache.http.client.ClientProtocolException; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.impl.client.DefaultHttpClient; +import org.apache.http.util.EntityUtils; + +import java.io.BufferedInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.lang.ref.WeakReference; +import java.net.HttpURLConnection; +import java.net.URL; +import java.util.concurrent.RejectedExecutionException; + +import javax.microedition.khronos.opengles.GL10; + +import uk.co.blatech.blaupload.BlauploadApplication; +import uk.co.blatech.blaupload.R; +import uk.co.blatech.blaupload.data.ImageItem; +import uk.co.senab.bitmapcache.BitmapLruCache; +import uk.co.senab.bitmapcache.CacheableBitmapDrawable; +import uk.co.senab.bitmapcache.CacheableImageView; + +/** + * Simple extension of CacheableImageView which allows downloading of Images of the Internet. + * + * This code isn't production quality, but works well enough for this sample.s + * + * @author Chris Banes + */ +public class NetworkCacheableImageView extends CacheableImageView { + + public interface OnImageLoadedListener { + void onImageLoaded(CacheableBitmapDrawable result); + } + + /** + * This task simply fetches an Bitmap from the specified URL and wraps it in a wrapper. This + * implementation is NOT 'best practice' or production ready code. + */ + private static class ImageUrlAsyncTask + extends AsyncTask { + + private final BitmapLruCache mCache; + + private final WeakReference mImageViewRef; + private final OnImageLoadedListener mListener; + + private final BitmapFactory.Options mDecodeOpts; + + ImageUrlAsyncTask(ImageView imageView, BitmapLruCache cache, + BitmapFactory.Options decodeOpts, OnImageLoadedListener listener) { + mCache = cache; + mImageViewRef = new WeakReference(imageView); + mListener = listener; + mDecodeOpts = decodeOpts; + } + + @Override + protected CacheableBitmapDrawable doInBackground(String... params) { + + // Return early if the ImageView has disappeared. + if (null == mImageViewRef.get()) { + return null; + } + + final String url = params[0]; + + // Now we're not on the main thread we can check all caches + CacheableBitmapDrawable result = mCache.get(url); + + if (null == result) { + Log.d("ImageUrlAsyncTask", "Downloading: " + url); + +// // The bitmap isn't cached so download from the web +// HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection(); +// InputStream is = new BufferedInputStream(conn.getInputStream()); + Bitmap bmp = downloadImage(url); + // Add to cache + if (bmp != null) { + result = mCache.put(url, bmp); + } + } else { + Log.d("ImageUrlAsyncTask", "Got from Cache: " + url); + } + + return result; + + + } + + @Override + protected void onPostExecute(CacheableBitmapDrawable result) { + super.onPostExecute(result); + + ImageView iv = mImageViewRef.get(); + if (null != iv) { + iv.setImageDrawable(result); + } + + if (null != mListener) { + mListener.onImageLoaded(result); + } + } + + private Bitmap downloadImage(String url) { + + + DefaultHttpClient client = new DefaultHttpClient(); + HttpGet httpGet = new HttpGet(url); + Bitmap bmp = null; + + + int[] maxTextureSize = new int[1]; + maxTextureSize[0] = 4096; + GLES10.glGetIntegerv(GL10.GL_MAX_TEXTURE_SIZE, maxTextureSize, 0); + //TODO: Fix out of memory errors + try { + HttpResponse response = client.execute(httpGet); + StatusLine statusLine = response.getStatusLine(); + int statusCode = statusLine.getStatusCode(); + String[] parts = url.split("\\."); + String extension = parts[parts.length-1]; + //Create the Bitmap if the file exists + if (statusCode == 200 && isImageExtension(extension)) { + // If the thumbnail wasn't found, show a placeholder + HttpEntity entity = response.getEntity(); + byte[] bytes = EntityUtils.toByteArray(entity); + try { + bmp = BitmapFactory.decodeByteArray(bytes, 0, bytes.length); + } catch (Exception e) { + Log.e(NetworkCacheableImageView.class.toString(), "Failed decoding " + url); + e.printStackTrace(); + return bmp; + } + int bmpHeight = bmp.getHeight(); + int bmpWidth = bmp.getWidth(); + if (bmpWidth > maxTextureSize[0]) { + + float ratio = (float)maxTextureSize[0]/(float)bmpWidth; + Matrix matrix = new Matrix(); + bmpWidth = maxTextureSize[0]; + bmpHeight = Math.round(bmpHeight * ratio); + matrix.postScale(ratio, ratio); + bmp = Bitmap.createBitmap(bmp, 0, 0, bmpWidth, bmpHeight, matrix, false); + } else if (bmpHeight > maxTextureSize[0]) { + float ratio = (float)maxTextureSize[0]/(float)bmpHeight; + bmpHeight = maxTextureSize[0]; + bmpWidth = Math.round(bmpWidth * ratio); + Matrix matrix = new Matrix(); + matrix.postScale(ratio, ratio); + bmp = Bitmap.createBitmap(bmp, 0, 0, bmpWidth, bmpHeight, matrix, false); + } + } else { + //If the file doesn't exist, use the placeholder instead +// bmp = BitmapFactory.decodeResource(res, R.drawable.x); + return bmp; + } + + //TODO: Error handling (Not sure how we get here) + } catch (ClientProtocolException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + + return bmp; + } + + private boolean isImageExtension(String extension) { + if (extension.equalsIgnoreCase("jpeg") || extension.equalsIgnoreCase("jpg") || + extension.equalsIgnoreCase("gif") || extension.equalsIgnoreCase("png")) { + return true; + } else { + return false; + } + } + } + + private final BitmapLruCache mCache; + + private ImageUrlAsyncTask mCurrentTask; + + public NetworkCacheableImageView(Context context, AttributeSet attrs) { + super(context, attrs); + mCache = BlauploadApplication.getApplication(context).getBitmapCache(); + } + + /** + * Loads the Bitmap. + * + * @param url - URL of image + * @param fullSize - Whether the image should be kept at the original size + * @return true if the bitmap was found in the cache + */ + public boolean loadImage(String url, final boolean fullSize, OnImageLoadedListener listener) { + // First check whether there's already a task running, if so cancel it + if (null != mCurrentTask) { + mCurrentTask.cancel(true); + } + + // Check to see if the memory cache already has the bitmap. We can + // safely do + // this on the main thread. + BitmapDrawable wrapper = mCache.getFromMemoryCache(url); + + if (null != wrapper) { + // The cache has it, so just display it + setImageDrawable(wrapper); + return true; + } else { + // Memory Cache doesn't have the URL, do threaded request... + setImageDrawable(null); + + BitmapFactory.Options decodeOpts = null; + + if (!fullSize) { + //decodeOpts = new BitmapFactory.Options(); + //decodeOpts.inSampleSize = 2; + } + + mCurrentTask = new ImageUrlAsyncTask(this, mCache, decodeOpts, listener); + + try { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { + mCurrentTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, url); + } else { + mCurrentTask.execute(url); + } + } catch (RejectedExecutionException e) { + // This shouldn't happen, but might. + } + + return false; + } + } + +} diff --git a/app/src/main/java/uk/co/blatech/blaupload/ui/ZoomViewPager.java b/app/src/main/java/uk/co/blatech/blaupload/ui/ZoomViewPager.java new file mode 100644 index 0000000..e0b837e --- /dev/null +++ b/app/src/main/java/uk/co/blatech/blaupload/ui/ZoomViewPager.java @@ -0,0 +1,75 @@ +package uk.co.blatech.blaupload.ui; + +import android.content.Context; +import android.support.v4.view.ViewPager; +import android.util.AttributeSet; +import android.view.MotionEvent; + +/** + * Found at http://stackoverflow.com/questions/7814017/is-it-possible-to-disable-scrolling-on-a-viewpager. + * Convenient way to temporarily disable ViewPager navigation while interacting with ImageView. + * + * Julia Zudikova + */ + +/** + * Hacky fix for Issue #4 and + * http://code.google.com/p/android/issues/detail?id=18990 + *

+ * ScaleGestureDetector seems to mess up the touch events, which means that + * ViewGroups which make use of onInterceptTouchEvent throw a lot of + * IllegalArgumentException: pointerIndex out of range. + *

+ * There's not much I can do in my code for now, but we can mask the result by + * just catching the problem and ignoring it. + * + * @author Chris Banes + */ +public class ZoomViewPager extends ViewPager { + + private boolean isLocked; + + public ZoomViewPager(Context context) { + super(context); + isLocked = false; + } + + public ZoomViewPager(Context context, AttributeSet attrs) { + super(context, attrs); + isLocked = false; + } + + @Override + public boolean onInterceptTouchEvent(MotionEvent ev) { + if (!isLocked) { + try { + return super.onInterceptTouchEvent(ev); + } catch (IllegalArgumentException e) { + e.printStackTrace(); + return false; + } + } + return false; + } + + @Override + public boolean onTouchEvent(MotionEvent event) { + if (!isLocked) { + return super.onTouchEvent(event); + } + return false; + } + + public void toggleLock() { + isLocked = !isLocked; + } + + public void setLocked(boolean isLocked) { + this.isLocked = isLocked; + } + + public boolean isLocked() { + return isLocked; + } + +} \ No newline at end of file diff --git a/app/src/main/java/uk/co/blatech/blaupload/util/ImageLoader.java b/app/src/main/java/uk/co/blatech/blaupload/util/ImageLoader.java index 9af9a26..4a2dbc5 100644 --- a/app/src/main/java/uk/co/blatech/blaupload/util/ImageLoader.java +++ b/app/src/main/java/uk/co/blatech/blaupload/util/ImageLoader.java @@ -1,9 +1,14 @@ package uk.co.blatech.blaupload.util; +import android.annotation.TargetApi; import android.content.res.Resources; import android.graphics.Bitmap; import android.graphics.BitmapFactory; +import android.graphics.Matrix; +import android.opengl.GLES10; import android.os.AsyncTask; +import android.os.Build; +import android.util.Log; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; @@ -15,6 +20,8 @@ import org.apache.http.util.EntityUtils; import java.io.IOException; +import javax.microedition.khronos.opengles.GL10; + import uk.co.blatech.blaupload.R; import uk.co.blatech.blaupload.data.ImageItem; @@ -50,9 +57,12 @@ public class ImageLoader extends AsyncTask { String host = args[0]; String filename = args[1]; int id = Integer.parseInt(args[2]); + int[] maxTextureSize = new int[1]; + maxTextureSize[0] = 4096; + GLES10.glGetIntegerv(GL10.GL_MAX_TEXTURE_SIZE, maxTextureSize, 0); //Add an extra .png on the end to match the current thumbnail filenames - String url = host + filename + ".png"; + String url = host + filename; DefaultHttpClient client = new DefaultHttpClient(); HttpGet httpGet = new HttpGet(url); @@ -62,13 +72,40 @@ public class ImageLoader extends AsyncTask { HttpResponse response = client.execute(httpGet); StatusLine statusLine = response.getStatusLine(); int statusCode = statusLine.getStatusCode(); - + String[] parts = filename.split("\\."); + String extension = parts[parts.length-1]; //Create the Bitmap if the file exists - if (statusCode == 200) { + if (statusCode == 200 && isImageExtension(extension)) { // If the thumbnail wasn't found, show a placeholder HttpEntity entity = response.getEntity(); byte[] bytes = EntityUtils.toByteArray(entity); - bmp = BitmapFactory.decodeByteArray(bytes, 0, bytes.length); + try { + bmp = BitmapFactory.decodeByteArray(bytes, 0, bytes.length); + } catch (Exception e) { + Log.e(ImageLoader.class.toString(), "Failed decoding " + filename); + e.printStackTrace(); + bmp = BitmapFactory.decodeResource(res, R.drawable.x); + ImageItem image = new ImageItem(id, bmp, filename); + return image; + } + int bmpHeight = bmp.getHeight(); + int bmpWidth = bmp.getWidth(); + if (bmpWidth > maxTextureSize[0]) { + + float ratio = (float)maxTextureSize[0]/(float)bmpWidth; + Matrix matrix = new Matrix(); + bmpWidth = maxTextureSize[0]; + bmpHeight = Math.round(bmpHeight * ratio); + matrix.postScale(ratio, ratio); + bmp = Bitmap.createBitmap(bmp, 0, 0, bmpWidth, bmpHeight, matrix, false); + } else if (bmpHeight > maxTextureSize[0]) { + float ratio = (float)maxTextureSize[0]/(float)bmpHeight; + bmpHeight = maxTextureSize[0]; + bmpWidth = Math.round(bmpWidth * ratio); + Matrix matrix = new Matrix(); + matrix.postScale(ratio, ratio); + bmp = Bitmap.createBitmap(bmp, 0, 0, bmpWidth, bmpHeight, matrix, false); + } } else { //If the file doesn't exist, use the placeholder instead bmp = BitmapFactory.decodeResource(res, R.drawable.x); @@ -92,4 +129,13 @@ public class ImageLoader extends AsyncTask { } + private boolean isImageExtension(String extension) { + if (extension.equalsIgnoreCase("jpeg") || extension.equalsIgnoreCase("jpg") || + extension.equalsIgnoreCase("gif") || extension.equalsIgnoreCase("png")) { + return true; + } else { + return false; + } + } + } diff --git a/app/src/main/java/uk/co/blatech/blaupload/util/JSONLoader.java b/app/src/main/java/uk/co/blatech/blaupload/util/JSONLoader.java index 028202a..ffb9601 100644 --- a/app/src/main/java/uk/co/blatech/blaupload/util/JSONLoader.java +++ b/app/src/main/java/uk/co/blatech/blaupload/util/JSONLoader.java @@ -59,7 +59,7 @@ public class JSONLoader extends AsyncTask { DefaultHttpClient client = new DefaultHttpClient(); CookieStore cookieStore = new BasicCookieStore(); //TODO: Don't hard code the cookie. - BasicClientCookie cookie = new BasicClientCookie("password", "PASSWORDHASHHERE"); + BasicClientCookie cookie = new BasicClientCookie("password", "577f7781b7079fd9c0e67ef7e301617cf6eb317311ce8cd0b4b6b2652ffd14d5"); cookie.setDomain(".blaupload.co.uk"); cookie.setPath("/"); cookieStore.addCookie(cookie); diff --git a/app/src/main/res/layout/activity_image_pager.xml b/app/src/main/res/layout/activity_image_pager.xml new file mode 100644 index 0000000..74181fa --- /dev/null +++ b/app/src/main/res/layout/activity_image_pager.xml @@ -0,0 +1,16 @@ + + + + + diff --git a/app/src/main/res/layout/activity_image_viewer.xml b/app/src/main/res/layout/activity_image_viewer.xml new file mode 100644 index 0000000..9eef971 --- /dev/null +++ b/app/src/main/res/layout/activity_image_viewer.xml @@ -0,0 +1,7 @@ + \ No newline at end of file diff --git a/app/src/main/res/layout/fragment_image_viewer.xml b/app/src/main/res/layout/fragment_image_viewer.xml new file mode 100644 index 0000000..d9f75fa --- /dev/null +++ b/app/src/main/res/layout/fragment_image_viewer.xml @@ -0,0 +1,13 @@ + + + + diff --git a/app/src/main/res/menu/image_pager.xml b/app/src/main/res/menu/image_pager.xml new file mode 100644 index 0000000..67ae247 --- /dev/null +++ b/app/src/main/res/menu/image_pager.xml @@ -0,0 +1,8 @@ +

+ + diff --git a/app/src/main/res/menu/image_viewer.xml b/app/src/main/res/menu/image_viewer.xml new file mode 100644 index 0000000..bed19e0 --- /dev/null +++ b/app/src/main/res/menu/image_viewer.xml @@ -0,0 +1,8 @@ + + + diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 25d7732..ba06821 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -10,5 +10,8 @@ Example action Settings Hello blank fragment + ImageViewer + Hello world! + ImagePager -- cgit v1.2.3 From b52bf0af7a2af7743b600bb8c3c5a804bf9d2916 Mon Sep 17 00:00:00 2001 From: Joe Robinson Date: Wed, 13 Aug 2014 23:13:52 +0100 Subject: Added higher resolution image loading on zoom --- .idea/libraries/Android_BitmapCache_2_3.xml | 9 - .idea/libraries/disklrucache_2_0_2.xml | 11 - .idea/libraries/library_1_2_3.xml | 10 - .idea/libraries/picasso_2_3_3.xml | 11 + .idea/libraries/universal_image_loader_1_9_2.xml | 11 + app/app.iml | 5 +- app/build.gradle | 6 +- .../co/blatech/blaupload/BlauploadApplication.java | 39 +- .../co/blatech/blaupload/HomeScreenFragment.java | 4 +- .../java/uk/co/blatech/blaupload/ImagePager.java | 20 +- .../co/blatech/blaupload/ui/ImagePagerAdapter.java | 118 +- .../blaupload/ui/NetworkCacheableImageView.java | 271 ----- .../uk/co/blatech/blaupload/ui/TouchImageView.java | 1276 ++++++++++++++++++++ .../co/blatech/blaupload/util/BlaImageLoader.java | 139 +++ .../uk/co/blatech/blaupload/util/ImageLoader.java | 141 --- app/src/main/res/layout/activity_image_pager.xml | 17 +- app/src/main/res/layout/image_pager_item.xml | 23 + 17 files changed, 1596 insertions(+), 515 deletions(-) delete mode 100644 .idea/libraries/Android_BitmapCache_2_3.xml delete mode 100644 .idea/libraries/disklrucache_2_0_2.xml delete mode 100644 .idea/libraries/library_1_2_3.xml create mode 100644 .idea/libraries/picasso_2_3_3.xml create mode 100644 .idea/libraries/universal_image_loader_1_9_2.xml delete mode 100644 app/src/main/java/uk/co/blatech/blaupload/ui/NetworkCacheableImageView.java create mode 100644 app/src/main/java/uk/co/blatech/blaupload/ui/TouchImageView.java create mode 100644 app/src/main/java/uk/co/blatech/blaupload/util/BlaImageLoader.java delete mode 100644 app/src/main/java/uk/co/blatech/blaupload/util/ImageLoader.java create mode 100644 app/src/main/res/layout/image_pager_item.xml diff --git a/.idea/libraries/Android_BitmapCache_2_3.xml b/.idea/libraries/Android_BitmapCache_2_3.xml deleted file mode 100644 index 34c06f6..0000000 --- a/.idea/libraries/Android_BitmapCache_2_3.xml +++ /dev/null @@ -1,9 +0,0 @@ - - - - - - - - - \ No newline at end of file diff --git a/.idea/libraries/disklrucache_2_0_2.xml b/.idea/libraries/disklrucache_2_0_2.xml deleted file mode 100644 index 45145e6..0000000 --- a/.idea/libraries/disklrucache_2_0_2.xml +++ /dev/null @@ -1,11 +0,0 @@ - - - - - - - - - - - \ No newline at end of file diff --git a/.idea/libraries/library_1_2_3.xml b/.idea/libraries/library_1_2_3.xml deleted file mode 100644 index 1c592d4..0000000 --- a/.idea/libraries/library_1_2_3.xml +++ /dev/null @@ -1,10 +0,0 @@ - - - - - - - - - - \ No newline at end of file diff --git a/.idea/libraries/picasso_2_3_3.xml b/.idea/libraries/picasso_2_3_3.xml new file mode 100644 index 0000000..cff7520 --- /dev/null +++ b/.idea/libraries/picasso_2_3_3.xml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/universal_image_loader_1_9_2.xml b/.idea/libraries/universal_image_loader_1_9_2.xml new file mode 100644 index 0000000..be34e19 --- /dev/null +++ b/.idea/libraries/universal_image_loader_1_9_2.xml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/app/app.iml b/app/app.iml index 1556047..a5e1ead 100644 --- a/app/app.iml +++ b/app/app.iml @@ -61,11 +61,10 @@ - - + - + diff --git a/app/build.gradle b/app/build.gradle index d099f61..a481a41 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -24,7 +24,7 @@ dependencies { // You must install or update the Support Repository through the SDK manager to use this dependency. compile 'com.android.support:support-v4:19.+' compile 'com.squareup:otto:1.3.5' - compile 'com.github.chrisbanes.photoview:library:1.2.3' - compile 'com.jakewharton:disklrucache:2.0.2' - compile files('libs/Android-BitmapCache-2.3.jar') + compile 'com.squareup.picasso:picasso:2.3.3' + compile 'com.nostra13.universalimageloader:universal-image-loader:1.9.2' + } diff --git a/app/src/main/java/uk/co/blatech/blaupload/BlauploadApplication.java b/app/src/main/java/uk/co/blatech/blaupload/BlauploadApplication.java index 518eff8..9a5c5e2 100644 --- a/app/src/main/java/uk/co/blatech/blaupload/BlauploadApplication.java +++ b/app/src/main/java/uk/co/blatech/blaupload/BlauploadApplication.java @@ -1,50 +1,19 @@ package uk.co.blatech.blaupload; import android.app.Application; -import android.content.Context; -import android.os.Environment; -import android.util.Log; -import java.io.File; - -import uk.co.senab.bitmapcache.BitmapLruCache; +import com.nostra13.universalimageloader.core.ImageLoader; +import com.nostra13.universalimageloader.core.ImageLoaderConfiguration; public class BlauploadApplication extends Application { - private BitmapLruCache mCache; - @Override public void onCreate() { super.onCreate(); - File cacheLocation; - - // If we have external storage use it for the disk cache. Otherwise we use - // the cache dir - if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) { - cacheLocation = new File( - Environment.getExternalStorageDirectory() + "/blaupload"); - } else { - cacheLocation = new File(getFilesDir() + "/blaupload"); - } - cacheLocation.mkdirs(); - - BitmapLruCache.Builder builder = new BitmapLruCache.Builder(this); - builder.setMemoryCacheEnabled(true).setMemoryCacheMaxSizeUsingHeapSize(); - builder.setDiskCacheEnabled(true).setDiskCacheLocation(cacheLocation); - - - mCache = builder.build(); - - Log.d(BlauploadApplication.class.toString(), cacheLocation.toString()); - } - - public BitmapLruCache getBitmapCache() { - return mCache; - } + ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(getApplicationContext()).build(); + ImageLoader.getInstance().init(config); - public static BlauploadApplication getApplication(Context context) { - return (BlauploadApplication) context.getApplicationContext(); } } \ No newline at end of file diff --git a/app/src/main/java/uk/co/blatech/blaupload/HomeScreenFragment.java b/app/src/main/java/uk/co/blatech/blaupload/HomeScreenFragment.java index ee51908..88ed453 100644 --- a/app/src/main/java/uk/co/blatech/blaupload/HomeScreenFragment.java +++ b/app/src/main/java/uk/co/blatech/blaupload/HomeScreenFragment.java @@ -22,8 +22,8 @@ import java.util.ArrayList; import uk.co.blatech.blaupload.data.ImageItem; import uk.co.blatech.blaupload.ui.GridViewAdapter; +import uk.co.blatech.blaupload.util.BlaImageLoader; import uk.co.blatech.blaupload.util.EventBus; -import uk.co.blatech.blaupload.util.ImageLoader; import uk.co.blatech.blaupload.util.ImageLoaderResultEvent; import uk.co.blatech.blaupload.util.JSONLoader; import uk.co.blatech.blaupload.util.JSONLoaderResultEvent; @@ -153,7 +153,7 @@ public class HomeScreenFragment extends Fragment { //Load the thumbnail for each item on a thread int id = 0; for (String filename : filenames) { - new ImageLoader(getResources()).execute("http://www.lc8n.com/blathumbs/", filename+".png", String.valueOf(id)); + new BlaImageLoader(getResources()).execute("http://www.lc8n.com/bla100/", filename, String.valueOf(id)); id++; } diff --git a/app/src/main/java/uk/co/blatech/blaupload/ImagePager.java b/app/src/main/java/uk/co/blatech/blaupload/ImagePager.java index 0191c52..f562e2f 100644 --- a/app/src/main/java/uk/co/blatech/blaupload/ImagePager.java +++ b/app/src/main/java/uk/co/blatech/blaupload/ImagePager.java @@ -2,12 +2,12 @@ package uk.co.blatech.blaupload; import android.app.Activity; import android.os.Bundle; -import android.support.v4.view.ViewPager; import android.util.Log; import android.view.Menu; import android.view.MenuItem; -import com.squareup.otto.Subscribe; +import com.nostra13.universalimageloader.core.ImageLoader; +import com.nostra13.universalimageloader.core.ImageLoaderConfiguration; import org.json.JSONArray; import org.json.JSONException; @@ -15,24 +15,23 @@ import org.json.JSONObject; import java.util.ArrayList; -import uk.co.blatech.blaupload.R; -import uk.co.blatech.blaupload.data.ImageItem; import uk.co.blatech.blaupload.ui.ImagePagerAdapter; +import uk.co.blatech.blaupload.ui.ZoomViewPager; import uk.co.blatech.blaupload.util.EventBus; -import uk.co.blatech.blaupload.util.ImageLoader; -import uk.co.blatech.blaupload.util.ImageLoaderResultEvent; public class ImagePager extends Activity { - private ViewPager mViewPager; + private ZoomViewPager mViewPager; private JSONArray json; private ArrayList urls; + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); - mViewPager = new ViewPager(this); - setContentView(mViewPager); + setContentView(R.layout.activity_image_pager); + + mViewPager = (ZoomViewPager) findViewById(R.id.imagePager); EventBus.getInstance().register(this); urls = new ArrayList(); String jsonText = getIntent().getStringExtra("json"); @@ -49,7 +48,8 @@ public class ImagePager extends Activity { for (int i = 0; i < json.length(); i++) { try { JSONObject item = json.getJSONObject(i); - urls.add("http://www.blaupload.co.uk/"+item.getString("filename")); + urls.add("http://www.lc8n.com/bla500/"+item.getString("filename")); + } catch (JSONException e) { e.printStackTrace(); } diff --git a/app/src/main/java/uk/co/blatech/blaupload/ui/ImagePagerAdapter.java b/app/src/main/java/uk/co/blatech/blaupload/ui/ImagePagerAdapter.java index 4f5dba0..df55eda 100644 --- a/app/src/main/java/uk/co/blatech/blaupload/ui/ImagePagerAdapter.java +++ b/app/src/main/java/uk/co/blatech/blaupload/ui/ImagePagerAdapter.java @@ -16,41 +16,145 @@ package uk.co.blatech.blaupload.ui; import android.content.Context; +import android.graphics.Bitmap; +import android.graphics.Matrix; +import android.graphics.RectF; +import android.graphics.drawable.Drawable; import android.support.v4.view.PagerAdapter; +import android.util.Log; +import android.view.LayoutInflater; +import android.view.MotionEvent; import android.view.View; import android.view.ViewGroup; -import android.view.ViewGroup.LayoutParams; import android.widget.ImageView.ScaleType; +import com.nostra13.universalimageloader.core.ImageLoader; +import com.nostra13.universalimageloader.core.listener.SimpleImageLoadingListener; +import com.squareup.picasso.Picasso; +import com.squareup.picasso.Target; + +import java.io.IOException; import java.util.ArrayList; +import uk.co.blatech.blaupload.R; + public class ImagePagerAdapter extends PagerAdapter { private final ArrayList mUrls; private final Context mContext; + private float[] mWidths; + + private float[] mHeights; + private int position; + private ViewGroup container; + + private TouchImageView imageView; + private TouchImageView zoomImageView; + private ImageLoader imageLoader; + private ZoomViewPager mPager; + private ArrayList filenames; public ImagePagerAdapter(Context context, ArrayList urls) { mUrls = urls; mContext = context; + mWidths = new float[mUrls.size()]; + + mHeights = new float[mUrls.size()]; + filenames = new ArrayList(); } + + private Target target = new Target() { + @Override + public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) { + zoomImageView.setImageBitmap(bitmap); + zoomImageView.setVisibility(View.VISIBLE); + imageView.setVisibility(View.GONE); + } + + @Override + public void onBitmapFailed(Drawable errorDrawable) { + + } + + @Override + public void onPrepareLoad(Drawable placeHolderDrawable) { + + } + + + }; + @Override public int getCount() { return null != mUrls ? mUrls.size() : 0; } @Override - public View instantiateItem(ViewGroup container, int position) { - NetworkCacheableImageView imageView = new NetworkCacheableImageView(mContext, null); + public View instantiateItem(ViewGroup con, int pos) { + container = con; + position = pos; - String url = mUrls.get(position); - imageView.loadImage(url, true, null); + + LayoutInflater inflater = (LayoutInflater) container.getContext() + .getSystemService(Context.LAYOUT_INFLATER_SERVICE); + View view = inflater.inflate(R.layout.image_pager_item, container, false); + + + imageView = (TouchImageView) view.findViewById(R.id.imageView); + zoomImageView = (TouchImageView) view.findViewById(R.id.zoomImageView); + final String url = mUrls.get(position); + String[] parts = url.split("/"); + final String filename = parts[parts.length-1]; + + + imageLoader = ImageLoader.getInstance(); + imageLoader.displayImage(url, imageView); imageView.setScaleType(ScaleType.FIT_CENTER); - container.addView(imageView, LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT); + zoomImageView.setScaleType(ScaleType.FIT_CENTER); + container.refreshDrawableState(); + + imageView.setTag("img"+pos); + zoomImageView.setTag("zoom"+pos); + mWidths[position] = 0; + mHeights[position] = 0; + filenames.add(pos, filename); + mPager = (ZoomViewPager) container.findViewById(R.id.imagePager); + + imageView.setOnTouchListener(new View.OnTouchListener() { + @Override + public boolean onTouch(View view, MotionEvent motionEvent) { + final TouchImageView thisImage = (TouchImageView) container.findViewWithTag("img" + mPager.getCurrentItem()); + final TouchImageView zoomImage = (TouchImageView) container.findViewWithTag("zoom" + mPager.getCurrentItem()); + + if (thisImage.getCurrentZoom() > 1.0) { + + imageLoader.displayImage("http://www.blaupload.co.uk/"+ filenames.get(mPager.getCurrentItem()), zoomImage, new SimpleImageLoadingListener() { + // + @Override + public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) { + + zoomImage.setZoom(thisImage.getCurrentZoom()); + + zoomImage.setVisibility(View.VISIBLE); + thisImage.setVisibility(View.GONE); + } + }); + + } + Log.d(ImagePagerAdapter.class.toString(), "Initial width: "+ mWidths[position] +", height: "+ mHeights[position]); + Log.d(ImagePagerAdapter.class.toString(), "Current width: " + thisImage.getMeasuredWidth() + ", height: " + thisImage.getMeasuredHeight()); + Log.d(ImagePagerAdapter.class.toString(), "Other width: " + thisImage.getWidth() + ", height: " + thisImage.getHeight()); + Log.d(ImagePagerAdapter.class.toString(), "Zoom: " + thisImage.getCurrentZoom()); + return true; + } + }); + + container.addView(view, 0); - return imageView; + return view; } @Override diff --git a/app/src/main/java/uk/co/blatech/blaupload/ui/NetworkCacheableImageView.java b/app/src/main/java/uk/co/blatech/blaupload/ui/NetworkCacheableImageView.java deleted file mode 100644 index 043caae..0000000 --- a/app/src/main/java/uk/co/blatech/blaupload/ui/NetworkCacheableImageView.java +++ /dev/null @@ -1,271 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2013 Chris Banes. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - ******************************************************************************/ - -package uk.co.blatech.blaupload.ui; - -import android.content.Context; -import android.content.res.Resources; -import android.graphics.Bitmap; -import android.graphics.BitmapFactory; -import android.graphics.Matrix; -import android.graphics.drawable.BitmapDrawable; -import android.opengl.GLES10; -import android.os.AsyncTask; -import android.os.Build; -import android.util.AttributeSet; -import android.util.Log; -import android.widget.ImageView; - -import org.apache.http.HttpEntity; -import org.apache.http.HttpResponse; -import org.apache.http.StatusLine; -import org.apache.http.client.ClientProtocolException; -import org.apache.http.client.methods.HttpGet; -import org.apache.http.impl.client.DefaultHttpClient; -import org.apache.http.util.EntityUtils; - -import java.io.BufferedInputStream; -import java.io.IOException; -import java.io.InputStream; -import java.lang.ref.WeakReference; -import java.net.HttpURLConnection; -import java.net.URL; -import java.util.concurrent.RejectedExecutionException; - -import javax.microedition.khronos.opengles.GL10; - -import uk.co.blatech.blaupload.BlauploadApplication; -import uk.co.blatech.blaupload.R; -import uk.co.blatech.blaupload.data.ImageItem; -import uk.co.senab.bitmapcache.BitmapLruCache; -import uk.co.senab.bitmapcache.CacheableBitmapDrawable; -import uk.co.senab.bitmapcache.CacheableImageView; - -/** - * Simple extension of CacheableImageView which allows downloading of Images of the Internet. - * - * This code isn't production quality, but works well enough for this sample.s - * - * @author Chris Banes - */ -public class NetworkCacheableImageView extends CacheableImageView { - - public interface OnImageLoadedListener { - void onImageLoaded(CacheableBitmapDrawable result); - } - - /** - * This task simply fetches an Bitmap from the specified URL and wraps it in a wrapper. This - * implementation is NOT 'best practice' or production ready code. - */ - private static class ImageUrlAsyncTask - extends AsyncTask { - - private final BitmapLruCache mCache; - - private final WeakReference mImageViewRef; - private final OnImageLoadedListener mListener; - - private final BitmapFactory.Options mDecodeOpts; - - ImageUrlAsyncTask(ImageView imageView, BitmapLruCache cache, - BitmapFactory.Options decodeOpts, OnImageLoadedListener listener) { - mCache = cache; - mImageViewRef = new WeakReference(imageView); - mListener = listener; - mDecodeOpts = decodeOpts; - } - - @Override - protected CacheableBitmapDrawable doInBackground(String... params) { - - // Return early if the ImageView has disappeared. - if (null == mImageViewRef.get()) { - return null; - } - - final String url = params[0]; - - // Now we're not on the main thread we can check all caches - CacheableBitmapDrawable result = mCache.get(url); - - if (null == result) { - Log.d("ImageUrlAsyncTask", "Downloading: " + url); - -// // The bitmap isn't cached so download from the web -// HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection(); -// InputStream is = new BufferedInputStream(conn.getInputStream()); - Bitmap bmp = downloadImage(url); - // Add to cache - if (bmp != null) { - result = mCache.put(url, bmp); - } - } else { - Log.d("ImageUrlAsyncTask", "Got from Cache: " + url); - } - - return result; - - - } - - @Override - protected void onPostExecute(CacheableBitmapDrawable result) { - super.onPostExecute(result); - - ImageView iv = mImageViewRef.get(); - if (null != iv) { - iv.setImageDrawable(result); - } - - if (null != mListener) { - mListener.onImageLoaded(result); - } - } - - private Bitmap downloadImage(String url) { - - - DefaultHttpClient client = new DefaultHttpClient(); - HttpGet httpGet = new HttpGet(url); - Bitmap bmp = null; - - - int[] maxTextureSize = new int[1]; - maxTextureSize[0] = 4096; - GLES10.glGetIntegerv(GL10.GL_MAX_TEXTURE_SIZE, maxTextureSize, 0); - //TODO: Fix out of memory errors - try { - HttpResponse response = client.execute(httpGet); - StatusLine statusLine = response.getStatusLine(); - int statusCode = statusLine.getStatusCode(); - String[] parts = url.split("\\."); - String extension = parts[parts.length-1]; - //Create the Bitmap if the file exists - if (statusCode == 200 && isImageExtension(extension)) { - // If the thumbnail wasn't found, show a placeholder - HttpEntity entity = response.getEntity(); - byte[] bytes = EntityUtils.toByteArray(entity); - try { - bmp = BitmapFactory.decodeByteArray(bytes, 0, bytes.length); - } catch (Exception e) { - Log.e(NetworkCacheableImageView.class.toString(), "Failed decoding " + url); - e.printStackTrace(); - return bmp; - } - int bmpHeight = bmp.getHeight(); - int bmpWidth = bmp.getWidth(); - if (bmpWidth > maxTextureSize[0]) { - - float ratio = (float)maxTextureSize[0]/(float)bmpWidth; - Matrix matrix = new Matrix(); - bmpWidth = maxTextureSize[0]; - bmpHeight = Math.round(bmpHeight * ratio); - matrix.postScale(ratio, ratio); - bmp = Bitmap.createBitmap(bmp, 0, 0, bmpWidth, bmpHeight, matrix, false); - } else if (bmpHeight > maxTextureSize[0]) { - float ratio = (float)maxTextureSize[0]/(float)bmpHeight; - bmpHeight = maxTextureSize[0]; - bmpWidth = Math.round(bmpWidth * ratio); - Matrix matrix = new Matrix(); - matrix.postScale(ratio, ratio); - bmp = Bitmap.createBitmap(bmp, 0, 0, bmpWidth, bmpHeight, matrix, false); - } - } else { - //If the file doesn't exist, use the placeholder instead -// bmp = BitmapFactory.decodeResource(res, R.drawable.x); - return bmp; - } - - //TODO: Error handling (Not sure how we get here) - } catch (ClientProtocolException e) { - e.printStackTrace(); - } catch (IOException e) { - e.printStackTrace(); - } - - return bmp; - } - - private boolean isImageExtension(String extension) { - if (extension.equalsIgnoreCase("jpeg") || extension.equalsIgnoreCase("jpg") || - extension.equalsIgnoreCase("gif") || extension.equalsIgnoreCase("png")) { - return true; - } else { - return false; - } - } - } - - private final BitmapLruCache mCache; - - private ImageUrlAsyncTask mCurrentTask; - - public NetworkCacheableImageView(Context context, AttributeSet attrs) { - super(context, attrs); - mCache = BlauploadApplication.getApplication(context).getBitmapCache(); - } - - /** - * Loads the Bitmap. - * - * @param url - URL of image - * @param fullSize - Whether the image should be kept at the original size - * @return true if the bitmap was found in the cache - */ - public boolean loadImage(String url, final boolean fullSize, OnImageLoadedListener listener) { - // First check whether there's already a task running, if so cancel it - if (null != mCurrentTask) { - mCurrentTask.cancel(true); - } - - // Check to see if the memory cache already has the bitmap. We can - // safely do - // this on the main thread. - BitmapDrawable wrapper = mCache.getFromMemoryCache(url); - - if (null != wrapper) { - // The cache has it, so just display it - setImageDrawable(wrapper); - return true; - } else { - // Memory Cache doesn't have the URL, do threaded request... - setImageDrawable(null); - - BitmapFactory.Options decodeOpts = null; - - if (!fullSize) { - //decodeOpts = new BitmapFactory.Options(); - //decodeOpts.inSampleSize = 2; - } - - mCurrentTask = new ImageUrlAsyncTask(this, mCache, decodeOpts, listener); - - try { - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { - mCurrentTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, url); - } else { - mCurrentTask.execute(url); - } - } catch (RejectedExecutionException e) { - // This shouldn't happen, but might. - } - - return false; - } - } - -} diff --git a/app/src/main/java/uk/co/blatech/blaupload/ui/TouchImageView.java b/app/src/main/java/uk/co/blatech/blaupload/ui/TouchImageView.java new file mode 100644 index 0000000..45513c4 --- /dev/null +++ b/app/src/main/java/uk/co/blatech/blaupload/ui/TouchImageView.java @@ -0,0 +1,1276 @@ +/* + * TouchImageView.java + * By: Michael Ortiz + * Updated By: Patrick Lackemacher + * Updated By: Babay88 + * Updated By: @ipsilondev + * Updated By: hank-cp + * Updated By: singpolyma + * ------------------- + * Extends Android ImageView to include pinch zooming, panning, fling and double tap zoom. + */ + +package uk.co.blatech.blaupload.ui; + +import android.annotation.TargetApi; +import android.content.Context; +import android.content.res.Configuration; +import android.graphics.Bitmap; +import android.graphics.Canvas; +import android.graphics.Matrix; +import android.graphics.PointF; +import android.graphics.RectF; +import android.graphics.drawable.Drawable; +import android.net.Uri; +import android.os.Build; +import android.os.Build.VERSION; +import android.os.Build.VERSION_CODES; +import android.os.Bundle; +import android.os.Parcelable; +import android.util.AttributeSet; +import android.util.Log; +import android.view.GestureDetector; +import android.view.MotionEvent; +import android.view.ScaleGestureDetector; +import android.view.View; +import android.view.animation.AccelerateDecelerateInterpolator; +import android.widget.ImageView; +import android.widget.OverScroller; +import android.widget.Scroller; + +public class TouchImageView extends ImageView { + + private static final String DEBUG = "DEBUG"; + + // + // SuperMin and SuperMax multipliers. Determine how much the image can be + // zoomed below or above the zoom boundaries, before animating back to the + // min/max zoom boundary. + // + private static final float SUPER_MIN_MULTIPLIER = .75f; + private static final float SUPER_MAX_MULTIPLIER = 1.25f; + + // + // Scale of image ranges from minScale to maxScale, where minScale == 1 + // when the image is stretched to fit view. + // + private float normalizedScale; + + // + // Matrix applied to image. MSCALE_X and MSCALE_Y should always be equal. + // MTRANS_X and MTRANS_Y are the other values used. prevMatrix is the matrix + // saved prior to the screen rotating. + // + private Matrix matrix, prevMatrix; + + private static enum State { NONE, DRAG, ZOOM, FLING, ANIMATE_ZOOM }; + private State state; + + private float minScale; + private float maxScale; + private float superMinScale; + private float superMaxScale; + private float[] m; + + private Context context; + private Fling fling; + + private ScaleType mScaleType; + + private boolean imageRenderedAtLeastOnce; + private boolean onDrawReady; + + private ZoomVariables delayedZoomVariables; + + // + // Size of view and previous view size (ie before rotation) + // + private int viewWidth, viewHeight, prevViewWidth, prevViewHeight; + + // + // Size of image when it is stretched to fit view. Before and After rotation. + // + private float matchViewWidth, matchViewHeight, prevMatchViewWidth, prevMatchViewHeight; + + private ScaleGestureDetector mScaleDetector; + private GestureDetector mGestureDetector; + private GestureDetector.OnDoubleTapListener doubleTapListener = null; + private OnTouchListener userTouchListener = null; + private OnTouchImageViewListener touchImageViewListener = null; + + public TouchImageView(Context context) { + super(context); + sharedConstructing(context); + } + + public TouchImageView(Context context, AttributeSet attrs) { + super(context, attrs); + sharedConstructing(context); + } + + public TouchImageView(Context context, AttributeSet attrs, int defStyle) { + super(context, attrs, defStyle); + sharedConstructing(context); + } + + private void sharedConstructing(Context context) { + super.setClickable(true); + this.context = context; + mScaleDetector = new ScaleGestureDetector(context, new ScaleListener()); + mGestureDetector = new GestureDetector(context, new GestureListener()); + matrix = new Matrix(); + prevMatrix = new Matrix(); + m = new float[9]; + normalizedScale = 1; + if (mScaleType == null) { + mScaleType = ScaleType.FIT_CENTER; + } + minScale = 1; + maxScale = 3; + superMinScale = SUPER_MIN_MULTIPLIER * minScale; + superMaxScale = SUPER_MAX_MULTIPLIER * maxScale; + setImageMatrix(matrix); + setScaleType(ScaleType.MATRIX); + setState(State.NONE); + onDrawReady = false; + super.setOnTouchListener(new PrivateOnTouchListener()); + } + + @Override + public void setOnTouchListener(View.OnTouchListener l) { + userTouchListener = l; + } + + public void setOnTouchImageViewListener(OnTouchImageViewListener l) { + touchImageViewListener = l; + } + + public void setOnDoubleTapListener(GestureDetector.OnDoubleTapListener l) { + doubleTapListener = l; + } + + @Override + public void setImageResource(int resId) { + super.setImageResource(resId); + savePreviousImageValues(); + fitImageToView(); + } + + @Override + public void setImageBitmap(Bitmap bm) { + super.setImageBitmap(bm); + savePreviousImageValues(); + fitImageToView(); + } + + @Override + public void setImageDrawable(Drawable drawable) { + super.setImageDrawable(drawable); + savePreviousImageValues(); + fitImageToView(); + } + + @Override + public void setImageURI(Uri uri) { + super.setImageURI(uri); + savePreviousImageValues(); + fitImageToView(); + } + + @Override + public void setScaleType(ScaleType type) { + if (type == ScaleType.FIT_START || type == ScaleType.FIT_END) { + throw new UnsupportedOperationException("TouchImageView does not support FIT_START or FIT_END"); + } + if (type == ScaleType.MATRIX) { + super.setScaleType(ScaleType.MATRIX); + + } else { + mScaleType = type; + if (onDrawReady) { + // + // If the image is already rendered, scaleType has been called programmatically + // and the TouchImageView should be updated with the new scaleType. + // + setZoom(this); + } + } + } + + @Override + public ScaleType getScaleType() { + return mScaleType; + } + + /** + * Returns false if image is in initial, unzoomed state. False, otherwise. + * @return true if image is zoomed + */ + public boolean isZoomed() { + return normalizedScale != 1; + } + + /** + * Return a Rect representing the zoomed image. + * @return rect representing zoomed image + */ + public RectF getZoomedRect() { + if (mScaleType == ScaleType.FIT_XY) { + throw new UnsupportedOperationException("getZoomedRect() not supported with FIT_XY"); + } + PointF topLeft = transformCoordTouchToBitmap(0, 0, true); + PointF bottomRight = transformCoordTouchToBitmap(viewWidth, viewHeight, true); + + float w = getDrawable().getIntrinsicWidth(); + float h = getDrawable().getIntrinsicHeight(); + return new RectF(topLeft.x / w, topLeft.y / h, bottomRight.x / w, bottomRight.y / h); + } + + /** + * Save the current matrix and view dimensions + * in the prevMatrix and prevView variables. + */ + private void savePreviousImageValues() { + if (matrix != null && viewHeight != 0 && viewWidth != 0) { + matrix.getValues(m); + prevMatrix.setValues(m); + prevMatchViewHeight = matchViewHeight; + prevMatchViewWidth = matchViewWidth; + prevViewHeight = viewHeight; + prevViewWidth = viewWidth; + } + } + + @Override + public Parcelable onSaveInstanceState() { + Bundle bundle = new Bundle(); + bundle.putParcelable("instanceState", super.onSaveInstanceState()); + bundle.putFloat("saveScale", normalizedScale); + bundle.putFloat("matchViewHeight", matchViewHeight); + bundle.putFloat("matchViewWidth", matchViewWidth); + bundle.putInt("viewWidth", viewWidth); + bundle.putInt("viewHeight", viewHeight); + matrix.getValues(m); + bundle.putFloatArray("matrix", m); + bundle.putBoolean("imageRendered", imageRenderedAtLeastOnce); + return bundle; + } + + @Override + public void onRestoreInstanceState(Parcelable state) { + if (state instanceof Bundle) { + Bundle bundle = (Bundle) state; + normalizedScale = bundle.getFloat("saveScale"); + m = bundle.getFloatArray("matrix"); + prevMatrix.setValues(m); + prevMatchViewHeight = bundle.getFloat("matchViewHeight"); + prevMatchViewWidth = bundle.getFloat("matchViewWidth"); + prevViewHeight = bundle.getInt("viewHeight"); + prevViewWidth = bundle.getInt("viewWidth"); + imageRenderedAtLeastOnce = bundle.getBoolean("imageRendered"); + super.onRestoreInstanceState(bundle.getParcelable("instanceState")); + return; + } + + super.onRestoreInstanceState(state); + } + + @Override + protected void onDraw(Canvas canvas) { + onDrawReady = true; + imageRenderedAtLeastOnce = true; + if (delayedZoomVariables != null) { + setZoom(delayedZoomVariables.scale, delayedZoomVariables.focusX, delayedZoomVariables.focusY, delayedZoomVariables.scaleType); + delayedZoomVariables = null; + } + super.onDraw(canvas); + } + + @Override + public void onConfigurationChanged(Configuration newConfig) { + super.onConfigurationChanged(newConfig); + savePreviousImageValues(); + } + + /** + * Get the max zoom multiplier. + * @return max zoom multiplier. + */ + public float getMaxZoom() { + return maxScale; + } + + /** + * Set the max zoom multiplier. Default value: 3. + * @param max max zoom multiplier. + */ + public void setMaxZoom(float max) { + maxScale = max; + superMaxScale = SUPER_MAX_MULTIPLIER * maxScale; + } + + /** + * Get the min zoom multiplier. + * @return min zoom multiplier. + */ + public float getMinZoom() { + return minScale; + } + + /** + * Get the current zoom. This is the zoom relative to the initial + * scale, not the original resource. + * @return current zoom multiplier. + */ + public float getCurrentZoom() { + return normalizedScale; + } + + /** + * Set the min zoom multiplier. Default value: 1. + * @param min min zoom multiplier. + */ + public void setMinZoom(float min) { + minScale = min; + superMinScale = SUPER_MIN_MULTIPLIER * minScale; + } + + /** + * Reset zoom and translation to initial state. + */ + public void resetZoom() { + normalizedScale = 1; + fitImageToView(); + } + + /** + * Set zoom to the specified scale. Image will be centered by default. + * @param scale + */ + public void setZoom(float scale) { + setZoom(scale, 0.5f, 0.5f); + } + + /** + * Set zoom to the specified scale. Image will be centered around the point + * (focusX, focusY). These floats range from 0 to 1 and denote the focus point + * as a fraction from the left and top of the view. For example, the top left + * corner of the image would be (0, 0). And the bottom right corner would be (1, 1). + * @param scale + * @param focusX + * @param focusY + */ + public void setZoom(float scale, float focusX, float focusY) { + setZoom(scale, focusX, focusY, mScaleType); + } + + /** + * Set zoom to the specified scale. Image will be centered around the point + * (focusX, focusY). These floats range from 0 to 1 and denote the focus point + * as a fraction from the left and top of the view. For example, the top left + * corner of the image would be (0, 0). And the bottom right corner would be (1, 1). + * @param scale + * @param focusX + * @param focusY + * @param scaleType + */ + public void setZoom(float scale, float focusX, float focusY, ScaleType scaleType) { + // + // setZoom can be called before the image is on the screen, but at this point, + // image and view sizes have not yet been calculated in onMeasure. Thus, we should + // delay calling setZoom until the view has been measured. + // + if (!onDrawReady) { + delayedZoomVariables = new ZoomVariables(scale, focusX, focusY, scaleType); + return; + } + + if (scaleType != mScaleType) { + setScaleType(scaleType); + } + resetZoom(); + scaleImage(scale, viewWidth / 2, viewHeight / 2, true); + matrix.getValues(m); + m[Matrix.MTRANS_X] = -((focusX * getImageWidth()) - (viewWidth * 0.5f)); + m[Matrix.MTRANS_Y] = -((focusY * getImageHeight()) - (viewHeight * 0.5f)); + matrix.setValues(m); + fixTrans(); + setImageMatrix(matrix); + } + + /** + * Set zoom parameters equal to another TouchImageView. Including scale, position, + * and ScaleType. + * @param img + */ + public void setZoom(TouchImageView img) { + PointF center = img.getScrollPosition(); + setZoom(img.getCurrentZoom(), center.x, center.y, img.getScaleType()); + } + + /** + * Return the point at the center of the zoomed image. The PointF coordinates range + * in value between 0 and 1 and the focus point is denoted as a fraction from the left + * and top of the view. For example, the top left corner of the image would be (0, 0). + * And the bottom right corner would be (1, 1). + * @return PointF representing the scroll position of the zoomed image. + */ + public PointF getScrollPosition() { + Drawable drawable = getDrawable(); + if (drawable == null) { + return null; + } + int drawableWidth = drawable.getIntrinsicWidth(); + int drawableHeight = drawable.getIntrinsicHeight(); + + PointF point = transformCoordTouchToBitmap(viewWidth / 2, viewHeight / 2, true); + point.x /= drawableWidth; + point.y /= drawableHeight; + return point; + } + + /** + * Set the focus point of the zoomed image. The focus points are denoted as a fraction from the + * left and top of the view. The focus points can range in value between 0 and 1. + * @param focusX + * @param focusY + */ + public void setScrollPosition(float focusX, float focusY) { + setZoom(normalizedScale, focusX, focusY); + } + + /** + * Performs boundary checking and fixes the image matrix if it + * is out of bounds. + */ + private void fixTrans() { + matrix.getValues(m); + float transX = m[Matrix.MTRANS_X]; + float transY = m[Matrix.MTRANS_Y]; + + float fixTransX = getFixTrans(transX, viewWidth, getImageWidth()); + float fixTransY = getFixTrans(transY, viewHeight, getImageHeight()); + + if (fixTransX != 0 || fixTransY != 0) { + matrix.postTranslate(fixTransX, fixTransY); + } + } + + /** + * When transitioning from zooming from focus to zoom from center (or vice versa) + * the image can become unaligned within the view. This is apparent when zooming + * quickly. When the content size is less than the view size, the content will often + * be centered incorrectly within the view. fixScaleTrans first calls fixTrans() and + * then makes sure the image is centered correctly within the view. + */ + private void fixScaleTrans() { + fixTrans(); + matrix.getValues(m); + if (getImageWidth() < viewWidth) { + m[Matrix.MTRANS_X] = (viewWidth - getImageWidth()) / 2; + } + + if (getImageHeight() < viewHeight) { + m[Matrix.MTRANS_Y] = (viewHeight - getImageHeight()) / 2; + } + matrix.setValues(m); + } + + private float getFixTrans(float trans, float viewSize, float contentSize) { + float minTrans, maxTrans; + + if (contentSize <= viewSize) { + minTrans = 0; + maxTrans = viewSize - contentSize; + + } else { + minTrans = viewSize - contentSize; + maxTrans = 0; + } + + if (trans < minTrans) + return -trans + minTrans; + if (trans > maxTrans) + return -trans + maxTrans; + return 0; + } + + private float getFixDragTrans(float delta, float viewSize, float contentSize) { + if (contentSize <= viewSize) { + return 0; + } + return delta; + } + + private float getImageWidth() { + return matchViewWidth * normalizedScale; + } + + private float getImageHeight() { + return matchViewHeight * normalizedScale; + } + + @Override + protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { + Drawable drawable = getDrawable(); + if (drawable == null || drawable.getIntrinsicWidth() == 0 || drawable.getIntrinsicHeight() == 0) { + setMeasuredDimension(0, 0); + return; + } + + int drawableWidth = drawable.getIntrinsicWidth(); + int drawableHeight = drawable.getIntrinsicHeight(); + int widthSize = MeasureSpec.getSize(widthMeasureSpec); + int widthMode = MeasureSpec.getMode(widthMeasureSpec); + int heightSize = MeasureSpec.getSize(heightMeasureSpec); + int heightMode = MeasureSpec.getMode(heightMeasureSpec); + viewWidth = setViewSize(widthMode, widthSize, drawableWidth); + viewHeight = setViewSize(heightMode, heightSize, drawableHeight); + + // + // Set view dimensions + // + setMeasuredDimension(viewWidth, viewHeight); + + // + // Fit content within view + // + fitImageToView(); + } + + /** + * If the normalizedScale is equal to 1, then the image is made to fit the screen. Otherwise, + * it is made to fit the screen according to the dimensions of the previous image matrix. This + * allows the image to maintain its zoom after rotation. + */ + private void fitImageToView() { + Drawable drawable = getDrawable(); + if (drawable == null || drawable.getIntrinsicWidth() == 0 || drawable.getIntrinsicHeight() == 0) { + return; + } + if (matrix == null || prevMatrix == null) { + return; + } + + int drawableWidth = drawable.getIntrinsicWidth(); + int drawableHeight = drawable.getIntrinsicHeight(); + + // + // Scale image for view + // + float scaleX = (float) viewWidth / drawableWidth; + float scaleY = (float) viewHeight / drawableHeight; + + switch (mScaleType) { + case CENTER: + scaleX = scaleY = 1; + break; + + case CENTER_CROP: + scaleX = scaleY = Math.max(scaleX, scaleY); + break; + + case CENTER_INSIDE: + scaleX = scaleY = Math.min(1, Math.min(scaleX, scaleY)); + + case FIT_CENTER: + scaleX = scaleY = Math.min(scaleX, scaleY); + break; + + case FIT_XY: + break; + + default: + // + // FIT_START and FIT_END not supported + // + throw new UnsupportedOperationException("TouchImageView does not support FIT_START or FIT_END"); + + } + + // + // Center the image + // + float redundantXSpace = viewWidth - (scaleX * drawableWidth); + float redundantYSpace = viewHeight - (scaleY * drawableHeight); + matchViewWidth = viewWidth - redundantXSpace; + matchViewHeight = viewHeight - redundantYSpace; + if (!isZoomed() && !imageRenderedAtLeastOnce) { + // + // Stretch and center image to fit view + // + matrix.setScale(scaleX, scaleY); + matrix.postTranslate(redundantXSpace / 2, redundantYSpace / 2); + normalizedScale = 1; + + } else { + // + // These values should never be 0 or we will set viewWidth and viewHeight + // to NaN in translateMatrixAfterRotate. To avoid this, call savePreviousImageValues + // to set them equal to the current values. + // + if (prevMatchViewWidth == 0 || prevMatchViewHeight == 0) { + savePreviousImageValues(); + } + + prevMatrix.getValues(m); + + // + // Rescale Matrix after rotation + // + m[Matrix.MSCALE_X] = matchViewWidth / drawableWidth * normalizedScale; + m[Matrix.MSCALE_Y] = matchViewHeight / drawableHeight * normalizedScale; + + // + // TransX and TransY from previous matrix + // + float transX = m[Matrix.MTRANS_X]; + float transY = m[Matrix.MTRANS_Y]; + + // + // Width + // + float prevActualWidth = prevMatchViewWidth * normalizedScale; + float actualWidth = getImageWidth(); + translateMatrixAfterRotate(Matrix.MTRANS_X, transX, prevActualWidth, actualWidth, prevViewWidth, viewWidth, drawableWidth); + + // + // Height + // + float prevActualHeight = prevMatchViewHeight * normalizedScale; + float actualHeight = getImageHeight(); + translateMatrixAfterRotate(Matrix.MTRANS_Y, transY, prevActualHeight, actualHeight, prevViewHeight, viewHeight, drawableHeight); + + // + // Set the matrix to the adjusted scale and translate values. + // + matrix.setValues(m); + } + fixTrans(); + setImageMatrix(matrix); + } + + /** + * Set view dimensions based on layout params + * + * @param mode + * @param size + * @param drawableWidth + * @return + */ + private int setViewSize(int mode, int size, int drawableWidth) { + int viewSize; + switch (mode) { + case MeasureSpec.EXACTLY: + viewSize = size; + break; + + case MeasureSpec.AT_MOST: + viewSize = Math.min(drawableWidth, size); + break; + + case MeasureSpec.UNSPECIFIED: + viewSize = drawableWidth; + break; + + default: + viewSize = size; + break; + } + return viewSize; + } + + /** + * After rotating, the matrix needs to be translated. This function finds the area of image + * which was previously centered and adjusts translations so that is again the center, post-rotation. + * + * @param axis Matrix.MTRANS_X or Matrix.MTRANS_Y + * @param trans the value of trans in that axis before the rotation + * @param prevImageSize the width/height of the image before the rotation + * @param imageSize width/height of the image after rotation + * @param prevViewSize width/height of view before rotation + * @param viewSize width/height of view after rotation + * @param drawableSize width/height of drawable + */ + private void translateMatrixAfterRotate(int axis, float trans, float prevImageSize, float imageSize, int prevViewSize, int viewSize, int drawableSize) { + if (imageSize < viewSize) { + // + // The width/height of image is less than the view's width/height. Center it. + // + m[axis] = (viewSize - (drawableSize * m[Matrix.MSCALE_X])) * 0.5f; + + } else if (trans > 0) { + // + // The image is larger than the view, but was not before rotation. Center it. + // + m[axis] = -((imageSize - viewSize) * 0.5f); + + } else { + // + // Find the area of the image which was previously centered in the view. Determine its distance + // from the left/top side of the view as a fraction of the entire image's width/height. Use that percentage + // to calculate the trans in the new view width/height. + // + float percentage = (Math.abs(trans) + (0.5f * prevViewSize)) / prevImageSize; + m[axis] = -((percentage * imageSize) - (viewSize * 0.5f)); + } + } + + private void setState(State state) { + this.state = state; + } + + public boolean canScrollHorizontallyFroyo(int direction) { + return canScrollHorizontally(direction); + } + + @Override + public boolean canScrollHorizontally(int direction) { + matrix.getValues(m); + float x = m[Matrix.MTRANS_X]; + + if (getImageWidth() < viewWidth) { + return false; + + } else if (x >= -1 && direction < 0) { + return false; + + } else if (Math.abs(x) + viewWidth + 1 >= getImageWidth() && direction > 0) { + return false; + } + + return true; + } + + /** + * Gesture Listener detects a single click or long click and passes that on + * to the view's listener. + * @author Ortiz + * + */ + private class GestureListener extends GestureDetector.SimpleOnGestureListener { + + @Override + public boolean onSingleTapConfirmed(MotionEvent e) + { + if(doubleTapListener != null) { + return doubleTapListener.onSingleTapConfirmed(e); + } + return performClick(); + } + + @Override + public void onLongPress(MotionEvent e) + { + performLongClick(); + } + + @Override + public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) + { + if (fling != null) { + // + // If a previous fling is still active, it should be cancelled so that two flings + // are not run simultaenously. + // + fling.cancelFling(); + } + fling = new Fling((int) velocityX, (int) velocityY); + compatPostOnAnimation(fling); + return super.onFling(e1, e2, velocityX, velocityY); + } + + @Override + public boolean onDoubleTap(MotionEvent e) { + boolean consumed = false; + if(doubleTapListener != null) { + consumed = doubleTapListener.onDoubleTap(e); + } + if (state == State.NONE) { + float targetZoom = (normalizedScale == minScale) ? maxScale : minScale; + DoubleTapZoom doubleTap = new DoubleTapZoom(targetZoom, e.getX(), e.getY(), false); + compatPostOnAnimation(doubleTap); + consumed = true; + } + return consumed; + } + + @Override + public boolean onDoubleTapEvent(MotionEvent e) { + if(doubleTapListener != null) { + return doubleTapListener.onDoubleTapEvent(e); + } + return false; + } + } + + public interface OnTouchImageViewListener { + public void onMove(); + } + + /** + * Responsible for all touch events. Handles the heavy lifting of drag and also sends + * touch events to Scale Detector and Gesture Detector. + * @author Ortiz + * + */ + private class PrivateOnTouchListener implements OnTouchListener { + + // + // Remember last point position for dragging + // + private PointF last = new PointF(); + + @Override + public boolean onTouch(View v, MotionEvent event) { + mScaleDetector.onTouchEvent(event); + mGestureDetector.onTouchEvent(event); + PointF curr = new PointF(event.getX(), event.getY()); + + if (state == State.NONE || state == State.DRAG || state == State.FLING) { + switch (event.getAction()) { + case MotionEvent.ACTION_DOWN: + last.set(curr); + if (fling != null) + fling.cancelFling(); + setState(State.DRAG); + break; + + case MotionEvent.ACTION_MOVE: + if (state == State.DRAG) { + float deltaX = curr.x - last.x; + float deltaY = curr.y - last.y; + float fixTransX = getFixDragTrans(deltaX, viewWidth, getImageWidth()); + float fixTransY = getFixDragTrans(deltaY, viewHeight, getImageHeight()); + matrix.postTranslate(fixTransX, fixTransY); + fixTrans(); + last.set(curr.x, curr.y); + } + break; + + case MotionEvent.ACTION_UP: + case MotionEvent.ACTION_POINTER_UP: + setState(State.NONE); + break; + } + } + + setImageMatrix(matrix); + + // + // User-defined OnTouchListener + // + if(userTouchListener != null) { + userTouchListener.onTouch(v, event); + } + + // + // OnTouchImageViewListener is set: TouchImageView dragged by user. + // + if (touchImageViewListener != null) { + touchImageViewListener.onMove(); + } + + // + // indicate event was handled + // + return true; + } + } + + /** + * ScaleListener detects user two finger scaling and scales image. + * @author Ortiz + * + */ + private class ScaleListener extends ScaleGestureDetector.SimpleOnScaleGestureListener { + @Override + public boolean onScaleBegin(ScaleGestureDetector detector) { + setState(State.ZOOM); + return true; + } + + @Override + public boolean onScale(ScaleGestureDetector detector) { + scaleImage(detector.getScaleFactor(), detector.getFocusX(), detector.getFocusY(), true); + + // + // OnTouchImageViewListener is set: TouchImageView pinch zoomed by user. + // + if (touchImageViewListener != null) { + touchImageViewListener.onMove(); + } + return true; + } + + @Override + public void onScaleEnd(ScaleGestureDetector detector) { + super.onScaleEnd(detector); + setState(State.NONE); + boolean animateToZoomBoundary = false; + float targetZoom = normalizedScale; + if (normalizedScale > maxScale) { + targetZoom = maxScale; + animateToZoomBoundary = true; + + } else if (normalizedScale < minScale) { + targetZoom = minScale; + animateToZoomBoundary = true; + } + + if (animateToZoomBoundary) { + DoubleTapZoom doubleTap = new DoubleTapZoom(targetZoom, viewWidth / 2, viewHeight / 2, true); + compatPostOnAnimation(doubleTap); + } + } + } + + private void scaleImage(double deltaScale, float focusX, float focusY, boolean stretchImageToSuper) { + + float lowerScale, upperScale; + if (stretchImageToSuper) { + lowerScale = superMinScale; + upperScale = superMaxScale; + + } else { + lowerScale = minScale; + upperScale = maxScale; + } + + float origScale = normalizedScale; + normalizedScale *= deltaScale; + if (normalizedScale > upperScale) { + normalizedScale = upperScale; + deltaScale = upperScale / origScale; + } else if (normalizedScale < lowerScale) { + normalizedScale = lowerScale; + deltaScale = lowerScale / origScale; + } + + matrix.postScale((float) deltaScale, (float) deltaScale, focusX, focusY); + fixScaleTrans(); + } + + /** + * DoubleTapZoom calls a series of runnables which apply + * an animated zoom in/out graphic to the image. + * @author Ortiz + * + */ + private class DoubleTapZoom implements Runnable { + + private long startTime; + private static final float ZOOM_TIME = 500; + private float startZoom, targetZoom; + private float bitmapX, bitmapY; + private boolean stretchImageToSuper; + private AccelerateDecelerateInterpolator interpolator = new AccelerateDecelerateInterpolator(); + private PointF startTouch; + private PointF endTouch; + + DoubleTapZoom(float targetZoom, float focusX, float focusY, boolean stretchImageToSuper) { + setState(State.ANIMATE_ZOOM); + startTime = System.currentTimeMillis(); + this.startZoom = normalizedScale; + this.targetZoom = targetZoom; + this.stretchImageToSuper = stretchImageToSuper; + PointF bitmapPoint = transformCoordTouchToBitmap(focusX, focusY, false); + this.bitmapX = bitmapPoint.x; + this.bitmapY = bitmapPoint.y; + + // + // Used for translating image during scaling + // + startTouch = transformCoordBitmapToTouch(bitmapX, bitmapY); + endTouch = new PointF(viewWidth / 2, viewHeight / 2); + } + + @Override + public void run() { + float t = interpolate(); + double deltaScale = calculateDeltaScale(t); + scaleImage(deltaScale, bitmapX, bitmapY, stretchImageToSuper); + translateImageToCenterTouchPosition(t); + fixScaleTrans(); + setImageMatrix(matrix); + + // + // OnTouchImageViewListener is set: double tap runnable updates listener + // with every frame. + // + if (touchImageViewListener != null) { + touchImageViewListener.onMove(); + } + + if (t < 1f) { + // + // We haven't finished zooming + // + compatPostOnAnimation(this); + + } else { + // + // Finished zooming + // + setState(State.NONE); + } + } + + /** + * Interpolate between where the image should start and end in order to translate + * the image so that the point that is touched is what ends up centered at the end + * of the zoom. + * @param t + */ + private void translateImageToCenterTouchPosition(float t) { + float targetX = startTouch.x + t * (endTouch.x - startTouch.x); + float targetY = startTouch.y + t * (endTouch.y - startTouch.y); + PointF curr = transformCoordBitmapToTouch(bitmapX, bitmapY); + matrix.postTranslate(targetX - curr.x, targetY - curr.y); + } + + /** + * Use interpolator to get t + * @return + */ + private float interpolate() { + long currTime = System.currentTimeMillis(); + float elapsed = (currTime - startTime) / ZOOM_TIME; + elapsed = Math.min(1f, elapsed); + return interpolator.getInterpolation(elapsed); + } + + /** + * Interpolate the current targeted zoom and get the delta + * from the current zoom. + * @param t + * @return + */ + private double calculateDeltaScale(float t) { + double zoom = startZoom + t * (targetZoom - startZoom); + return zoom / normalizedScale; + } + } + + /** + * This function will transform the coordinates in the touch event to the coordinate + * system of the drawable that the imageview contain + * @param x x-coordinate of touch event + * @param y y-coordinate of touch event + * @param clipToBitmap Touch event may occur within view, but outside image content. True, to clip return value + * to the bounds of the bitmap size. + * @return Coordinates of the point touched, in the coordinate system of the original drawable. + */ + private PointF transformCoordTouchToBitmap(float x, float y, boolean clipToBitmap) { + matrix.getValues(m); + float origW = getDrawable().getIntrinsicWidth(); + float origH = getDrawable().getIntrinsicHeight(); + float transX = m[Matrix.MTRANS_X]; + float transY = m[Matrix.MTRANS_Y]; + float finalX = ((x - transX) * origW) / getImageWidth(); + float finalY = ((y - transY) * origH) / getImageHeight(); + + if (clipToBitmap) { + finalX = Math.min(Math.max(finalX, 0), origW); + finalY = Math.min(Math.max(finalY, 0), origH); + } + + return new PointF(finalX , finalY); + } + + /** + * Inverse of transformCoordTouchToBitmap. This function will transform the coordinates in the + * drawable's coordinate system to the view's coordinate system. + * @param bx x-coordinate in original bitmap coordinate system + * @param by y-coordinate in original bitmap coordinate system + * @return Coordinates of the point in the view's coordinate system. + */ + private PointF transformCoordBitmapToTouch(float bx, float by) { + matrix.getValues(m); + float origW = getDrawable().getIntrinsicWidth(); + float origH = getDrawable().getIntrinsicHeight(); + float px = bx / origW; + float py = by / origH; + float finalX = m[Matrix.MTRANS_X] + getImageWidth() * px; + float finalY = m[Matrix.MTRANS_Y] + getImageHeight() * py; + return new PointF(finalX , finalY); + } + + /** + * Fling launches sequential runnables which apply + * the fling graphic to the image. The values for the translation + * are interpolated by the Scroller. + * @author Ortiz + * + */ + private class Fling implements Runnable { + + CompatScroller scroller; + int currX, currY; + + Fling(int velocityX, int velocityY) { + setState(State.FLING); + scroller = new CompatScroller(context); + matrix.getValues(m); + + int startX = (int) m[Matrix.MTRANS_X]; + int startY = (int) m[Matrix.MTRANS_Y]; + int minX, maxX, minY, maxY; + + if (getImageWidth() > viewWidth) { + minX = viewWidth - (int) getImageWidth(); + maxX = 0; + + } else { + minX = maxX = startX; + } + + if (getImageHeight() > viewHeight) { + minY = viewHeight - (int) getImageHeight(); + maxY = 0; + + } else { + minY = maxY = startY; + } + + scroller.fling(startX, startY, (int) velocityX, (int) velocityY, minX, + maxX, minY, maxY); + currX = startX; + currY = startY; + } + + public void cancelFling() { + if (scroller != null) { + setState(State.NONE); + scroller.forceFinished(true); + } + } + + @Override + public void run() { + + // + // OnTouchImageViewListener is set: TouchImageView listener has been flung by user. + // Listener runnable updated with each frame of fling animation. + // + if (touchImageViewListener != null) { + touchImageViewListener.onMove(); + } + + if (scroller.isFinished()) { + scroller = null; + return; + } + + if (scroller.computeScrollOffset()) { + int newX = scroller.getCurrX(); + int newY = scroller.getCurrY(); + int transX = newX - currX; + int transY = newY - currY; + currX = newX; + currY = newY; + matrix.postTranslate(transX, transY); + fixTrans(); + setImageMatrix(matrix); + compatPostOnAnimation(this); + } + } + } + + @TargetApi(Build.VERSION_CODES.GINGERBREAD) + private class CompatScroller { + Scroller scroller; + OverScroller overScroller; + boolean isPreGingerbread; + + public CompatScroller(Context context) { + if (VERSION.SDK_INT < VERSION_CODES.GINGERBREAD) { + isPreGingerbread = true; + scroller = new Scroller(context); + + } else { + isPreGingerbread = false; + overScroller = new OverScroller(context); + } + } + + public void fling(int startX, int startY, int velocityX, int velocityY, int minX, int maxX, int minY, int maxY) { + if (isPreGingerbread) { + scroller.fling(startX, startY, velocityX, velocityY, minX, maxX, minY, maxY); + } else { + overScroller.fling(startX, startY, velocityX, velocityY, minX, maxX, minY, maxY); + } + } + + public void forceFinished(boolean finished) { + if (isPreGingerbread) { + scroller.forceFinished(finished); + } else { + overScroller.forceFinished(finished); + } + } + + public boolean isFinished() { + if (isPreGingerbread) { + return scroller.isFinished(); + } else { + return overScroller.isFinished(); + } + } + + public boolean computeScrollOffset() { + if (isPreGingerbread) { + return scroller.computeScrollOffset(); + } else { + overScroller.computeScrollOffset(); + return overScroller.computeScrollOffset(); + } + } + + public int getCurrX() { + if (isPreGingerbread) { + return scroller.getCurrX(); + } else { + return overScroller.getCurrX(); + } + } + + public int getCurrY() { + if (isPreGingerbread) { + return scroller.getCurrY(); + } else { + return overScroller.getCurrY(); + } + } + } + + @TargetApi(Build.VERSION_CODES.JELLY_BEAN) + private void compatPostOnAnimation(Runnable runnable) { + if (VERSION.SDK_INT >= VERSION_CODES.JELLY_BEAN) { + postOnAnimation(runnable); + + } else { + postDelayed(runnable, 1000/60); + } + } + + private class ZoomVariables { + public float scale; + public float focusX; + public float focusY; + public ScaleType scaleType; + + public ZoomVariables(float scale, float focusX, float focusY, ScaleType scaleType) { + this.scale = scale; + this.focusX = focusX; + this.focusY = focusY; + this.scaleType = scaleType; + } + } + + private void printMatrixInfo() { + float[] n = new float[9]; + matrix.getValues(n); + Log.d(DEBUG, "Scale: " + n[Matrix.MSCALE_X] + " TransX: " + n[Matrix.MTRANS_X] + " TransY: " + n[Matrix.MTRANS_Y]); + } +} \ No newline at end of file diff --git a/app/src/main/java/uk/co/blatech/blaupload/util/BlaImageLoader.java b/app/src/main/java/uk/co/blatech/blaupload/util/BlaImageLoader.java new file mode 100644 index 0000000..58d1c14 --- /dev/null +++ b/app/src/main/java/uk/co/blatech/blaupload/util/BlaImageLoader.java @@ -0,0 +1,139 @@ +package uk.co.blatech.blaupload.util; + +import android.content.res.Resources; +import android.graphics.Bitmap; +import android.graphics.BitmapFactory; +import android.graphics.Matrix; +import android.opengl.GLES10; +import android.os.AsyncTask; +import android.util.Log; + +import org.apache.http.HttpEntity; +import org.apache.http.HttpResponse; +import org.apache.http.StatusLine; +import org.apache.http.client.ClientProtocolException; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.impl.client.DefaultHttpClient; +import org.apache.http.util.EntityUtils; + +import java.io.IOException; + +import javax.microedition.khronos.opengles.GL10; + +import uk.co.blatech.blaupload.R; +import uk.co.blatech.blaupload.data.ImageItem; + +/** + * This class is used for loading images in the background + * + * Created by joe on 02/08/14. + */ +public class BlaImageLoader extends AsyncTask { + + private Resources res; + + //Get the resources object from the UI thread so we can load a placeholder image + public BlaImageLoader(Resources resources) { + this.res = resources; + } + + /** + * + * @param args host, filename, id + * host - the hostname to download from, eg http://www.blaupload.co.uk + * filename - the filename with no path, eg image.jpg + * id - the ID of the image used by the UI to order the images + * + * TODO: Some generic way of adding the suffix used for thumbnails + * + * @return an ImageItem object which contains the ID, a Bitmap image object, and the filename + */ + @Override + protected ImageItem doInBackground(String... args) { + + Bitmap bmp = null; + String host = args[0]; + String filename = args[1]; + int id = Integer.parseInt(args[2]); + int[] maxTextureSize = new int[1]; + maxTextureSize[0] = 4096; + GLES10.glGetIntegerv(GL10.GL_MAX_TEXTURE_SIZE, maxTextureSize, 0); + + //Add an extra .png on the end to match the current thumbnail filenames + String url = host + filename; + + DefaultHttpClient client = new DefaultHttpClient(); + HttpGet httpGet = new HttpGet(url); + + + try { + HttpResponse response = client.execute(httpGet); + StatusLine statusLine = response.getStatusLine(); + int statusCode = statusLine.getStatusCode(); + String[] parts = filename.split("\\."); + String extension = parts[parts.length-1]; + //Create the Bitmap if the file exists + if (statusCode == 200 && isImageExtension(extension)) { + // If the thumbnail wasn't found, show a placeholder + HttpEntity entity = response.getEntity(); + byte[] bytes = EntityUtils.toByteArray(entity); + try { + bmp = BitmapFactory.decodeByteArray(bytes, 0, bytes.length); + } catch (Exception e) { + Log.e(BlaImageLoader.class.toString(), "Failed decoding " + filename); + e.printStackTrace(); + bmp = BitmapFactory.decodeResource(res, R.drawable.x); + ImageItem image = new ImageItem(id, bmp, filename); + return image; + } + int bmpHeight = bmp.getHeight(); + int bmpWidth = bmp.getWidth(); + if (bmpWidth > maxTextureSize[0]) { + + float ratio = (float)maxTextureSize[0]/(float)bmpWidth; + Matrix matrix = new Matrix(); + bmpWidth = maxTextureSize[0]; + bmpHeight = Math.round(bmpHeight * ratio); + matrix.postScale(ratio, ratio); + bmp = Bitmap.createBitmap(bmp, 0, 0, bmpWidth, bmpHeight, matrix, false); + } else if (bmpHeight > maxTextureSize[0]) { + float ratio = (float)maxTextureSize[0]/(float)bmpHeight; + bmpHeight = maxTextureSize[0]; + bmpWidth = Math.round(bmpWidth * ratio); + Matrix matrix = new Matrix(); + matrix.postScale(ratio, ratio); + bmp = Bitmap.createBitmap(bmp, 0, 0, bmpWidth, bmpHeight, matrix, false); + } + } else { + //If the file doesn't exist, use the placeholder instead + bmp = BitmapFactory.decodeResource(res, R.drawable.x); + + } + + //TODO: Error handling (Not sure how we get here) + } catch (ClientProtocolException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + + ImageItem image = new ImageItem(id, bmp, filename); + return image; + } + + @Override + protected void onPostExecute(ImageItem result) { + EventBus.getInstance().post(new ImageLoaderResultEvent(result)); + + } + + private boolean isImageExtension(String extension) { + if (extension.equalsIgnoreCase("jpeg") || extension.equalsIgnoreCase("jpg") || + extension.equalsIgnoreCase("gif") || extension.equalsIgnoreCase("png")) { + return true; + } else { + return false; + } + } + +} diff --git a/app/src/main/java/uk/co/blatech/blaupload/util/ImageLoader.java b/app/src/main/java/uk/co/blatech/blaupload/util/ImageLoader.java deleted file mode 100644 index 4a2dbc5..0000000 --- a/app/src/main/java/uk/co/blatech/blaupload/util/ImageLoader.java +++ /dev/null @@ -1,141 +0,0 @@ -package uk.co.blatech.blaupload.util; - -import android.annotation.TargetApi; -import android.content.res.Resources; -import android.graphics.Bitmap; -import android.graphics.BitmapFactory; -import android.graphics.Matrix; -import android.opengl.GLES10; -import android.os.AsyncTask; -import android.os.Build; -import android.util.Log; - -import org.apache.http.HttpEntity; -import org.apache.http.HttpResponse; -import org.apache.http.StatusLine; -import org.apache.http.client.ClientProtocolException; -import org.apache.http.client.methods.HttpGet; -import org.apache.http.impl.client.DefaultHttpClient; -import org.apache.http.util.EntityUtils; - -import java.io.IOException; - -import javax.microedition.khronos.opengles.GL10; - -import uk.co.blatech.blaupload.R; -import uk.co.blatech.blaupload.data.ImageItem; - -/** - * This class is used for loading images in the background - * - * Created by joe on 02/08/14. - */ -public class ImageLoader extends AsyncTask { - - private Resources res; - - //Get the resources object from the UI thread so we can load a placeholder image - public ImageLoader(Resources resources) { - this.res = resources; - } - - /** - * - * @param args host, filename, id - * host - the hostname to download from, eg http://www.blaupload.co.uk - * filename - the filename with no path, eg image.jpg - * id - the ID of the image used by the UI to order the images - * - * TODO: Some generic way of adding the suffix used for thumbnails - * - * @return an ImageItem object which contains the ID, a Bitmap image object, and the filename - */ - @Override - protected ImageItem doInBackground(String... args) { - - Bitmap bmp = null; - String host = args[0]; - String filename = args[1]; - int id = Integer.parseInt(args[2]); - int[] maxTextureSize = new int[1]; - maxTextureSize[0] = 4096; - GLES10.glGetIntegerv(GL10.GL_MAX_TEXTURE_SIZE, maxTextureSize, 0); - - //Add an extra .png on the end to match the current thumbnail filenames - String url = host + filename; - - DefaultHttpClient client = new DefaultHttpClient(); - HttpGet httpGet = new HttpGet(url); - - - try { - HttpResponse response = client.execute(httpGet); - StatusLine statusLine = response.getStatusLine(); - int statusCode = statusLine.getStatusCode(); - String[] parts = filename.split("\\."); - String extension = parts[parts.length-1]; - //Create the Bitmap if the file exists - if (statusCode == 200 && isImageExtension(extension)) { - // If the thumbnail wasn't found, show a placeholder - HttpEntity entity = response.getEntity(); - byte[] bytes = EntityUtils.toByteArray(entity); - try { - bmp = BitmapFactory.decodeByteArray(bytes, 0, bytes.length); - } catch (Exception e) { - Log.e(ImageLoader.class.toString(), "Failed decoding " + filename); - e.printStackTrace(); - bmp = BitmapFactory.decodeResource(res, R.drawable.x); - ImageItem image = new ImageItem(id, bmp, filename); - return image; - } - int bmpHeight = bmp.getHeight(); - int bmpWidth = bmp.getWidth(); - if (bmpWidth > maxTextureSize[0]) { - - float ratio = (float)maxTextureSize[0]/(float)bmpWidth; - Matrix matrix = new Matrix(); - bmpWidth = maxTextureSize[0]; - bmpHeight = Math.round(bmpHeight * ratio); - matrix.postScale(ratio, ratio); - bmp = Bitmap.createBitmap(bmp, 0, 0, bmpWidth, bmpHeight, matrix, false); - } else if (bmpHeight > maxTextureSize[0]) { - float ratio = (float)maxTextureSize[0]/(float)bmpHeight; - bmpHeight = maxTextureSize[0]; - bmpWidth = Math.round(bmpWidth * ratio); - Matrix matrix = new Matrix(); - matrix.postScale(ratio, ratio); - bmp = Bitmap.createBitmap(bmp, 0, 0, bmpWidth, bmpHeight, matrix, false); - } - } else { - //If the file doesn't exist, use the placeholder instead - bmp = BitmapFactory.decodeResource(res, R.drawable.x); - - } - - //TODO: Error handling (Not sure how we get here) - } catch (ClientProtocolException e) { - e.printStackTrace(); - } catch (IOException e) { - e.printStackTrace(); - } - - ImageItem image = new ImageItem(id, bmp, filename); - return image; - } - - @Override - protected void onPostExecute(ImageItem result) { - EventBus.getInstance().post(new ImageLoaderResultEvent(result)); - - } - - private boolean isImageExtension(String extension) { - if (extension.equalsIgnoreCase("jpeg") || extension.equalsIgnoreCase("jpg") || - extension.equalsIgnoreCase("gif") || extension.equalsIgnoreCase("png")) { - return true; - } else { - return false; - } - } - -} diff --git a/app/src/main/res/layout/activity_image_pager.xml b/app/src/main/res/layout/activity_image_pager.xml index 74181fa..fc75d13 100644 --- a/app/src/main/res/layout/activity_image_pager.xml +++ b/app/src/main/res/layout/activity_image_pager.xml @@ -1,16 +1,7 @@ - - - - - + tools:context="uk.co.blatech.blaupload.ImagePager"/> \ No newline at end of file diff --git a/app/src/main/res/layout/image_pager_item.xml b/app/src/main/res/layout/image_pager_item.xml new file mode 100644 index 0000000..26d8083 --- /dev/null +++ b/app/src/main/res/layout/image_pager_item.xml @@ -0,0 +1,23 @@ + + + + + + + \ No newline at end of file -- cgit v1.2.3 From 5a3c6644096acef7d8b228b0c74d7c8cc3681f21 Mon Sep 17 00:00:00 2001 From: Joe Robinson Date: Wed, 13 Aug 2014 23:14:43 +0100 Subject: Added higher resolution image loading on zoom --- app/src/main/java/uk/co/blatech/blaupload/util/JSONLoader.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/src/main/java/uk/co/blatech/blaupload/util/JSONLoader.java b/app/src/main/java/uk/co/blatech/blaupload/util/JSONLoader.java index ffb9601..0ca8f5f 100644 --- a/app/src/main/java/uk/co/blatech/blaupload/util/JSONLoader.java +++ b/app/src/main/java/uk/co/blatech/blaupload/util/JSONLoader.java @@ -59,7 +59,7 @@ public class JSONLoader extends AsyncTask { DefaultHttpClient client = new DefaultHttpClient(); CookieStore cookieStore = new BasicCookieStore(); //TODO: Don't hard code the cookie. - BasicClientCookie cookie = new BasicClientCookie("password", "577f7781b7079fd9c0e67ef7e301617cf6eb317311ce8cd0b4b6b2652ffd14d5"); + BasicClientCookie cookie = new BasicClientCookie("password", "lolnopass"); cookie.setDomain(".blaupload.co.uk"); cookie.setPath("/"); cookieStore.addCookie(cookie); -- cgit v1.2.3