首页 文章

试图用相机中的图像替换可绘制的矢量,并且可绘制的图像在图像后面伸展?

提问于
浏览
0

我有一些代码设置,用户使用他们的相机拍摄照片,并在现有的图像视图中加载到页面上 - 当页面默认加载时,有一个占位符图像,但一旦他们用相机拍照,它就会填充imageview(排序),但图像矢量仍然显示在它后面,似乎拉伸整个容器?

作为参考,这里是两个图像,一个前后:

Before

Blank profile screen

After
stretched out image above profile data

所有我想要的是图像替换我作为占位符的drawable,并填充容纳它的容器的宽度,但我猜我的代码中的某些东西必定在某处出错 .

以下是此页面的布局文件:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/edit_profile_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.android.projectrc.activities.ViewProfileActivity">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/white"
        android:fitsSystemWindows="true">

        <include
            android:id="@+id/edit_profile_app_bar"
            layout="@layout/edit_profile_support_bar" />
    </android.support.design.widget.AppBarLayout>

    <android.support.v4.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical">

                <LinearLayout
                    android:id="@+id/profile_image_container"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="@color/lighter_gray"
                    android:gravity="center_vertical|center_horizontal"
                    android:minHeight="275dp">

                    <ImageView
                        android:id="@+id/profile_image"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:background="@drawable/account_image_background"
                        android:padding="1dp"
                        app:srcCompat="@drawable/ic_account_circle_white_48px" />
                </LinearLayout>

                <TextView
                    android:id="@+id/profile_name"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_horizontal"
                    android:text="Name"
                    android:textAlignment="center"
                    android:textAppearance="@style/TextAppearance.AppCompat.Large"
                    android:textStyle="bold" />

                <TextView
                    android:id="@+id/name_change_link"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_horizontal"
                    android:layout_margin="2dp"
                    android:text="Change Name" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_horizontal"
                    android:layout_marginBottom="3dp"
                    android:text="Private Details" />

                <android.support.v7.widget.RecyclerView
                    android:id="@+id/private_items_recycler"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content" />
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_horizontal"
                    android:text="Optional Details" />

                <android.support.v7.widget.RecyclerView
                    android:id="@+id/optional_items_recycler"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content" />

            </LinearLayout>
        </android.support.v4.widget.NestedScrollView>
</android.support.design.widget.CoordinatorLayout>

这是加载相机照片的代码:

private fun loadBitmapImage() {
    //get view dimensions
    val maxWidth = profile_image_container.width
    val maxHeight = profile_image_container.height

    //get image dimensions
    val options = BitmapFactory.Options()
    options.inJustDecodeBounds = true
    BitmapFactory.decodeFile(mProfilePhotoPath, options)
    Log.i(TAG, "image height :: ${options.outHeight}\nimage width :: ${options.outWidth}\n" +
            "image bitmap :: ${options.inBitmap}")
    //calculate image scaling (if necessary)
    options.inSampleSize = Utilities.calculateInSampleSize(options, maxWidth, maxHeight)

    //now we can load the image
    options.inJustDecodeBounds = false
    val smallBitmap = BitmapFactory.decodeFile(mProfilePhotoPath, options)
    val imageView = profile_image as AppCompatImageView
    imageView.setImageBitmap(smallBitmap)
}

并且(如果需要)这是calculateInSampleSize方法:

fun calculateInSampleSize(
        options: BitmapFactory.Options, reqWidth: Int, reqHeight: Int): Int {
    // Raw height and width of image
    val height = options.outHeight
    val width = options.outWidth
    var inSampleSize = 1

    if (height > reqHeight || width > reqWidth) {

        val halfHeight = height / 2
        val halfWidth = width / 2

        // Calculate the largest inSampleSize value that is a power of 2 and keeps both
        // height and width larger than the requested height and width.
        while (halfHeight / inSampleSize >= reqHeight && halfWidth / inSampleSize >= reqWidth) {
            inSampleSize *= 2
        }
    }

1 回答

  • 0

    我猜这与视图的整体逻辑以及如何替换图像有关 .

    当您的应用处于"default"状态时,容器将为 (w:match_parent, h:275dp) .

    这是您自己的基础,以便计算图像的下采样 .

    现在既然你不太可能拍摄具有该比例的照片(你的 profile_image_container 线性布局的比例),那么你要加载的_680171就不能完全适合 .

    所以这里有一些你能想到的建议:

    到您的NestedScrollView(因此顶部不会隐藏在AppBar下,并且您的ImageView实际上看起来垂直居中)

    • 尝试重新考虑如何显示从相机拍摄的图像(比率,或 calculateInSampleSize 功能内部,如您所愿) .

相关问题