在Android Studio中查询图像大小 [英] Query in image size in android studio

查看:162
本文介绍了在Android Studio中查询图像大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好.有一个640X480的球形图像.在图像中,您可以看到我将layout_width指定为849px,layout_height指定为680 px.我采用了蓝色的背景色.由于指定的layout_width远远大于640px(图像的宽度大小) ),然后为什么图片的左侧和右侧没有被蓝色背景包围.图片的顶部和底部被蓝色背景包围了.

Hello. There is a ball image of 640X480.In the image, as you can see I have specified layout_width as 849px and layout_height as 680 px.I have taken a background colour of blue.As layout_width specified is much more than 640px(width size of image), then why the image's left side and right side is not surrounded with blue background.Image's top and bottom is surrounded with blue background.

XML代码:

    <?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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/imageView3"
        android:layout_width="849px"
        android:layout_height="680px"
        android:background="#2196F3"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/ball_640x480" />
</androidx.constraintlayout.widget.ConstraintLayout> 

我无法理解的是在图像中,设置wrap_content之后,为什么图像的上下仍存在空白?

What I cant understand is in the image,after setting wrap_content also why is still there white space below and above the image?

下面是更新的xml:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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/imageView3"
        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/ball_640x480" />
</androidx.constraintlayout.widget.ConstraintLayout>

推荐答案

之所以给出您的情况,是因为该图像不如ImageView那样大.如果您希望图像完全填满View,则可以使用属性android:scaleType="centerCrop".您可以按照此视觉指南进行操作,以了解ImageView的不同比例类型并查看它在不同比例类型上的表现.

Your situation is given due to the fact that the image is not as big as the ImageView is. If you would like the image to completely fill the View you could use the property android:scaleType="centerCrop". You can follow along this visual guide on ImageView's different scale types and see how it behaves on different scale types.

如果您想在ImageView上使用边框,我将不使用android:background属性,因为您的图像将被绘制在背景的顶部,并且可能会完全覆盖它.

If you would like to have a border along the ImageView I would not use the android:background property because your image is going to be drawn on top of the background and probably completely cover it.

我的方法是拥有一个仅比图像视图大一点的父视图,并且该视图具有background属性.您可以使用FrameLayout轻松地执行此操作,并将某些android:layout_margin应用于ImageView,如下例所示:

My approach would be to have a parent view that is just a little bit bigger than the image view and that view hold the background property. You can easily do that with a FrameLayout and applying some android:layout_margin to the ImageView like the following example:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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">


    <FrameLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#2196F3"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <ImageView
            android:id="@+id/imageView3"
            android:gravity="center"
            android:layout_margin="16dp"
            android:layout_width="849px"
            android:layout_height="680px"
            app:srcCompat="@drawable/ball_640x480" />
    </FrameLayout>

</androidx.constraintlayout.widget.ConstraintLayout> 

与评论有关的屏幕截图:

Screenshot related to the comments:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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/imageView3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:srcCompat="@drawable/ball_640x480"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"/>

</androidx.constraintlayout.widget.ConstraintLayout> 

这篇关于在Android Studio中查询图像大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆