旋转ImageView及其背景,无需裁剪 [英] Rotating ImageView and its background without crop

查看:185
本文介绍了旋转ImageView及其背景,无需裁剪的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在搜索,但我无法解决我的问题。我不能使用 android:rotation ,因为我希望这个应用程序与11版本的Android API兼容。
我的问题与此类似:在Android中旋转视图

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

我做到了这一点:

@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是RelativeLayout的一部分,我放置图像和一些文本。

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?

推荐答案

您只需添加:

android:clipChildren="false"

给父母 ViewGroup

例如:

<?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:

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

在API Level 11之前提供了一个干净的解决方案

Which provides a clean solution before API Level 11

这篇关于旋转ImageView及其背景,无需裁剪的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆