如何缩放以框架布局显示的来自相机的图像?

时间:2018-07-17 11:28:47

标签: android android-studio scaling aspect-ratio android-framelayout

我需要缩放从相机拍摄并以帧布局(而不是在图像视图中)显示的图像,以便保持并保持其宽高比。 直到现在,我只能在imageviews中找到图像缩放比例,但我无法在framelayout中做到这一点。 注意:我在imageview的框架布局中显示来自Firebase数据库的太阳镜图片。 因此,基本上,framelayout会显示从相机拍摄的自拍,然后我将随机的太阳镜框放在其上,然后可以在framelayout区域上拖动和旋转。 下面的XML代码。

 <FrameLayout

        android:layout_width="match_parent"
        android:id="@+id/frm"
        android:layout_height="match_parent"
        android:layout_marginRight="6dp"
        android:layout_marginTop="4dp"
        android:layout_marginLeft="6dp"
        android:layout_centerInParent="true"
        android:adjustViewBounds="true"

        android:layout_gravity="fill"
        android:layout_marginBottom="8dp">

        <ImageView android:id="@+id/imageView"

            android:layout_width="match_parent"
            android:layout_height="fill_parent"
            android:layout_marginRight="6dp"
            android:layout_marginLeft="6dp"
            android:layout_gravity="center"
            android:scaleType="matrix"
            android:adjustViewBounds="true"/>
    </FrameLayout>

1 个答案:

答案 0 :(得分:0)

Bitmap.createScaledBitmap(yourBitmap, 50, 50, true);
ByteArrayOutputStream stream = new ByteArrayOutputStream();         
photo.compress(Bitmap.CompressFormat.JPEG,100,stream);
imageView.setImageBitmap(decodeSampledBitmapFromByte(stream.toByteArray(),50,50));

但是请确保您明智地使用它,因为它可能会抛出

  

java.lang.OutofMemoryError:位图大小超出VM预算。

为避免这种情况,请确保在解码位图之前先检查其尺寸。