Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rotating ImageView and its background without crop

I've been searching a lot, but I can't get a solution for my problem. I can't use android:rotation as I want this app to be compatible with Android API under 11 version. My problem is something similar to this: Rotating a view in Android

I've 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();
}

The ImageView is part of a RelativeLayout where I place the image and some text.

But the image is cropped in the edges. How can I avoid that?

like image 620
nutshell Avatar asked Mar 27 '13 20:03

nutshell


People also ask

How to get Image rotation in Android?

Use ExifInterface for rotate image. Use this method for get correct value to be rotate captured image from camera. And put this code in Activity result method and get value to rotate image... String selectedImage = data.


1 Answers

You simply have to add:

android:clipChildren="false"

to the parent ViewGroup.

For 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();
    }
}

Which provides a clean solution before API Level 11

like image 93
Murmel Avatar answered Sep 17 '22 20:09

Murmel