Fixed
Status Update
Comments
al...@android.com <al...@android.com>
[Deleted User] <[Deleted User]> #2
[Comment deleted]
[Deleted User] <[Deleted User]> #3
I can reproduce the same issue.
I have a custom ImageView with the following onDraw(Canvas canvas) method:
@Override
protected void onDraw(Canvas canvas) {
Drawable drawable = getDrawable();
Rect bounds = drawable.getBounds();
int w = bounds.right - bounds.left;
int h = bounds.bottom - bounds.top;
int left = getPaddingLeft();
int top = getPaddingTop();
int right = getPaddingRight();
int bottom = getPaddingBottom();
int width = getWidth() - left - right;
int height = getHeight() - top - bottom;
int saveCount = canvas.getSaveCount();
canvas.translate(left + width / 2, top + height / 2);
canvas.rotate(-mTargetDegree);
canvas.translate(-w / 2, -h / 2);
drawable.draw(canvas);
canvas.restoreToCount(saveCount);
}
This is the declaration of the ImageView in the layout file:
<com.example.customviews.RotateImageView
android:id="@+id/rotatable_imageview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/oval_button_solid_shape"
android:layout_margin="30dp"
android:src="@drawable/ic_audiotrack_24dp"/>
And this is the vector drawable file, generated using the new vector drawable creator included since Android Studio v1.4:
<?xml version="1.0" encoding="UTF-8"?>
<vector
xmlns:android="http://schemas.android.com/apk/res/android "
android:width="24dp"
android:height="24dp"
android:autoMirrored="true"
android:viewportHeight="24.0"
android:viewportWidth="24.0">
<path
android:fillColor="#FFFFFFFF"
android:pathData="M12,3v9.28c-0.47,-0.17 -0.97,-0.28 -1.5,-0.28C8.01,12 6,14.01 6,16.5S8.01,21 10.5,21c2.31,0 4.2,-1.75 4.45,-4H15V6h4V3h-7z" />
</vector>
I have attached three screenshots taken with a Nexus 5 woking on Android 6.0, but I can reproduce the same behavior in all the devices with the same Android version:
-In the first one, mTargetDegree = 0.
-In the second one, mTargetDegree = 90.
-In the third one, mTargetDegree = 85.
However the same code has been tested in some devices working on Android 5.1 and there is no problem. I have attached three more screenshots taken with a BQ Aquaris M4.5 (Android 5.1).
I have a custom ImageView with the following onDraw(Canvas canvas) method:
@Override
protected void onDraw(Canvas canvas) {
Drawable drawable = getDrawable();
Rect bounds = drawable.getBounds();
int w = bounds.right - bounds.left;
int h = bounds.bottom - bounds.top;
int left = getPaddingLeft();
int top = getPaddingTop();
int right = getPaddingRight();
int bottom = getPaddingBottom();
int width = getWidth() - left - right;
int height = getHeight() - top - bottom;
int saveCount = canvas.getSaveCount();
canvas.translate(left + width / 2, top + height / 2);
canvas.rotate(-mTargetDegree);
canvas.translate(-w / 2, -h / 2);
drawable.draw(canvas);
canvas.restoreToCount(saveCount);
}
This is the declaration of the ImageView in the layout file:
<com.example.customviews.RotateImageView
android:id="@+id/rotatable_imageview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/oval_button_solid_shape"
android:layout_margin="30dp"
android:src="@drawable/ic_audiotrack_24dp"/>
And this is the vector drawable file, generated using the new vector drawable creator included since Android Studio v1.4:
<?xml version="1.0" encoding="UTF-8"?>
<vector
xmlns:android="
android:width="24dp"
android:height="24dp"
android:autoMirrored="true"
android:viewportHeight="24.0"
android:viewportWidth="24.0">
<path
android:fillColor="#FFFFFFFF"
android:pathData="M12,3v9.28c-0.47,-0.17 -0.97,-0.28 -1.5,-0.28C8.01,12 6,14.01 6,16.5S8.01,21 10.5,21c2.31,0 4.2,-1.75 4.45,-4H15V6h4V3h-7z" />
</vector>
I have attached three screenshots taken with a Nexus 5 woking on Android 6.0, but I can reproduce the same behavior in all the devices with the same Android version:
-In the first one, mTargetDegree = 0.
-In the second one, mTargetDegree = 90.
-In the third one, mTargetDegree = 85.
However the same code has been tested in some devices working on Android 5.1 and there is no problem. I have attached three more screenshots taken with a BQ Aquaris M4.5 (Android 5.1).
zt...@google.com <zt...@google.com> #4
It will be fixed in the coming Android release.
Some background just FYI, before Marshmallow , canvas scaling + VectorDrawable(like ImageView) will not draw in the proper scale, therefore blurry can happen.
Now in Marshmallow, the scaling issue is fixed when there is no rotation, however rotation is broken in this 90 / 270 case.
For workaround, if it is static icon, try to rotate inside the VectorDrawable <group android:rotation="90" .. >
If canvas rotation is really needed here, maybe cache the VectorDrawble into a bitmap and draw the bitmap instead.
Some background just FYI, before Marshmallow , canvas scaling + VectorDrawable(like ImageView) will not draw in the proper scale, therefore blurry can happen.
Now in Marshmallow, the scaling issue is fixed when there is no rotation, however rotation is broken in this 90 / 270 case.
For workaround, if it is static icon, try to rotate inside the VectorDrawable <group android:rotation="90" .. >
If canvas rotation is really needed here, maybe cache the VectorDrawble into a bitmap and draw the bitmap instead.
[Deleted User] <[Deleted User]> #6
This affects RotateDrawable, however using View.setRotation seems to work fine.
an...@gmail.com <an...@gmail.com> #7
This is also and still not running:
// onDraw(Canvas canvas)
canvas.save();
canvas.rotate(angle); // 90 and 270 don't work
vectorDrawable.draw(canvas);
canvas.restore();
// onDraw(Canvas canvas)
canvas.save();
canvas.rotate(angle); // 90 and 270 don't work
vectorDrawable.draw(canvas);
canvas.restore();
fe...@gmail.com <fe...@gmail.com> #9
Happens with RotateDrawable as well on Android 6.0, on the N emulator seems fixed.
Description
but rotate in other degree like 88 works ,although with some blurring effect.
sample code to reproduce:
public void draw(Canvas cavas){
// prepare drawable
Drawable drawble = getContext().getDrawable(R.drawable.vector_drawable);
drawable.setBounds(0,0,300,300);
// manipulate canvas
int save_status = canvas.save();
canvas.rotate(90,300,300); // <-- 90 degree will not work
// draw
drawable.draw(canvas);
// restore canvas
canvas.restoreToCount(save_status);
}
sample vector resource:
<vector xmlns:android="
android:viewportWidth="108"
android:width="108dp"
android:height="144dp"
android:viewportHeight="144">
<path
android:pathData="M 0 0 L 65.7 0 L 65.7 120.6 L 108 120.6 L 108 144 L 42.3 144 L 42.3 23.4 L 0 23.4 Z"
android:fillColor="#7baaf7">
</path>
</vector>
potential cause:
I take a bit look at the draw implementation of VectorDrawable and it is case by the quick path return associated with scaleWidth && scaleHeight.
see attached picture.// master/graphics/java/android/graphics/drawable/VectorDrawable.java#L299
In my opinion,Matrix.MSCALE_X/Y is not necessary the real scale coefficient as the matrix is supposed to be a composite project matrix with multiple transformation.
and when rotating 90 degree(and other degrees),these values are equivalent zero.
my current workaround:
set scaleWidth/Height to getBounds()