Scale bitmap shaders for target density

am: 0d2a46b733

Change-Id: I8e3274a9c6047d943336827e7e0c8563e40c2ca5
This commit is contained in:
Alan Viverette
2016-10-07 20:39:48 +00:00
committed by android-build-merger
3 changed files with 59 additions and 45 deletions

View File

@ -463,31 +463,14 @@ public class BitmapDrawable extends Drawable {
return isAutoMirrored() && getLayoutDirection() == LayoutDirection.RTL;
}
private void updateMirrorMatrix(float dx) {
if (mMirrorMatrix == null) {
mMirrorMatrix = new Matrix();
}
mMirrorMatrix.setTranslate(dx, 0);
mMirrorMatrix.preScale(-1.0f, 1.0f);
}
@Override
protected void onBoundsChange(Rect bounds) {
mDstRectAndInsetsDirty = true;
final Bitmap bitmap = mBitmapState.mBitmap;
final Shader shader = mBitmapState.mPaint.getShader();
if (shader != null) {
if (needMirroring()) {
updateMirrorMatrix(bounds.right - bounds.left);
shader.setLocalMatrix(mMirrorMatrix);
mBitmapState.mPaint.setShader(shader);
} else {
if (mMirrorMatrix != null) {
mMirrorMatrix = null;
shader.setLocalMatrix(Matrix.IDENTITY_MATRIX);
mBitmapState.mPaint.setShader(shader);
}
}
if (bitmap != null && shader != null) {
updateShaderMatrix(bitmap, mBitmapState.mPaint, shader, needMirroring());
}
}
@ -548,19 +531,7 @@ public class BitmapDrawable extends Drawable {
canvas.restore();
}
} else {
if (needMirroring) {
// Mirror the bitmap
updateMirrorMatrix(mDstRect.right - mDstRect.left);
shader.setLocalMatrix(mMirrorMatrix);
paint.setShader(shader);
} else {
if (mMirrorMatrix != null) {
mMirrorMatrix = null;
shader.setLocalMatrix(Matrix.IDENTITY_MATRIX);
paint.setShader(shader);
}
}
updateShaderMatrix(bitmap, paint, shader, needMirroring);
canvas.drawRect(mDstRect, paint);
}
@ -573,6 +544,51 @@ public class BitmapDrawable extends Drawable {
}
}
/**
* Updates the {@code paint}'s shader matrix to be consistent with the
* destination size and layout direction.
*
* @param bitmap the bitmap to be drawn
* @param paint the paint used to draw the bitmap
* @param shader the shader to set on the paint
* @param needMirroring whether the bitmap should be mirrored
*/
private void updateShaderMatrix(@NonNull Bitmap bitmap, @NonNull Paint paint,
@NonNull Shader shader, boolean needMirroring) {
final int sourceDensity = bitmap.getDensity();
final int targetDensity = mTargetDensity;
final boolean needScaling = sourceDensity != 0 && sourceDensity != targetDensity;
if (needScaling || needMirroring) {
final Matrix matrix = getOrCreateMirrorMatrix();
matrix.reset();
if (needMirroring) {
final int dx = mDstRect.right - mDstRect.left;
matrix.setTranslate(dx, 0);
matrix.setScale(-1, 1);
}
if (needScaling) {
final float densityScale = targetDensity / (float) sourceDensity;
matrix.postScale(densityScale, densityScale);
}
shader.setLocalMatrix(matrix);
} else {
mMirrorMatrix = null;
shader.setLocalMatrix(Matrix.IDENTITY_MATRIX);
}
paint.setShader(shader);
}
private Matrix getOrCreateMirrorMatrix() {
if (mMirrorMatrix == null) {
mMirrorMatrix = new Matrix();
}
return mMirrorMatrix;
}
private void updateDstRectAndInsetsIfDirty() {
if (mDstRectAndInsetsDirty) {
if (mBitmapState.mTileModeX == null && mBitmapState.mTileModeY == null) {