【问题标题】:How do I make the ImageView the same size as the image in pixels如何使 ImageView 的大小与图像的像素大小相同
【发布时间】:2021-07-16 14:16:14
【问题描述】:

我有一个大小为 242x314 像素的图像,但是当我把它放在 ImageView 中时,图像变成了 726x942,我在 LayoutInspector 中看到,layout_width 和 layout_height 设置为 wrap_content,这是为什么呢?我使 ImageView 的大小与图像相同。

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">


    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/eight_age" />

</android.support.constraint.ConstraintLayout>

【问题讨论】:

    标签: android


    【解决方案1】:

    获取图片的宽高后,需要通过java代码设置ImageView。这是它的java代码。

       public void setImage(String filePath){//If you have a bitmap use it directly
            Bitmap bitmap = BitmapFactory.decodeFile(filePath);
            int width = bitmap.getWidth();
            int height = bitmap.getHeight();
            ImageView imageView = findViewById(R.id.image);//Change id according you name it in xml
            imageView.setLayoutParams(new ConstraintLayout.LayoutParams(width,height));
        }
    

    这里是xml代码

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.constraint.ConstraintLayout 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:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">
    
    
        <ImageView
            android:id="@+id/imageView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:srcCompat="@drawable/eight_age" 
            android:id="@+id/image"<!--This line is for id change it according to need-->
            android:scaleType="fitCenter"/><!--Keep this line as is-->
    
    </android.support.constraint.ConstraintLayout>
    

    【讨论】:

    • 但我仍然想知道为什么 wrap_content 不能让图像显示其真实大小...
    • @AndLightLight 我不确定,但主要是因为 imageView 会自动尝试扩展并填充屏幕区域,或者还有一种可能性是它可以在除约束之外的其他布局上工作
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-16
    • 2013-12-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多