【问题标题】:Rotating ImageView and its background without crop旋转 ImageView 及其背景而不裁剪
【发布时间】:2013-03-27 20:51:45
【问题描述】:

我一直在寻找很多东西,但我无法为我的问题找到解决方案。我不能使用android:rotation,因为我希望这个应用程序与 11 版本以下的 Android API 兼容。 我的问题与此类似: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 的一部分,我在其中放置图像和一些文本。

但是图像的边缘被裁剪了。我怎样才能避免这种情况?

【问题讨论】:

    标签: android imageview


    【解决方案1】:

    您只需添加:

    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>
    

    旋转图像视图:

    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 级别 11 之前提供干净的解决方案

    【讨论】:

    • 你的剪辑子在这里没有工作,而是你的 300dp 高度工作:\
    • 您使用 API 级别
    【解决方案2】:

    尝试将您的 ImageView 添加为 FrameLayout 的子视图,并将 FrameLayouts 的宽度和高度设置为与您的 ImageView 相同。查看我的类似答案here。原始答案来自 Pavlo Viazovskyy here 的帖子。希望这会有所帮助!

    【讨论】:

    • 谢谢你的回复,我最后做的有点棘手,只是在draw里面做一些缩放:canvas.scale(0.9f, 0.9f, d.getIntrinsicWidth()/2, d. getIntrinsicHeight()/2);但我会记住你的选择。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-08-23
    • 2015-11-13
    • 2017-10-09
    • 1970-01-01
    • 1970-01-01
    • 2013-04-21
    • 2016-06-06
    相关资源
    最近更新 更多