如何以编程方式在 Android 中的按钮中缩放或自动适应 Drawable Top 的图像?

费德里科·纳瓦雷特

我正在开发一个应用程序,我在一个 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) 在其他设备中太小或太大并且如果我选择了内在边界,它们总是微缩的。

预定义边界(320x320)

智能手机视图:

在此处输入图片说明

平板电脑视图:

在此处输入图片说明

内在边界 (136x136):

在此处输入图片说明

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

You could tell me me that I should save them in the Resources/drawable and I'm aware of it, but I have more than 100 images and each of them are stored in a sub-folders with a unique IDs, which can contains at least one image with an ID like this one: 1.jpg, 2.jpg, etc.

As you can appreciate if I move all those images and sub-directories to the drawable folder, it just not going to work (Can the Android drawable directory contain subdirectories?).

I have this function in order to get the Drawable from the Assets folder:

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;
    }
}

Here is how I load the data in the Adapter to the Buttons:

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);

I'd like to know if there is any way to deal or Auto Fit the images because I have another example that with data from the Drawable folder and XML code the image is scaled without anything else for example:

在此处输入图片说明

Code:

<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" />

Also, I'd like to clarify that this image is smaller 128x128 than the previous ones (136x136), furthermore, it looks much better and I have just one copy of the image in the Drawable folder, I don't have a hdpi or a mdpi copies and as you can see it looks great in smartphones and tablets, I didn't need to do any additional change in order to scale it.

A possible questions and answer, why am I using a GridView in order to load the buttons? If I should use another control or Layout, for instance.

I didn't know another way in order to display an image and text at the same time and I was able to handle the click event as I expected.

Besides, I'm open to any suggestion if someone has another approach of how displaying several images with text in a GridView or something else that could solve my current problem. Thanks for your help, time, experience and worthy knowledge.

Federico Navarrete

I was able to find a solution to my problem that could help more people too:

1) You need to share the Column Width of your GridView to the constructor:

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

2) Then you configure the ideal size:

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) Define the new size in the adapter:

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

4) If you want that all views have the same Height, the height is predefined in the Constructor:

btnRecipe.SetHeight(Height);

Optional:

如果您想确定屏幕并像我对元素的最大尺寸所做的那样进行一些小的更改,则此功能很有用:

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何以编程方式关闭Android中的对话框?

来自分类Dev

如何以编程方式在Android中打开Flashlight

来自分类Dev

如何以编程方式在表行中添加图像?

来自分类Dev

Android如何以编程方式将多个图像映射到多个图像按钮?

来自分类Dev

如何以编程方式在Android中制作水平线

来自分类Dev

如何以编程方式更改Android L中的原色?

来自分类Dev

如何以编程方式按下android的物理按钮

来自分类Dev

Android-以编程方式使用@android:drawable / AnImage设置图像

来自分类Dev

如何以编程方式重新启动android中的服务?

来自分类Dev

如何以编程方式在Android中绘制以下图像?

来自分类Dev

如何以编程方式在Android应用中实现点击?

来自分类Dev

如何以编程方式在表行中添加图像?

来自分类Dev

如何以编程方式从Android中的服务器发送和接收图像

来自分类Dev

如何以编程方式使按钮在Android中可滚动?

来自分类Dev

如何在Android中以编程方式减少图像?

来自分类Dev

如何以编程方式创建图像按钮菜单?

来自分类Dev

如何在Android中以编程方式征集whatsapp图像?

来自分类Dev

如何以编程方式在android中自动执行触摸

来自分类Dev

如何以编程方式在Android布局中添加新按钮

来自分类Dev

如何以编程方式在android中的linearlayout中给textview位置

来自分类Dev

如何以编程方式在UITableViewCells中居中放置图像?

来自分类Dev

如何以编程方式在Android中重置铃声

来自分类Dev

如何以编程方式删除图像的背景,使其在android中透明

来自分类Dev

如何以编程方式获取当前在Android中播放的歌曲

来自分类Dev

Android Xamarin,如何以编程方式在EditText中设置nextfocusdown?

来自分类Dev

如何以编程方式更改Android中的GPS设置?

来自分类Dev

如何以编程方式在android中打开隐藏文件?

来自分类Dev

如何以编程方式在android sqlite中找出表的主键?

来自分类Dev

如何以编程方式在android中从html + css生成图像jpg?

Related 相关文章

  1. 1

    如何以编程方式关闭Android中的对话框?

  2. 2

    如何以编程方式在Android中打开Flashlight

  3. 3

    如何以编程方式在表行中添加图像?

  4. 4

    Android如何以编程方式将多个图像映射到多个图像按钮?

  5. 5

    如何以编程方式在Android中制作水平线

  6. 6

    如何以编程方式更改Android L中的原色?

  7. 7

    如何以编程方式按下android的物理按钮

  8. 8

    Android-以编程方式使用@android:drawable / AnImage设置图像

  9. 9

    如何以编程方式重新启动android中的服务?

  10. 10

    如何以编程方式在Android中绘制以下图像?

  11. 11

    如何以编程方式在Android应用中实现点击?

  12. 12

    如何以编程方式在表行中添加图像?

  13. 13

    如何以编程方式从Android中的服务器发送和接收图像

  14. 14

    如何以编程方式使按钮在Android中可滚动?

  15. 15

    如何在Android中以编程方式减少图像?

  16. 16

    如何以编程方式创建图像按钮菜单?

  17. 17

    如何在Android中以编程方式征集whatsapp图像?

  18. 18

    如何以编程方式在android中自动执行触摸

  19. 19

    如何以编程方式在Android布局中添加新按钮

  20. 20

    如何以编程方式在android中的linearlayout中给textview位置

  21. 21

    如何以编程方式在UITableViewCells中居中放置图像?

  22. 22

    如何以编程方式在Android中重置铃声

  23. 23

    如何以编程方式删除图像的背景,使其在android中透明

  24. 24

    如何以编程方式获取当前在Android中播放的歌曲

  25. 25

    Android Xamarin,如何以编程方式在EditText中设置nextfocusdown?

  26. 26

    如何以编程方式更改Android中的GPS设置?

  27. 27

    如何以编程方式在android中打开隐藏文件?

  28. 28

    如何以编程方式在android sqlite中找出表的主键?

  29. 29

    如何以编程方式在android中从html + css生成图像jpg?

热门标签

归档