Rotating ImageView and its background without crop

丶灬走出姿态 提交于 2019-12-07 03:27:34

问题


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?


回答1:


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




回答2:


Try adding your ImageView as a child view of a FrameLayout and set the FrameLayouts width and height the same as that of your ImageView. View my similar answer here. Original answer relayed from a post by Pavlo Viazovskyy here. Hope this helps!



来源:https://stackoverflow.com/questions/15669201/rotating-imageview-and-its-background-without-crop

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!