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

时间:2018-01-04 22:43:46

标签: android kotlin

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

作为参考,这里有两张图片,分别是之前和之后:

之前
Blank profile screen

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 :(得分:0)

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

当您的应用处于&#34;默认&#34;容器状态为utf8_print("This is a test \U0001F600") [1] "This is a test ​"

这是您自己的基础,以便计算图像的下采样。<​​/ p>

既然你不太可能拍摄具有该比率的照片((w:match_parent, h:275dp)线性布局的比例),那么你要加载的profile_image_container就不适合完美。

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

  1. 将scaleType添加到ImageView。这里是指向每个人的代表的链接:https://robots.thoughtbot.com/android-imageview-scaletype-a-visual-guide
  2. 请务必添加imageView.setImageBitmap(smallBitmap)

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

  3. 尝试重新考虑如何显示从相机拍摄的图像(比率,或app:layout_behavior="android.support.design.widget.AppBarLayout$ScrollingViewBehavior"功能内部,如您所愿。)