ImageView rotation and its background without crop

I searched a lot, but I can not solve the problem. I can’t use android:rotation, because I want this application to be compatible with the Android API under version 11. My problem is something like this: Rotating the view in Android

I have done this:

@Override
public void draw(Canvas canvas) {
    canvas.save();
    Drawable d = getDrawable();
    canvas.rotate(-10, d.getIntrinsicWidth() / 2, d.getIntrinsicHeight() / 2);
    super.draw(canvas);
    canvas.restore();
}

ImageView is part of the RelativeLayout where I put the image and some text.

But the image is cropped around the edges. How can i avoid this?

+4
source share
2 answers

You just need to add:

android:clipChildren="false"

parental ViewGroup.

Example:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
    android:layout_width="300dp"
    android:layout_height="300dp"
    android:clipChildren="false">
    <com.yourcompany.views.RotateImageView
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:background="@android:color/white"
        android:src="@drawable/ic_launcher" />
</FrameLayout>

RotateImageView:

public class RotateImageView extends ImageView {
    @Override
    public void draw(Canvas canvas) {
         canvas.save();
         int height = canvas.getHeight();
         int width = canvas.getWidth();
         canvas.rotate(45, height / 2, width / 2);
         super.draw(canvas);
         canvas.restore();
    }
}

What provides a clean solution up to API level 11

+7
source

ImageView FrameLayout FrameLayouts , ImageView. . . , !

0

All Articles