本文介绍了如何在Android的所有大小创建图像响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要响应图像支持android.I所有尺寸在下面给出一个形象: -

I want to responsive image to support all size of android.I give a image below :-

我试图设计相同的布局,但我的布局是这样的: -

I tried to design same layout but my layout look like:-

谁能告诉我如何存档这一切。

Can anyone tell me how i archive all this.

推荐答案

您可以使用这样的:

/**
 * Created by mariano zorrilla on 11/8/15.
 */

import android.content.Context;
import android.util.AttributeSet;
import android.widget.ImageView;

public class AspectRatioImageView extends ImageView
{
    public SquareImageView(Context context)
    {
        super(context);
    }

    public SquareImageView(Context context, AttributeSet attrs)
    {
        super(context, attrs);
    }

    public SquareImageView(Context context, AttributeSet attrs, int defStyle)
    {
        super(context, attrs, defStyle);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
    {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        setMeasuredDimension(getMeasuredWidth(), getMeasuredWidth() + (getMeasuredWidth() / 2));
    }
}

和是的......欢迎您! PS:删除您的ImageView和使用本AspectRatioImageView。使用它在你希望你的项目里面的一些utils的文件夹或任何其他地方。

and yes... you're welcome! PS: Remove your ImageView and use this AspectRatioImageView. Use it over some Utils folder or any other place you want inside your project.

您不需要使用GridView这一点。您可以使用它作为常规的ImageView,并得到同样的好处。

You don't need to use a gridview for this. You can use it as regular ImageView and get the same benefits.

编辑:只有当你想使用一个gridview,使用:

ONLY if you want to use a gridview, use this:

<GridView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/grid_images"
        android:numColumns="2"
        android:verticalSpacing="8dp"
        android:horizontalSpacing="8dp"/>

您的网格项:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.your.package.utils.AspectRatioImageView
        android:id="@+id/grid_thumb_img"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:scaleType="centerCrop"
        android:layout_weight="1"
        android:src="@drawable/ic_loading" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:text="Image Title"
        android:id="@+id/title_text"
        android:textColor="#FFF"
        android:textSize="18dp"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:layout_marginTop="-80dp"
        android:layout_below="@+id/grid_thumb_img" />
</RelativeLayout>

最终结果:

这篇关于如何在Android的所有大小创建图像响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 21:25