繁体   English   中英

Android:如何在 ImageView 旋转后裁剪图像?

[英]Android: how to have a cropped image after ImageView rotation?

我在相对布局中旋转的 ImageView 有问题。 这是我的问题:

在此处输入图像描述

我该怎么做才能得到我想要的?

谢谢 !

我的 java 代码:

 previewImageView.setRotation(rotAngle);

我的 xml:

    <RelativeLayout
        android:id="@+id/previewLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <com.xxxx.SquareImageView
            android:id="@+id/previewBackgroundImageView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scaleType="fitXY"
            android:src="@drawable/dark_blue_page"
            />

        <com.xxxx.TouchImageView
            android:id="@+id/previewImageView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"

            android:layout_alignStart="@+id/previewBackgroundImageView"
            android:layout_alignTop="@+id/previewBackgroundImageView"
            android:layout_alignEnd="@+id/previewBackgroundImageView"
            android:layout_alignBottom="@+id/previewBackgroundImageView"

            android:layout_margin="50dp"
            android:scaleType="centerInside"
            />

    </RelativeLayout>

最简单的解决方案是将 ImageView 包装在 FrameLayout 中

像那样:

 <FrameLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="50dp"
    android:layout_alignStart="@+id/previewBackgroundImageView"
    android:layout_alignTop="@+id/previewBackgroundImageView"
    android:layout_alignEnd="@+id/previewBackgroundImageView"
    android:layout_alignBottom="@+id/previewBackgroundImageView"
    android:layout_alignLeft="@+id/previewBackgroundImageView"
    android:layout_alignRight="@+id/previewBackgroundImageView" >

     <com.xxxx.TouchImageView
        android:id="@+id/previewImageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:scaleType="centerInside" />

 </FrameLayout>

这样,FrameLayout 将剪裁其子视图中超出 FrameLayout 边界的任何部分。 这是基于默认值为true的视图属性android:clipChildren

缺点:这会给项目带来更多的布局嵌套,可能会损害应用程序的性能。 但是单独的一个 FrameLayout 本身不会造成任何伤害。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM