Android GridView - 如何动态更改位图?

发布于 2024-10-11 20:41:35 字数 2569 浏览 7 评论 0原文

你好,我有一个 gridView,我用它来显示一些图片(不同级别的小拇指)。 当用户完成一个级别时,我想更改该级别的拇指。 (以某种方式表明它已经完成)。

我为每个级别创建了两个拇指。一张是原始的,一张是显示关卡已完成的。

但是如何更改图像的来源呢?

我用来绘制图像的代码如下所示。

主要活动:

/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.maps);

  GridView gridview = (GridView) findViewById(R.id.gridview);
  gridview.setAdapter(new ImageAdapter(this));

  gridview.setOnItemClickListener(new OnItemClickListener() {
    @Override
      public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
        //Open the map which was clicked on, if there is one
        if(position+1 > 1){
            Toast.makeText(maps.this, "Level " + (position+1) + " is not yet available!", Toast.LENGTH_SHORT).show();
        }else{
            Toast.makeText(maps.this, "Opening Level " + (position+1), Toast.LENGTH_SHORT).show();
            Intent myIntent = new Intent(v.getContext(), Tutorial2D.class);
            startActivity(myIntent);
        }
      }
  });
}

ImageAdapter 类:

public class ImageAdapter extends BaseAdapter {
private Context mContext;

public ImageAdapter(Context c) {
    mContext = c;
}

public int getCount() {
    return mThumbIds.length;
}

public Object getItem(int position) {
    return null;
}

public long getItemId(int position) {
    return 0;
}

// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
    ImageView imageView;
    if (convertView == null) {  // if it's not recycled, initialize some attributes
        imageView = new ImageView(mContext);
        imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imageView.setPadding(8, 8, 8, 8);
    } else {
        imageView = (ImageView) convertView;
    }

    //Changing
    imageView.setImageResource(mThumbIds[position]);
    return imageView;
}



// references to our images
private Integer[] mThumbIds = {
        R.drawable.map1, R.drawable.map2, R.drawable.map3,
        R.drawable.map4, R.drawable.map5, R.drawable.map6,
        R.drawable.map7, R.drawable.map8, R.drawable.map9,
        R.drawable.map10, R.drawable.map11, R.drawable.map12,
        R.drawable.map13, R.drawable.map14, R.drawable.map15,
        R.drawable.map16, R.drawable.map17, R.drawable.map18,
        R.drawable.map19
};

}

Hello I have a gridView which I use to show some pictures on (small thumb of diffrent levels).
When the user finishes one level, I would like to change the thumb for that level. (Somehow show that it has been completed).

I created two thumbs for each level. One is the original and one that shows that the level is completed.

But how can i change the source of the images?

The code which I use to draw the images looks like this.

The main activity:

/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.maps);

  GridView gridview = (GridView) findViewById(R.id.gridview);
  gridview.setAdapter(new ImageAdapter(this));

  gridview.setOnItemClickListener(new OnItemClickListener() {
    @Override
      public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
        //Open the map which was clicked on, if there is one
        if(position+1 > 1){
            Toast.makeText(maps.this, "Level " + (position+1) + " is not yet available!", Toast.LENGTH_SHORT).show();
        }else{
            Toast.makeText(maps.this, "Opening Level " + (position+1), Toast.LENGTH_SHORT).show();
            Intent myIntent = new Intent(v.getContext(), Tutorial2D.class);
            startActivity(myIntent);
        }
      }
  });
}

The ImageAdapter Class:

public class ImageAdapter extends BaseAdapter {
private Context mContext;

public ImageAdapter(Context c) {
    mContext = c;
}

public int getCount() {
    return mThumbIds.length;
}

public Object getItem(int position) {
    return null;
}

public long getItemId(int position) {
    return 0;
}

// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
    ImageView imageView;
    if (convertView == null) {  // if it's not recycled, initialize some attributes
        imageView = new ImageView(mContext);
        imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imageView.setPadding(8, 8, 8, 8);
    } else {
        imageView = (ImageView) convertView;
    }

    //Changing
    imageView.setImageResource(mThumbIds[position]);
    return imageView;
}



// references to our images
private Integer[] mThumbIds = {
        R.drawable.map1, R.drawable.map2, R.drawable.map3,
        R.drawable.map4, R.drawable.map5, R.drawable.map6,
        R.drawable.map7, R.drawable.map8, R.drawable.map9,
        R.drawable.map10, R.drawable.map11, R.drawable.map12,
        R.drawable.map13, R.drawable.map14, R.drawable.map15,
        R.drawable.map16, R.drawable.map17, R.drawable.map18,
        R.drawable.map19
};

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

七颜 2024-10-18 20:41:35

您可以根据拇指创建一个新的位图,并使用画布在其上绘制额外的图形。 将该位图传递给 imageview

然后你可以使用 imageView.setImageBitmap [edit]

[edit]类似

public View getView(int position, View convertView, ViewGroup parent) {
    ImageView imageView;
    if (convertView == null) {  // if it's not recycled, initialize some attributes
        imageView = new ImageView(mContext);
        imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imageView.setPadding(8, 8, 8, 8);
    } else {
        imageView = (ImageView) convertView;
    }

    if (mLevelfinished) {

        //create thumbnail bitmap
        mThumbBmp = BitmapFactory.decodeResource(mResources, mThumbIds[position]);

        //draw a check over the bitmap
        Canvas c = new Canvas(mThumbBmp);
        c.drawBitmap(mCheckBitmap, destX, destY, null);

        imageView.setImageBitmap(mThumbBmp);
    } else {
        imageView.setImageResource(mThumbIds[position]);
    }

    return imageView;
}

[edit2]

我现在发现我有点误解了你的问题.. doh。您已经拥有了这些拇指,而不是动态创建关卡完整拇指。在这种情况下,我将创建另一个包含这些已完成的拇指的数组,并从正确的数组中选择资源。为了坚持使用数组,也许也将关卡完成存储在一个数组中:

imageView.setImageResource((mLevelCompleted[position]) ? mThumbCompleteIds[position] : mThumbIds[position]);

You could create a new bitmap based off a thumb and draw your extra graphics over it using a canvas. Then you could pass that bitmap to the imageview using imageView.setImageBitmap

[edit]

something like

public View getView(int position, View convertView, ViewGroup parent) {
    ImageView imageView;
    if (convertView == null) {  // if it's not recycled, initialize some attributes
        imageView = new ImageView(mContext);
        imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imageView.setPadding(8, 8, 8, 8);
    } else {
        imageView = (ImageView) convertView;
    }

    if (mLevelfinished) {

        //create thumbnail bitmap
        mThumbBmp = BitmapFactory.decodeResource(mResources, mThumbIds[position]);

        //draw a check over the bitmap
        Canvas c = new Canvas(mThumbBmp);
        c.drawBitmap(mCheckBitmap, destX, destY, null);

        imageView.setImageBitmap(mThumbBmp);
    } else {
        imageView.setImageResource(mThumbIds[position]);
    }

    return imageView;
}

[edit2]

I now see I kinda misunderstood your question.. doh. Instead of creating the level complete thumb dynamically, you already have thumbs for those. In that case, I would create another array that has those completed thumbs and pick the resource from the correct array. And to stick with arrays, maybe store the level completion in one as well:

imageView.setImageResource((mLevelCompleted[position]) ? mThumbCompleteIds[position] : mThumbIds[position]);
七七 2024-10-18 20:41:35

我正在使用一个肮脏的技巧,当您事先知道将显示什么位图时,它可能会很有用。

我在可见的旁边设置了另一个不可见的ImageView,并操纵

myImageView.setVisibility(n); // (n = 0 = View.VISIBLE) or (n = 8 = View.GONE)

不要使用值为4的常量View.INVISIBLE,因为View.GONE 是正确的效果。当可见性设置为 View.GONE 时,该对象将不会占用布局中的空间。这类似于 CSS 中的 display: none;

I am using a dirty trick that might be useful when you know previously what bitmaps will be shown.

I set up another invisible ImageView just aside the visible one, and manipulate

myImageView.setVisibility(n); // (n = 0 = View.VISIBLE) or (n = 8 = View.GONE)

Do not use the constant View.INVISIBLE which values 4, because View.GONE is the right effect. When the visibility is set to View.GONE, the object will not take space in the layout. This is similar to display: none; from CSS.

念三年u 2024-10-18 20:41:35

这是我所做的:

gridview.setAdapter(new ImageAdapter(this));
gridview.setOnItemClickListener(new OnItemClickListener() {
  public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
    Toast.makeText(Punch.this, "" + mThumbIds[position], Toast.LENGTH_SHORT).show();
     mThumbIds[position]=R.drawable.crabx;//crabxImg;

  //here is where I would like to redraw the whole grid but it will not let me. 
  //Because ImageAdapter is not defined within this callback.
  }
})

Here is what I do:

gridview.setAdapter(new ImageAdapter(this));
gridview.setOnItemClickListener(new OnItemClickListener() {
  public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
    Toast.makeText(Punch.this, "" + mThumbIds[position], Toast.LENGTH_SHORT).show();
     mThumbIds[position]=R.drawable.crabx;//crabxImg;

  //here is where I would like to redraw the whole grid but it will not let me. 
  //Because ImageAdapter is not defined within this callback.
  }
})
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文