diff options
author | Sunny Goyal <sunnygoyal@google.com> | 2015-05-15 17:01:14 -0700 |
---|---|---|
committer | Sunny Goyal <sunnygoyal@google.com> | 2015-05-19 11:51:23 -0700 |
commit | f52abd7758b8f245522936e941336e574debabf5 (patch) | |
tree | 04e44db278dd19e3059d2ccf888157dad0e0b294 /src/com/android/launcher3/widget/WidgetImageView.java | |
parent | 436eeb6b971fa27bf7df85494d4da0e878803665 (diff) | |
download | android_packages_apps_Trebuchet-f52abd7758b8f245522936e941336e574debabf5.tar.gz android_packages_apps_Trebuchet-f52abd7758b8f245522936e941336e574debabf5.tar.bz2 android_packages_apps_Trebuchet-f52abd7758b8f245522936e941336e574debabf5.zip |
Widget cell improvement
> Using a custom view for preview, instead of extensing image view
> Preventing relayout when applying preview
> Removing unnecessary method calls
Bug: 21133230
Change-Id: Iab12fa1e5c871ee43a9fb0e6b6af897fecfb345f
Diffstat (limited to 'src/com/android/launcher3/widget/WidgetImageView.java')
-rw-r--r-- | src/com/android/launcher3/widget/WidgetImageView.java | 68 |
1 files changed, 58 insertions, 10 deletions
diff --git a/src/com/android/launcher3/widget/WidgetImageView.java b/src/com/android/launcher3/widget/WidgetImageView.java index f1eaf6488..6f8fd897b 100644 --- a/src/com/android/launcher3/widget/WidgetImageView.java +++ b/src/com/android/launcher3/widget/WidgetImageView.java @@ -17,25 +17,73 @@ package com.android.launcher3.widget; import android.content.Context; +import android.graphics.Bitmap; import android.graphics.Canvas; +import android.graphics.Paint; +import android.graphics.Rect; +import android.graphics.RectF; import android.util.AttributeSet; -import android.widget.ImageView; +import android.view.View; -public class WidgetImageView extends ImageView { +/** + * View that draws a bitmap horizontally centered. If the image width is greater than the view + * width, the image is scaled down appropriately. + */ +public class WidgetImageView extends View { + + private final Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG); + private final RectF mDstRectF = new RectF(); + private Bitmap mBitmap; + + public WidgetImageView(Context context) { + super(context); + } public WidgetImageView(Context context, AttributeSet attrs) { super(context, attrs); } + public WidgetImageView(Context context, AttributeSet attrs, int defStyle) { + super(context, attrs, defStyle); + } + + public void setBitmap(Bitmap bitmap) { + mBitmap = bitmap; + invalidate(); + } + + public Bitmap getBitmap() { + return mBitmap; + } + @Override protected void onDraw(Canvas canvas) { - canvas.save(); - canvas.clipRect(getScrollX() + getPaddingLeft(), - getScrollY() + getPaddingTop(), - getScrollX() + getRight() - getLeft() - getPaddingRight(), - getScrollY() + getBottom() - getTop() - getPaddingBottom()); - - super.onDraw(canvas); - canvas.restore(); + if (mBitmap != null) { + updateDstRectF(); + canvas.drawBitmap(mBitmap, null, mDstRectF, mPaint); + } + } + + private void updateDstRectF() { + if (mBitmap.getWidth() > getWidth()) { + float scale = ((float) getWidth()) / mBitmap.getWidth(); + mDstRectF.set(0, 0, getWidth(), scale * mBitmap.getHeight()); + } else { + mDstRectF.set( + (getWidth() - mBitmap.getWidth()) * 0.5f, + 0, + (getWidth() + mBitmap.getWidth()) * 0.5f, + mBitmap.getHeight()); + } + } + + /** + * @return the bounds where the image was drawn. + */ + public Rect getBitmapBounds() { + updateDstRectF(); + Rect rect = new Rect(); + mDstRectF.round(rect); + return rect; } } |