【问题标题】:How to Scale an image, coming from camera, displayed in a framelayout?如何缩放来自相机的图像,显​​示在框架布局中?
【发布时间】:2018-07-17 11:28:46
【问题描述】:

我需要缩放从相机拍摄并显示在 framelayout(而不是 imageview)中的图像,以便它保持并保持其纵横比。 到目前为止,我只能在 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>

【问题讨论】:

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


    【解决方案1】:
    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 预算。

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

    【讨论】:

    • 现在这就是我所指的,你写的(imageView.setImageBitmap)。有问题。我不是想在图像视图中缩放图像。我想缩放从相机拍摄的图像并作为背景显示在框架布局中。我想缩放图像以在 framelayout 中调整为 BG,这样它就不会失去纵横比并缩放到最佳尺寸。
    • @SaAdIrFan 粘贴您如何设置图像的 java 代码。我建议您使用GlidePicasso 图像缓存库以获得更好的性能。
    猜你喜欢
    • 2011-09-27
    • 1970-01-01
    • 1970-01-01
    • 2010-12-11
    • 1970-01-01
    • 1970-01-01
    • 2021-12-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多