【问题标题】:MonoDroid Scaling images in a GridViewMonoDroid 在 GridView 中缩放图像
【发布时间】:2013-01-07 01:50:29
【问题描述】:

我有一个 GridView,其中包含一组 ImageView。我在 GetView 中创建图像如下:

public override View GetView(int position, View convertView, ViewGroup parent)
{
    ImageView imageView;

    if (convertView == null)
    {  // if it's not recycled, initialize some attributes  
        imageView = new ImageView(context);
        imageView.LayoutParameters = new GridView.LayoutParams(118, 132); // images are all the same dimension
        imageView.SetScaleType(ImageView.ScaleType.FitXy);
        imageView.SetPadding(5, 5, 5, 0);
    }
    else
    {
        imageView = (ImageView)convertView;
    }

    imageView.SetImageResource(thumbIds[position]);
    return imageView;
}

// references to our images  
int[] thumbIds =
{  
    Resource.Drawable.IconAdvocacy, Resource.Drawable.IconBusinessServices,  
    Resource.Drawable.IconContactUs, Resource.Drawable.IconDistrict,  
    Resource.Drawable.IconEvents, Resource.Drawable.IconFind,  
    Resource.Drawable.IconLawsRegs, Resource.Drawable.IconHealth,  
    Resource.Drawable.IconPracMgmt, Resource.Drawable.IconPublications,  
    Resource.Drawable.IconUpdates, Resource.Drawable.IconUploadPhoto
};

如您所见,我在 LayoutParams 函数中定义了固定大小。这会导致每个设备上的图像大小相同。

问题:缩放图像的最佳方法是什么?

我应该: 1) 只需在每个可绘制、-hdpi、-ldpi 和-mdpi 文件夹中创建不同大小的图像?如果是这样,我如何指定 LayoutParams 的大小?

2) 获取 DisplayMetrics?如果是这样,我该怎么做?我一直无法获得有关如何执行此操作的 MonoDroid 信息。文档中的示例似乎不起作用。

非常感谢任何帮助!

谢谢

【问题讨论】:

    标签: android android-layout mono xamarin.android


    【解决方案1】:

    好吧,我认为由于您有固定数量的图像并且您没有动态加载它们,因此提前创建您需要的资源(就像您在选项 1 中指定的方式一样)可能足够安全。如果您如果需要动态加载或动态调整图像大小,那么我可能会建议使用选项 2 路径。

    选项 1

    为了获取所有布局的图像,您可以为 dpi 扩展指定 -land(横向)和 -port(纵向)扩展。因此,例如,您可以拥有 drawable-mdpi-port 和 drawable-mdpi-land。有关选项的完整列表和一些更详细的信息,请参阅Supporting Multiple Screens 文档。

    选项 2

    这段代码应该可以很好地通过代码获取当前的显示密度。如果您从活动中调用它,这应该完全有效。如果您是从片段或其他什么调用,请确保首先引用 Activity。

    var metrics = new DisplayMetrics();
    this.WindowManager.DefaultDisplay.GetMetrics(metrics);
    switch (metrics.DensityDpi)
    {
        case DisplayMetricsDensity.Default:
            break;
        case DisplayMetricsDensity.High:
            break;
        case DisplayMetricsDensity.Low:
            break;
        case DisplayMetricsDensity.Xhigh:
            break;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-15
      • 2014-12-14
      • 2021-10-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-29
      • 1970-01-01
      相关资源
      最近更新 更多