gpt4 book ai didi

android - 如何以编程方式在 Android 的按钮中缩放或自动调整 Drawable Top 的图像?

转载 作者:行者123 更新时间:2023-11-30 00:34:11 24 4
gpt4 key购买 nike

我正在开发一个应用程序,我在 GridView 中加载了多个按钮。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:paddingLeft="25dp"
android:paddingRight="25dp"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical">
<GridView
android:id="@+id/gridview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:columnWidth="128dp"
android:numColumns="auto_fit"
android:verticalSpacing="5dp"
android:horizontalSpacing="5dp"
android:stretchMode="columnWidth"
android:gravity="center" />
</LinearLayout>

按钮是使用适配器动态添加的,但是如果我预定义的边界 (320x320) 在某些设备中看起来不错,而在其他设备中太小或太大,并且如果我选择 Intrinsic boundaries 它们总是微型的。

预定义边界 (320x320):

智能手机 View :

enter image description here

平板电脑 View :

enter image description here

内部边界 (136x136):

enter image description here

我将图像存储在Assets 目录中,您的第一个问题是为什么?

你可以告诉我,我应该将它们保存在 Resources/drawable 中,我知道这一点,但我有 100 多张图片,每张都存储在一个子目录中具有唯一 ID 的文件夹,其中可以包含至少一张具有如下 ID 的图像:1.jpg、2.jpg 等。

如您所见,如果我将所有这些图像和子目录移动到可绘制文件夹,它就不会工作 ( Can the Android drawable directory contain subdirectories? )。

我有这个功能是为了从 Assets 文件夹中获取 Drawable:

public Drawable GetImage(int ID)
{
var thumbnail = string.Format("Thumbnails/{0}/1.jpg", ID);
using (StreamReader sr = new StreamReader(Application.Context.Assets.Open(thumbnail)))
{
Drawable d = Drawable.CreateFromStream(sr.BaseStream, null);

d.SetBounds(0, 0, 320, 320);

return d;
}
}

以下是我如何将适配器中的数据加载到按钮:

btnRecipe.Text = data[position].name;
btnRecipe.SetHeight(560);
// btnRecipe.SetWidth(122);
// btnRecipe.SetCompoundDrawablesWithIntrinsicBounds();
btnRecipe.SetTextColor(new Android.Graphics.Color(255, 255, 255));
btnRecipe.SetCompoundDrawables(null, data[position].image, null, null);

我想知道是否有任何方法可以处理或自动调整图像,因为我有另一个例子,使用 Drawable 文件夹中的数据和 XML 代码,图像被缩放而没有其他任何东西,例如:

enter image description here

代码:

<Button
android:text="@string/BtnComidas"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/my_white"
android:id="@+id/BtnComidas"
android:drawableTop="@drawable/comidas" />

此外,我想澄清一下,这张图片比之前的图片 (136x136) 小 128x128,而且看起来好多了,我在 Drawable 文件夹中只有一张图片副本,我没有hdpi 或 mdpi 副本,如您所见,它在智能手机和平板电脑上看起来很棒,我不需要做任何额外的更改来缩放它。

一个可能的问题和答案,为什么我要使用 GridView 来加载按钮?例如,如果我应该使用另一个控件或布局。

我不知道同时显示图像和文本的另一种方法,我能够按预期处理点击事件。

此外,如果有人有其他方法可以在 GridView 中显示带有文本的多个图像或其他可以解决我当前问题的方法,我愿意接受任何建议。感谢您的帮助、时间、经验和有值(value)的知识。

最佳答案

我找到了可以帮助更多人的解决方案:

1) 您需要将 GridView 的列宽共享给构造函数:

GridDishes.Adapter = new GridViewAdapter(this, data, GridDishes.ColumnWidth, IsSmartphone());

2) 然后配置理想尺寸:

public GridViewAdapter(Context c, List<CompatedInfo> data, int size, bool isSmartphone)
{
NotifyDataSetChanged();
context = c;
this.data = data;

//Define the size that you're going to use
if (Size > 0)
Size = (int)(size * 0.8);

//This is going to help you to have the same size for all elements
if (isSmartphone)
Height = (int)(Size * 1.65);
else
Height = (int)(Size * 1.7);
}

3) 在适配器中定义新尺寸:

data[position].image.SetBounds(0, 0, Size, Size);

4) 如果您希望所有 View 具有相同的高度,则在构造函数中预定义高度:

btnRecipe.SetHeight(Height);

可选:

如果你想确定屏幕并像我对元素的最大尺寸做的那样做一些小的改变,这个函数很有用:

private bool IsSmartphone()
{
DisplayMetrics metrics = new DisplayMetrics();
WindowManager.DefaultDisplay.GetMetrics(metrics);

float yInches = metrics.HeightPixels / metrics.Ydpi;
float xInches = metrics.WidthPixels / metrics.Xdpi;
double diagonalInches = Math.Sqrt(xInches * xInches + yInches * yInches);

if (diagonalInches >= 6.5)
{
return false;
}
else
{
return true;
}
}

函数来源:

https://stackoverflow.com/a/24701063/2889347

关于android - 如何以编程方式在 Android 的按钮中缩放或自动调整 Drawable Top 的图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43693434/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com