ImageView 旋转 + 填充屏幕

4

我希望用旋转的ImageView填满屏幕。如何用ImageView填满屏幕以及如何旋转ImageView的问题已经在该网站上独立回答了多次。我的XML代码如下。

<ImageView
  android:id="@+id/video_frame"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:adjustViewBounds="true"
  android:scaleType="fitCenter"
  android:rotation="270"
/>

问题在于,旋转发生在图像被适配且未应用旋转之后,因此我得到的图像不会填充整个屏幕。
当然,我可以按照以下方式旋转图像(并删除 ImageView 旋转):
 Matrix mMatrix = new Matrix();
 mMatrix.postRotate(-90);
 ImageView video = (ImageView)findViewById(R.id.video_frame);
 Bitmap frame = BitmapFactory.decodeFile("path/to/file.jpg");
 Bitmap frame2 = Bitmap.createBitmap(frame, 0, 0, frame.getWidth(), frame.getHeight(), mMatrix, true);
 video.setImageBitmap(frame2);

问题在于这个应用是实时的,我每秒要提供多个帧(20+)。每帧的旋转似乎相当耗费性能,即使在这种情况下图像填满屏幕,结果也会出现延迟,并且不如没有矩阵旋转时那么响应。

因此,我的问题是是否可以仅通过XML或其他方式解决此问题,而无需对要显示的每个图像进行旋转?

1个回答

1
你可以使用 RotateLayout,将你的 ImageView 放在 RotateLayout 中,并在 RotateLayoutsetAngle() 方法中传递旋转角度。这样它只会旋转视图而不是图像或者说 bitmap,比其他图像旋转方法更快。

你可以在 Gradle 文件中添加其依赖。

用法:

在你的布局文件中添加:

<com.github.rongi.rotate_layout.layout.RotateLayout
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:angle="90"> <!-- Specify rotate angle here -->

    <YourLayoutHere
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
    </YourLayoutHere>
</com.github.rongi.rotate_layout.layout.RotateLayout>

看这里!您的布局将被旋转90度。

下载

compile 'rongi.rotate-layout:rotate-layout:3.0.0'

特点

以正确的方式处理所有触摸事件。您按下相同的按钮,您所触摸的按钮!布局以正确的方式进行测量。这意味着如果原始视图为50x100,则在旋转90度后,它将自我测量为100x50,并且可以适合具有此尺寸的另一个布局。

旋转布局链接


谢谢,这个方法奏效了。我必须设置RotateLayoutlayout_height="fill_parent"(在90度旋转后变为宽度)。 - rex123

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接