diff options
author | Wu-cheng Li <wuchengli@google.com> | 2010-09-17 15:48:59 -0700 |
---|---|---|
committer | Wu-cheng Li <wuchengli@google.com> | 2010-09-28 15:11:45 -0700 |
commit | 9f73bd9a85d295091fae39dc256a122e1843e2e8 (patch) | |
tree | 61468fe0a61b24ec6cd3856629b2b87fedaf9efe /src/com/android/camera/RotateImageView.java | |
parent | 6bf9e407d0dc0cd7e2f1f4799987523b107e8c96 (diff) | |
download | LegacyCamera-9f73bd9a85d295091fae39dc256a122e1843e2e8.zip LegacyCamera-9f73bd9a85d295091fae39dc256a122e1843e2e8.tar.gz LegacyCamera-9f73bd9a85d295091fae39dc256a122e1843e2e8.tar.bz2 |
Add last captured image thumbnails for xlarge devices.
Change-Id: I96d5472b62a7ffcc57642c09a0a7567a19f6ed42
Diffstat (limited to 'src/com/android/camera/RotateImageView.java')
-rw-r--r-- | src/com/android/camera/RotateImageView.java | 147 |
1 files changed, 144 insertions, 3 deletions
diff --git a/src/com/android/camera/RotateImageView.java b/src/com/android/camera/RotateImageView.java index 40fd007..57a7f13 100644 --- a/src/com/android/camera/RotateImageView.java +++ b/src/com/android/camera/RotateImageView.java @@ -17,13 +17,30 @@ package com.android.camera; import android.content.Context; +import android.graphics.Bitmap; +import android.graphics.BitmapFactory; import android.graphics.Canvas; -import android.graphics.Rect; +import android.graphics.drawable.TransitionDrawable; +import android.graphics.drawable.BitmapDrawable; import android.graphics.drawable.Drawable; +import android.graphics.Rect; +import android.media.ThumbnailUtils; +import android.net.Uri; +import android.os.ParcelFileDescriptor; import android.util.AttributeSet; +import android.util.Log; import android.view.animation.AnimationUtils; +import android.view.ViewGroup.LayoutParams; import android.widget.ImageView; +import java.io.BufferedInputStream; +import java.io.BufferedOutputStream; +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; + /** * A @{code ImageView} which can rotate it's content. */ @@ -38,15 +55,21 @@ public class RotateImageView extends ImageView { private int mStartDegree = 0; private int mTargetDegree = 0; - private boolean mClockwise = false; + private boolean mClockwise = false, mEnableAnimation = true; private long mAnimationStartTime = 0; private long mAnimationEndTime = 0; + private Uri mUri; + public RotateImageView(Context context, AttributeSet attrs) { super(context, attrs); } + public void enableAnimation(boolean enable) { + mEnableAnimation = enable; + } + public void setDegree(int degree) { // make sure in the range of [0, 359] degree = degree >= 0 ? degree % 360 : degree % 360 + 360; @@ -72,7 +95,6 @@ public class RotateImageView extends ImageView { @Override protected void onDraw(Canvas canvas) { - Drawable drawable = getDrawable(); if (drawable == null) return; @@ -110,4 +132,123 @@ public class RotateImageView extends ImageView { drawable.draw(canvas); canvas.restoreToCount(saveCount); } + + private Bitmap mThumb; + private Drawable[] mThumbs; + private TransitionDrawable mThumbTransition; + + public void setData(Uri uri, Bitmap original) { + // Make sure uri and original are consistently both null or both + // non-null. + if (uri == null || original == null) { + uri = null; + original = null; + } + mUri = uri; + updateThumb(original); + } + + public Uri getUri() { + return mUri; + } + + private static final int BUFSIZE = 4096; + + // Stores the data from the specified file. + // Returns true for success. + public boolean storeData(String filePath) { + if (mUri == null) { + return false; + } + + FileOutputStream f = null; + BufferedOutputStream b = null; + DataOutputStream d = null; + try { + f = new FileOutputStream(filePath); + b = new BufferedOutputStream(f, BUFSIZE); + d = new DataOutputStream(b); + d.writeUTF(mUri.toString()); + mThumb.compress(Bitmap.CompressFormat.PNG, 100, d); + d.close(); + } catch (IOException e) { + return false; + } finally { + MenuHelper.closeSilently(f); + MenuHelper.closeSilently(b); + MenuHelper.closeSilently(d); + } + return true; + } + + // Loads the data from the specified file. + // Returns true for success. + public boolean loadData(String filePath) { + FileInputStream f = null; + BufferedInputStream b = null; + DataInputStream d = null; + try { + f = new FileInputStream(filePath); + b = new BufferedInputStream(f, BUFSIZE); + d = new DataInputStream(b); + Uri uri = Uri.parse(d.readUTF()); + Bitmap thumb = BitmapFactory.decodeStream(d); + setData(uri, thumb); + d.close(); + } catch (IOException e) { + return false; + } finally { + MenuHelper.closeSilently(f); + MenuHelper.closeSilently(b); + MenuHelper.closeSilently(d); + } + return true; + } + + private void updateThumb(Bitmap original) { + if (original == null) { + mThumb = null; + mThumbs = null; + setImageDrawable(null); + return; + } + + LayoutParams param = getLayoutParams(); + final int miniThumbWidth = param.width + - getPaddingLeft() - getPaddingRight(); + final int miniThumbHeight = param.height + - getPaddingTop() - getPaddingBottom(); + mThumb = ThumbnailUtils.extractThumbnail( + original, miniThumbWidth, miniThumbHeight); + Drawable drawable; + if (mThumbs == null || !mEnableAnimation) { + mThumbs = new Drawable[2]; + mThumbs[1] = new BitmapDrawable(getContext().getResources(), mThumb); + setImageDrawable(mThumbs[1]); + } else { + mThumbs[0] = mThumbs[1]; + mThumbs[1] = new BitmapDrawable(getContext().getResources(), mThumb); + mThumbTransition = new TransitionDrawable(mThumbs); + setImageDrawable(mThumbTransition); + mThumbTransition.startTransition(500); + } + } + + public boolean isUriValid() { + if (mUri == null) { + return false; + } + try { + ParcelFileDescriptor pfd = + getContext().getContentResolver().openFileDescriptor(mUri, "r"); + if (pfd == null) { + Log.e(TAG, "Fail to open URI. URI=" + mUri); + return false; + } + pfd.close(); + } catch (IOException ex) { + return false; + } + return true; + } } |