Allow scaling BackgroundImageView
Fixes: 133174106
Test: Manually
Change-Id: I4ea8ec52b89a6d7ef41d273ff2eb55689837e8cc
diff --git a/car-apps-common/res/values/attrs.xml b/car-apps-common/res/values/attrs.xml
index 877e5d2..f99da29 100644
--- a/car-apps-common/res/values/attrs.xml
+++ b/car-apps-common/res/values/attrs.xml
@@ -109,4 +109,11 @@
</attr>
<attr name="state_ux_restricted" format="boolean" />
</declare-styleable>
+
+ <declare-styleable name="BackgroundImageView">
+ <!-- Sets a scale to be applied on top of the scaling that was used to fit the image to the frame of the view. Defaults to 1.05 -->
+ <attr name="imageAdditionalScale" format="float"/>
+ </declare-styleable>
+ <!-- Attribute for specifying a default style for all BackgroundImageViews -->
+ <attr name="backgroundImageViewStyle" format="reference"/>
</resources>
diff --git a/car-apps-common/src/com/android/car/apps/common/BackgroundImageView.java b/car-apps-common/src/com/android/car/apps/common/BackgroundImageView.java
index 935034c..954b82a 100644
--- a/car-apps-common/src/com/android/car/apps/common/BackgroundImageView.java
+++ b/car-apps-common/src/com/android/car/apps/common/BackgroundImageView.java
@@ -16,6 +16,7 @@
package com.android.car.apps.common;
import android.content.Context;
+import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.util.AttributeSet;
import android.view.View;
@@ -42,7 +43,7 @@
}
public BackgroundImageView(Context context, AttributeSet attrs) {
- this(context, attrs, 0);
+ this(context, attrs, R.attr.backgroundImageViewStyle);
}
public BackgroundImageView(Context context, AttributeSet attrs, int defStyle) {
@@ -55,6 +56,16 @@
mBitmapTargetSize = getResources().getInteger(R.integer.background_bitmap_target_size_px);
mBitmapBlurPercent = getResources().getFloat(R.dimen.background_bitmap_blur_percent);
+
+ TypedArray a = context.getTheme().obtainStyledAttributes(attrs,
+ R.styleable.BackgroundImageView, defStyle, 0);
+
+ try {
+ setImageAdditionalScale(a.getFloat(R.styleable.BackgroundImageView_imageAdditionalScale,
+ 1.05f));
+ } finally {
+ a.recycle();
+ }
}
/**
@@ -90,6 +101,18 @@
/** Dims/undims the background image by 30% */
public void setDimmed(boolean dim) {
- mDarkeningScrim.setVisibility(dim ? View.VISIBLE : View.GONE);
+ mDarkeningScrim.setVisibility(dim ? View.VISIBLE : View.GONE);
+ }
+
+ /**
+ * Sets a scale to be applied on top of the scaling that was used to fit the
+ * image to the frame of the view.
+ *
+ * See {@link
+ * com.android.car.apps.common.CropAlignedImageView#setImageAdditionalScale(float)}
+ * for more details.
+ */
+ public void setImageAdditionalScale(float scale) {
+ mImageView.setImageAdditionalScale(scale);
}
}
diff --git a/car-apps-common/src/com/android/car/apps/common/CropAlignedImageView.java b/car-apps-common/src/com/android/car/apps/common/CropAlignedImageView.java
index 6464507..f11f33b 100644
--- a/car-apps-common/src/com/android/car/apps/common/CropAlignedImageView.java
+++ b/car-apps-common/src/com/android/car/apps/common/CropAlignedImageView.java
@@ -33,6 +33,9 @@
private static final int ALIGN_HORIZONTAL_RIGHT = 2;
private int mAlignHorizontal;
+ private float mAdditionalScale = 1f;
+ private int mFrameWidth;
+ private int mFrameHeight;
public CropAlignedImageView(Context context) {
this(context, null);
@@ -64,36 +67,58 @@
@Override
protected boolean setFrame(int frameLeft, int frameTop, int frameRight, int frameBottom) {
- if (getDrawable() != null) {
- setMatrix(frameRight - frameLeft, frameBottom - frameTop);
- }
+ mFrameWidth = frameRight - frameLeft;
+ mFrameHeight = frameBottom - frameTop;
+
+ setMatrix();
return super.setFrame(frameLeft, frameTop, frameRight, frameBottom);
}
- private void setMatrix(int frameWidth, int frameHeight) {
- float originalImageWidth = (float) getDrawable().getIntrinsicWidth();
- float originalImageHeight = (float) getDrawable().getIntrinsicHeight();
- float fitHorizontallyScaleFactor = frameWidth / originalImageWidth;
- float fitVerticallyScaleFactor = frameHeight / originalImageHeight;
- float usedScaleFactor = Math.max(fitHorizontallyScaleFactor, fitVerticallyScaleFactor);
- float newImageWidth = originalImageWidth * usedScaleFactor;
- float newImageHeight = originalImageHeight * usedScaleFactor;
- Matrix matrix = getImageMatrix();
- matrix.setScale(usedScaleFactor, usedScaleFactor, 0, 0);
- float dx = 0;
- switch (mAlignHorizontal) {
- case ALIGN_HORIZONTAL_CENTER:
- dx = (frameWidth - newImageWidth) / 2;
- break;
- case ALIGN_HORIZONTAL_LEFT:
- dx = 0;
- break;
- case ALIGN_HORIZONTAL_RIGHT:
- dx = (frameWidth - newImageWidth);
- break;
+ private void setMatrix() {
+ if (getDrawable() != null) {
+ float originalImageWidth = (float) getDrawable().getIntrinsicWidth();
+ float originalImageHeight = (float) getDrawable().getIntrinsicHeight();
+ float fitHorizontallyScaleFactor = mFrameWidth / originalImageWidth;
+ float fitVerticallyScaleFactor = mFrameHeight / originalImageHeight;
+ float usedScaleFactor = Math.max(fitHorizontallyScaleFactor, fitVerticallyScaleFactor);
+
+ // mAdditionalScale isn't factored into the fittedImageWidth
+ // because we want to scale from the center of the fitted image, so our translations
+ // shouldn't take it into effect
+ float fittedImageWidth = originalImageWidth * usedScaleFactor;
+
+ Matrix matrix = new Matrix();
+ matrix.setTranslate(-originalImageWidth / 2f, -originalImageHeight / 2f);
+ matrix.postScale(usedScaleFactor * mAdditionalScale,
+ usedScaleFactor * mAdditionalScale);
+ float dx = 0;
+ switch (mAlignHorizontal) {
+ case ALIGN_HORIZONTAL_CENTER:
+ dx = mFrameWidth / 2f;
+ break;
+ case ALIGN_HORIZONTAL_LEFT:
+ dx = fittedImageWidth / 2f;
+ break;
+ case ALIGN_HORIZONTAL_RIGHT:
+ dx = (mFrameWidth - fittedImageWidth / 2f);
+ break;
+ }
+ matrix.postTranslate(dx, mFrameHeight / 2f);
+ setImageMatrix(matrix);
}
- matrix.postTranslate(dx, (frameHeight - newImageHeight) / 2);
- setImageMatrix(matrix);
+ }
+
+ /**
+ * Sets a scale to be applied on top of the scaling that was used to fit the
+ * image to the frame of the view.
+ *
+ * This will scale the image from its center. This means it won't translate the image
+ * any further, so if it was aligned to the left, the left of the image will expand past
+ * the left edge of the view. Values <1 will cause black bars to appear.
+ */
+ public void setImageAdditionalScale(float scale) {
+ mAdditionalScale = scale;
+ setMatrix();
}
}
diff --git a/car-apps-common/src/com/android/car/apps/common/CrossfadeImageView.java b/car-apps-common/src/com/android/car/apps/common/CrossfadeImageView.java
index d13d928..1f4b7b6 100644
--- a/car-apps-common/src/com/android/car/apps/common/CrossfadeImageView.java
+++ b/car-apps-common/src/com/android/car/apps/common/CrossfadeImageView.java
@@ -147,4 +147,14 @@
mInactiveImageView = mImageView2;
}
}
+
+ /**
+ * Sets the additional image scale. See {@link
+ * com.android.car.apps.common.CropAlignedImageView#setImageAdditionalScale(float)}
+ * for more details.
+ */
+ public void setImageAdditionalScale(float scale) {
+ mImageView2.setImageAdditionalScale(scale);
+ mImageView1.setImageAdditionalScale(scale);
+ }
}