简体   繁体   English

在网格布局中垂直和水平缩放图像

[英]Scale image both vertically and horizontally within a gridlayout

I am new to Android development, and I am trying to use the GridLayout to create rows with 3 columns, each column with a imageview. 我是Android开发的新手,我正在尝试使用GridLayout创建具有3列的行,每列具有一个imageview。 In this case I am using a square image created in photoshop, all red, just for testing. 在这种情况下,我使用的是在Photoshop中创建的正方形图像,全部为红色,仅用于测试。

This is the xml I currently use: 这是我当前使用的xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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/activity_menu"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="me.andrewlarsen.testapp.MenuActivity">

    <GridLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:rowCount="2"
        android:columnCount="3"
        android:gravity="center"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true">


        <ImageView
            android:layout_gravity="fill"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:srcCompat="@mipmap/redblock"
            android:layout_row="0"
            android:layout_column="0"
            android:layout_columnWeight="1"
            android:layout_rowWeight="1"
            android:scaleType="fitXY"
            android:id="@+id/imageView1" />

        <ImageView
            android:layout_gravity="fill"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:srcCompat="@mipmap/redblock"
            android:layout_row="0"
            android:layout_column="1"
            android:layout_columnWeight="1"
            android:layout_rowWeight="1"
            android:scaleType="fitXY"
            android:id="@+id/imageView2" />

        <ImageView
            android:layout_gravity="fill"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:srcCompat="@mipmap/redblock"
            android:layout_row="0"
            android:layout_column="2"
            android:layout_columnWeight="1"
            android:layout_rowWeight="1"
            android:scaleType="fitXY"
            android:id="@+id/imageView3" />

        <ImageView
            android:layout_gravity="fill"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:srcCompat="@mipmap/redblock"
            android:layout_row="1"
            android:layout_column="0"
            android:layout_columnWeight="1"
            android:layout_rowWeight="1"
            android:id="@+id/imageView4" />

        <ImageView
            android:layout_gravity="fill"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:srcCompat="@mipmap/redblock"
            android:layout_row="1"
            android:layout_column="1"
            android:layout_columnWeight="1"
            android:layout_rowWeight="1"
            android:id="@+id/imageView5" />

        <ImageView
            android:layout_gravity="fill"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:srcCompat="@mipmap/redblock"
            android:layout_row="1"
            android:layout_column="2"
            android:layout_columnWeight="1"
            android:layout_rowWeight="1"
            android:id="@+id/imageView6" />

    </GridLayout>
</RelativeLayout>

The xml above gives a result looking like this: 上面的xml给出如下结果:

在此处输入图片说明

So as you can see I got 2 rows, that is different from eachother. 如您所见,我得到了两行,彼此不同。 On all 3 columns in the first row I've added android:scaleType="fitXY" however it only scales horizontally, not vertically. 在第一行的所有3列中,我都添加了android:scaleType =“ fitXY”,但是它只能水平缩放,而不能垂直缩放。

I want it to scale up, in both directions so the result would be a square just larger. 我希望它在两个方向上都按比例放大,因此结果将是一个更大的正方形。

I am trying to make it look like this: 我试图使它看起来像这样:

在此处输入图片说明

When I am using android:scaleType="fitXY" I would expect the image to scale up both in X direction (horizontally) and Y direction (vertically) however it does not do so. 当我使用android:scaleType =“ fitXY”时,我希望图像在X方向(水平)和Y方向(垂直)都按比例缩放,但是它没有这样做。

Appreciate all the help I can get. 感谢所有我能得到的帮助。

Problem solved, had to use: 问题解决了,不得不使用:

android:adjustViewBounds="true"

in each imageview element 在每个imageview元素中

Try Remove weights (android:layout_columnWeight="1" and android:layout_rowWeight="1" ), and what size your redblock ? 尝试删除权重(android:layout_columnWeight =“ 1”和android:layout_rowWeight =“ 1”),以及您的redblock大小是多少? if possible send your image. 如果可能,发送您的图片。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM