forked from upstream-mirrors/OpenTracks
merge heads
This commit is contained in:
@@ -28,7 +28,6 @@ import com.google.android.apps.mytracks.util.StatsUtils;
|
|||||||
import com.google.android.maps.mytracks.R;
|
import com.google.android.maps.mytracks.R;
|
||||||
|
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.graphics.Bitmap;
|
|
||||||
import android.net.Uri;
|
import android.net.Uri;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.support.v4.app.Fragment;
|
import android.support.v4.app.Fragment;
|
||||||
@@ -66,7 +65,6 @@ public class MarkerDetailFragment extends Fragment {
|
|||||||
|
|
||||||
private MyTracksProviderUtils myTracksProviderUtils;
|
private MyTracksProviderUtils myTracksProviderUtils;
|
||||||
private Waypoint waypoint;
|
private Waypoint waypoint;
|
||||||
private Bitmap bitmap;
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onCreate(Bundle savedInstanceState) {
|
public void onCreate(Bundle savedInstanceState) {
|
||||||
@@ -97,13 +95,6 @@ public class MarkerDetailFragment extends Fragment {
|
|||||||
update();
|
update();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void onPause() {
|
|
||||||
super.onPause();
|
|
||||||
if (bitmap != null) {
|
|
||||||
bitmap.recycle();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
|
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
|
||||||
inflater.inflate(R.menu.marker_detail, menu);
|
inflater.inflate(R.menu.marker_detail, menu);
|
||||||
@@ -180,8 +171,7 @@ public class MarkerDetailFragment extends Fragment {
|
|||||||
int displayWidth = defaultDisplay.getWidth();
|
int displayWidth = defaultDisplay.getWidth();
|
||||||
@SuppressWarnings("deprecation")
|
@SuppressWarnings("deprecation")
|
||||||
int displayHeight = defaultDisplay.getHeight();
|
int displayHeight = defaultDisplay.getHeight();
|
||||||
bitmap = PhotoUtils.setImageVew(
|
PhotoUtils.setImageVew(imageView, Uri.parse(photoUrl), displayWidth, displayHeight);
|
||||||
imageView, Uri.parse(photoUrl), displayWidth, displayHeight);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
TextView name = (TextView) getView().findViewById(R.id.marker_detail_waypoint_name);
|
TextView name = (TextView) getView().findViewById(R.id.marker_detail_waypoint_name);
|
||||||
|
|||||||
@@ -0,0 +1,172 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2013 Google Inc.
|
||||||
|
*
|
||||||
|
* 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 com.google.android.apps.mytracks.services.tasks;
|
||||||
|
|
||||||
|
import com.google.android.apps.mytracks.util.PhotoUtils;
|
||||||
|
|
||||||
|
import android.graphics.Bitmap;
|
||||||
|
import android.graphics.BitmapFactory;
|
||||||
|
import android.graphics.Matrix;
|
||||||
|
import android.media.ExifInterface;
|
||||||
|
import android.net.Uri;
|
||||||
|
import android.os.AsyncTask;
|
||||||
|
import android.util.Log;
|
||||||
|
import android.widget.ImageView;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.lang.ref.WeakReference;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A bitmap loader.
|
||||||
|
*
|
||||||
|
* @author Jimmy Shih
|
||||||
|
*/
|
||||||
|
public class BitmapLoader extends AsyncTask<Void, Void, Bitmap> {
|
||||||
|
private static final String TAG = BitmapLoader.class.getSimpleName();
|
||||||
|
|
||||||
|
private final WeakReference<ImageView> imageViewReference;
|
||||||
|
private final Uri uri;
|
||||||
|
private final int displayWidth;
|
||||||
|
private final int displayHeight;
|
||||||
|
|
||||||
|
public BitmapLoader(ImageView imageView, Uri uri, int displayWidth, int displayHeight) {
|
||||||
|
|
||||||
|
// Use a WeakReference to ensure the ImageView can be garbage collected
|
||||||
|
imageViewReference = new WeakReference<ImageView>(imageView);
|
||||||
|
this.uri = uri;
|
||||||
|
this.displayWidth = displayWidth;
|
||||||
|
this.displayHeight = displayHeight;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Uri getUri() {
|
||||||
|
return uri;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected Bitmap doInBackground(Void... params) {
|
||||||
|
|
||||||
|
// Get the image dimensions
|
||||||
|
BitmapFactory.Options options = new BitmapFactory.Options();
|
||||||
|
|
||||||
|
options.inJustDecodeBounds = true;
|
||||||
|
BitmapFactory.decodeFile(uri.getPath(), options);
|
||||||
|
|
||||||
|
// Set targetWidth and targetHeight
|
||||||
|
int targetWidth = displayWidth;
|
||||||
|
int targetHeight = displayHeight;
|
||||||
|
if (targetHeight == 0) {
|
||||||
|
targetHeight = (int) (targetWidth * ((float) options.outHeight / (float) options.outWidth));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set imageWidth and imageHeight based on image rotation
|
||||||
|
int rotation = getRotation();
|
||||||
|
int imageWidth;
|
||||||
|
int imageHeight;
|
||||||
|
|
||||||
|
if (rotation == 0 || rotation == 180) {
|
||||||
|
imageWidth = options.outWidth;
|
||||||
|
imageHeight = options.outHeight;
|
||||||
|
} else {
|
||||||
|
imageWidth = options.outHeight;
|
||||||
|
imageHeight = options.outWidth;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get a scaled down version of the image
|
||||||
|
options.inJustDecodeBounds = false;
|
||||||
|
options.inSampleSize = getInSampleSize(imageWidth, imageHeight, targetWidth, targetHeight);
|
||||||
|
options.inPurgeable = true;
|
||||||
|
|
||||||
|
Bitmap scaledBitmap = BitmapFactory.decodeFile(uri.getPath(), options);
|
||||||
|
|
||||||
|
// Get the final bitmap after rotating the scaled down image
|
||||||
|
Bitmap bitmap;
|
||||||
|
if (rotation == 0) {
|
||||||
|
bitmap = scaledBitmap;
|
||||||
|
} else {
|
||||||
|
Matrix matrix = new Matrix();
|
||||||
|
matrix.postRotate(rotation);
|
||||||
|
bitmap = Bitmap.createBitmap(
|
||||||
|
scaledBitmap, 0, 0, scaledBitmap.getWidth(), scaledBitmap.getHeight(), matrix, true);
|
||||||
|
scaledBitmap.recycle();
|
||||||
|
}
|
||||||
|
return bitmap;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void onPostExecute(Bitmap bitmap) {
|
||||||
|
if (isCancelled()) {
|
||||||
|
bitmap = null;
|
||||||
|
}
|
||||||
|
// If imageView is still around, set bitmap
|
||||||
|
if (imageViewReference != null && bitmap != null) {
|
||||||
|
ImageView imageView = imageViewReference.get();
|
||||||
|
if (imageView != null) {
|
||||||
|
BitmapLoader bitmapLoader = PhotoUtils.getBitmapLoader(imageView);
|
||||||
|
if (this == bitmapLoader) {
|
||||||
|
imageView.setImageBitmap(bitmap);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private int getRotation() {
|
||||||
|
try {
|
||||||
|
ExifInterface exifInterface = new ExifInterface(uri.getPath());
|
||||||
|
switch (exifInterface.getAttributeInt(
|
||||||
|
ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL)) {
|
||||||
|
case ExifInterface.ORIENTATION_ROTATE_90:
|
||||||
|
return 90;
|
||||||
|
case ExifInterface.ORIENTATION_ROTATE_180:
|
||||||
|
return 180;
|
||||||
|
case ExifInterface.ORIENTATION_ROTATE_270:
|
||||||
|
return 270;
|
||||||
|
default:
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
Log.e(TAG, "Unable to get photo orientation", e);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the in sample size.
|
||||||
|
*
|
||||||
|
* @param imageWidth the image width
|
||||||
|
* @param imageHeight the image height
|
||||||
|
* @param targetWidth the target width
|
||||||
|
* @param targetHeight the target height
|
||||||
|
*/
|
||||||
|
private int getInSampleSize(int imageWidth, int imageHeight, int targetWidth, int targetHeight) {
|
||||||
|
float widthRatio = 1;
|
||||||
|
if (imageWidth > targetWidth) {
|
||||||
|
widthRatio = (float) imageWidth / (float) targetWidth;
|
||||||
|
}
|
||||||
|
|
||||||
|
float heightRatio = 1;
|
||||||
|
if (imageHeight > targetHeight) {
|
||||||
|
heightRatio = (float) imageHeight / (float) targetHeight;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* To fit within the target area, return the larger sample ratio so the
|
||||||
|
* image will not be larger than the target dimensions. Use Math.floor to
|
||||||
|
* not under sample.
|
||||||
|
*/
|
||||||
|
return (int) Math.floor(Math.max(widthRatio, heightRatio));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -16,15 +16,12 @@
|
|||||||
|
|
||||||
package com.google.android.apps.mytracks.util;
|
package com.google.android.apps.mytracks.util;
|
||||||
|
|
||||||
import android.graphics.Bitmap;
|
import com.google.android.apps.mytracks.services.tasks.BitmapLoader;
|
||||||
import android.graphics.BitmapFactory;
|
|
||||||
import android.graphics.Matrix;
|
|
||||||
import android.media.ExifInterface;
|
|
||||||
import android.net.Uri;
|
import android.net.Uri;
|
||||||
import android.util.Log;
|
|
||||||
import android.widget.ImageView;
|
import android.widget.ImageView;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.lang.ref.WeakReference;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Utilities for photos.
|
* Utilities for photos.
|
||||||
@@ -33,8 +30,6 @@ import java.io.IOException;
|
|||||||
*/
|
*/
|
||||||
public class PhotoUtils {
|
public class PhotoUtils {
|
||||||
|
|
||||||
private static final String TAG = PhotoUtils.class.getSimpleName();
|
|
||||||
|
|
||||||
private PhotoUtils() {}
|
private PhotoUtils() {}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -45,108 +40,55 @@ public class PhotoUtils {
|
|||||||
* @param displayWidth the display width
|
* @param displayWidth the display width
|
||||||
* @param displayHeight the display height
|
* @param displayHeight the display height
|
||||||
*/
|
*/
|
||||||
public static Bitmap setImageVew(
|
public static void setImageVew(
|
||||||
ImageView imageView, Uri uri, int displayWidth, int displayHeight) {
|
ImageView imageView, Uri uri, int displayWidth, int displayHeight) {
|
||||||
|
if (cancelBitmapLoader(imageView, uri)) {
|
||||||
// Get the image dimensions
|
BitmapLoader bitmapLoader = new BitmapLoader(imageView, uri, displayWidth, displayHeight);
|
||||||
BitmapFactory.Options options = new BitmapFactory.Options();
|
WeakReference<BitmapLoader> bitmapLoaderReference = new WeakReference<BitmapLoader>(
|
||||||
|
bitmapLoader);
|
||||||
options.inJustDecodeBounds = true;
|
imageView.setTag(bitmapLoaderReference);
|
||||||
BitmapFactory.decodeFile(uri.getPath(), options);
|
bitmapLoader.execute();
|
||||||
|
|
||||||
// Set targetWidth and targetHeight
|
|
||||||
int targetWidth = displayWidth;
|
|
||||||
int targetHeight = displayHeight;
|
|
||||||
if (targetHeight == 0) {
|
|
||||||
targetHeight = (int) (targetWidth * ((float) options.outHeight / (float) options.outWidth));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set imageWidth and imageHeight based on image rotation
|
|
||||||
int rotation = getRotation(uri);
|
|
||||||
int imageWidth;
|
|
||||||
int imageHeight;
|
|
||||||
|
|
||||||
if (rotation == 0 || rotation == 180) {
|
|
||||||
imageWidth = options.outWidth;
|
|
||||||
imageHeight = options.outHeight;
|
|
||||||
} else {
|
|
||||||
imageWidth = options.outHeight;
|
|
||||||
imageHeight = options.outWidth;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Get a scaled down version of the image
|
|
||||||
options.inJustDecodeBounds = false;
|
|
||||||
options.inSampleSize = getInSampleSize(imageWidth, imageHeight, targetWidth, targetHeight);
|
|
||||||
options.inPurgeable = true;
|
|
||||||
|
|
||||||
Bitmap scaledBitmap = BitmapFactory.decodeFile(uri.getPath(), options);
|
|
||||||
|
|
||||||
// Get the final bitmap after rotating the scaled down image
|
|
||||||
Bitmap bitmap;
|
|
||||||
if (rotation == 0) {
|
|
||||||
bitmap = scaledBitmap;
|
|
||||||
} else {
|
|
||||||
Matrix matrix = new Matrix();
|
|
||||||
matrix.postRotate(rotation);
|
|
||||||
bitmap = Bitmap.createBitmap(
|
|
||||||
scaledBitmap, 0, 0, scaledBitmap.getWidth(), scaledBitmap.getHeight(), matrix, true);
|
|
||||||
scaledBitmap.recycle();
|
|
||||||
}
|
|
||||||
|
|
||||||
imageView.setImageBitmap(bitmap);
|
|
||||||
return bitmap;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the image rotation
|
* Gets the image view bitmap loader.
|
||||||
*
|
*
|
||||||
* @param uri the image uri
|
* @param imageView the image view
|
||||||
*/
|
*/
|
||||||
private static int getRotation(Uri uri) {
|
public static BitmapLoader getBitmapLoader(ImageView imageView) {
|
||||||
try {
|
if (imageView != null) {
|
||||||
ExifInterface exifInterface = new ExifInterface(uri.getPath());
|
Object object = imageView.getTag();
|
||||||
switch (exifInterface.getAttributeInt(
|
if (object instanceof WeakReference<?>) {
|
||||||
ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL)) {
|
@SuppressWarnings("unchecked")
|
||||||
case ExifInterface.ORIENTATION_ROTATE_90:
|
WeakReference<BitmapLoader> bitmapLoaderReference = (WeakReference<BitmapLoader>) object;
|
||||||
return 90;
|
return bitmapLoaderReference.get();
|
||||||
case ExifInterface.ORIENTATION_ROTATE_180:
|
|
||||||
return 180;
|
|
||||||
case ExifInterface.ORIENTATION_ROTATE_270:
|
|
||||||
return 270;
|
|
||||||
default:
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
} catch (IOException e) {
|
|
||||||
Log.e(TAG, "Unable to get photo orientation", e);
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the in sample size.
|
* Cancels the image view bitmap loader.
|
||||||
*
|
*
|
||||||
* @param imageWidth the image width
|
* @param imageView the image view
|
||||||
* @param imageHeight the image height
|
* @param uri the uri
|
||||||
* @param targetWidth the target width
|
* @return false if the bitmap loader shouldn't be canceled. True if there is
|
||||||
* @param targetHeight the target height
|
* no bitmap loader or the bitmap loader is cancelled.
|
||||||
*/
|
*/
|
||||||
private static int getInSampleSize(
|
private static boolean cancelBitmapLoader(ImageView imageView, Uri uri) {
|
||||||
int imageWidth, int imageHeight, int targetWidth, int targetHeight) {
|
BitmapLoader bitmapLoaderAsyncTask = getBitmapLoader(imageView);
|
||||||
float widthRatio = 1;
|
|
||||||
if (imageWidth > targetWidth) {
|
|
||||||
widthRatio = (float) imageWidth / (float) targetWidth;
|
|
||||||
}
|
|
||||||
|
|
||||||
float heightRatio = 1;
|
if (bitmapLoaderAsyncTask != null) {
|
||||||
if (imageHeight > targetHeight) {
|
if (bitmapLoaderAsyncTask.getUri().equals(uri)) {
|
||||||
heightRatio = (float) imageHeight / (float) targetHeight;
|
// same bitmap loader is already in progress, don't cancel
|
||||||
|
return false;
|
||||||
|
} else {
|
||||||
|
// cancel previous bitmap loader
|
||||||
|
bitmapLoaderAsyncTask.cancel(true);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
// imageview has no bitmap loader, or an existing bitmap loader is cancelled
|
||||||
/*
|
return true;
|
||||||
* To fit within the target area, return the larger sample ratio so the
|
|
||||||
* image will not be larger than the target dimensions. Use Math.floor to
|
|
||||||
* not under sample.
|
|
||||||
*/
|
|
||||||
return (int) Math.floor(Math.max(widthRatio, heightRatio));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user