【问题标题】:Loading BitMap Images - Xamarin.Andoird加载位图图像 - Xamarin.Android
【发布时间】:2017-06-04 15:47:24
【问题描述】:

我正在我的应用程序中执行位图调整大小。我有一个名为 Bitmap Images 的文件夹,它具有启动位图调整大小的功能。但是在我的适配器(这行代码)中,hold.Img.SetImageBitmap(BitmapImages.decodeSampledBitmapFromResource(getResources(),news[position].Image,100,100));,第一个参数getResource 被引发为错误。缺少什么?

public class MyAdapter : RecyclerView.Adapter

    {
        private JavaList<News> news;



        public MyAdapter(JavaList<News> news)
        {
            this.news = news;

        }

        //binding data to views
        public override void OnBindViewHolder(RecyclerView.ViewHolder holder, int position)
        {
            MyHolder hold = holder as MyHolder;
            hold.Comment.Text = news[position].Comment;
            //hold.Img.SetImageResource(news[position].Image);
            hold.Img.SetImageBitmap(BitmapImages.decodeSampledBitmapFromResource(getResources(),news[position].Image,100,100));


        }

【问题讨论】:

  • 发布你的错误
  • @Yupi,我还没有运行应用程序,因为资源已经被下划线了

标签: c# android bitmap


【解决方案1】:

问题是你没有通过Context。要访问getResources(),您必须通过Context。所以修改你的适配器类。

public class MyAdapter : RecyclerView.Adapter

{
    private JavaList<News> news;
    private Context context;



    public MyAdapter(JavaList<News> news, Context context)
    {
        this.news = news;
        this.context = context;
    }

   ......
 //Then in your OnBindViewHolder you can call getResources
  .....
   hold.Img.SetImageBitmap(BitmapImages.decodeSampledBitmapFromResource(context.getResources(),news[position].Image,100,100));
 }

【讨论】:

  • 此外,在此之后,您必须提供创建 MyAdapter 类实例的上下文,因为在类的构造函数中提供了上下文。
猜你喜欢
  • 2017-07-24
  • 1970-01-01
  • 2015-12-02
  • 2012-07-20
  • 1970-01-01
  • 2023-03-24
  • 2011-02-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多