您好我有一个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
};
}
答案 0 :(得分:1)
你可以用拇指创建一个新的位图,并使用画布在其上绘制额外的图形。然后,您可以使用imageView.setImageBitmap
将该位图传递给imageview[编辑]
类似
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]);
答案 1 :(得分:0)
我遇到了同样的问题。 这是我的工作:
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.
}
})
答案 2 :(得分:0)
当你以前知道将显示什么位图时,我正在使用一个可能有用的脏技巧。
我在可见的旁边设置另一个不可见的ImageView
并操纵
myImageView.setVisibility(n); // (n = 0 = View.VISIBLE) or (n = 8 = View.GONE)
请勿使用值View.INVISIBLE
的常量4
,因为View.GONE
是正确的效果。当可见性设置为View.GONE
时,对象将不会占用布局中的空间。这类似于CSS中的display: none;
。