Java Android:如何在ImageView旋转后生成裁剪图像?
我的ImageView有一个问题,我在相对位置旋转。我的问题是: 我该怎么做才能得到我想要的 谢谢 我的java代码:Java Android:如何在ImageView旋转后生成裁剪图像?,java,android,kotlin,imageview,android-imageview,Java,Android,Kotlin,Imageview,Android Imageview,我的ImageView有一个问题,我在相对位置旋转。我的问题是: 我该怎么做才能得到我想要的 谢谢 我的java代码: previewImageView.setRotation(rotAngle); 我的xml: <RelativeLayout android:id="@+id/previewLayout" android:layout_width="match_parent" andro
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>
最简单的解决方案是在FrameLayout中包装ImageView 就像这样:
<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
缺点:这会给项目带来更多的布局嵌套,可能会损害应用程序的性能。但是仅仅一个框架布局本身不会造成任何伤害。您是否尝试将TouchImageView作为孩子放到SquareImageView>?谢谢。不起作用。崩溃:SquareImageView无法转换为android.view.ViewGroup我认为更好的方法是在旋转后将中心裁剪设置为previewImageView。类似这样的内容:previewImageView.scaleType=ImageView.scaleType.CENTER\u CROPThanks。。。我确信我尝试了这个参数,但实际上没有。。。当它工作时。。。谢谢谢谢我最初使用的是@Bahador Eslami建议的CENTER_CROP,但它并没有完全解决我的问题(原因确实与我的项目有关)。所以我最后用了你一个很好的解决方案。谢谢