我正在尝试创建徽标测验,但是我无法弄清楚如何从ImageAdapter修改特定图像。任何指导都将受到高度赞赏。谢谢!
这是我的测验活动:
public class QuizActivity extends Activity {
private InterstitialAd interstitial;
private static int SIZE = 50;
public static int location;
private int counter = 0;
// initialize buttons
public static ImageButton buttons[] = new ImageButton[SIZE];
// array names
public static String[] names = { "hannibal", "orphan black", "lord of the rings",
"pretty little liars", "harry potter", "star trek",
"the fault in our stars", "iron man", "thor", "frozen",
"harry potter", "game of thrones", "hannibal", "doctor who",
"sherlock", "attack on titan", "teen wolf", "game of thrones",
"doctor who", "sherlock", "supernatural", "the hunger games",
"american horror story", "breaking bad", "attack on titan",
"teen wolf", "doctor who", "sherlock", "supernatural", "sherlock",
"arrow", "the big bang theory", "mad men", "arrested development",
"game of thrones", "breaking bad", "sons of anarchy", "the graduate",
"et", "scooby doo", "wizard of oz", "linux", "simpsons", "iron man", "wonder woman",
"green lantern", "flash", "captain america", "thor", "the picture of dorian gray"
};
// array images
public int[] images = { R.drawable.image1, R.drawable.image2,
R.drawable.image3, R.drawable.image4, R.drawable.image5,
R.drawable.image6, R.drawable.image7, R.drawable.image8,
R.drawable.image9, R.drawable.image10, R.drawable.image11,
R.drawable.image12, R.drawable.image13, R.drawable.image14,
R.drawable.image15, R.drawable.image16, R.drawable.image17,
R.drawable.image18, R.drawable.image19, R.drawable.image20,
R.drawable.image21, R.drawable.image22, R.drawable.image23,
R.drawable.image24, R.drawable.image25, R.drawable.image26,
R.drawable.image27, R.drawable.image28, R.drawable.image29,
R.drawable.image30,
R.drawable.image31, R.drawable.image32, R.drawable.image33,
R.drawable.image34, R.drawable.image35, R.drawable.image36,
R.drawable.image37, R.drawable.image38, R.drawable.image39,
R.drawable.image40,
R.drawable.image41, R.drawable.image42, R.drawable.image43,
R.drawable.image44, R.drawable.image45, R.drawable.image46,
R.drawable.image47, R.drawable.image48, R.drawable.image49,
R.drawable.image50};
GridView gridview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gridview = (GridView) findViewById(R.id.gridview);
gridview.setAdapter(new ImageAdapter(this));
gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
//Toast.makeText(QuizActivity.this, "" + position, Toast.LENGTH_SHORT).show();
// PopUpActivity
Intent intent = new Intent(QuizActivity.this, PopUpWindow.class);
intent.putExtra("position", position);
intent.putExtra("images", images);
intent.putExtra("names", names);
startActivity(intent);
counter++;
if (counter == 2) {
counter = 0;
// ad
displayInterstitial();
}
}
});
}
// Invoke displayInterstitial() when you are ready to display an
// interstitial.
public void displayInterstitial() {
// Create ad request.
AdRequest adRequest = new AdRequest.Builder().build();
// Begin loading your interstitial.
interstitial.loadAd(adRequest);
if (interstitial.isLoaded()) {
interstitial.show();
}
}
public void dimImage(){
//item..setAlpha(300); <- This is where I am confused
}
}
我的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;
}
imageView.setImageResource(mThumbIds[position]);
return imageView;
}
// references to our images
public Integer[] mThumbIds = {
R.drawable.image1, R.drawable.image2,
R.drawable.image3, R.drawable.image4,
R.drawable.image5, R.drawable.image6,
R.drawable.image7, R.drawable.image8,
R.drawable.image9, R.drawable.image10,
R.drawable.image11, R.drawable.image12,
R.drawable.image13, R.drawable.image14,
R.drawable.image15, R.drawable.image16,
R.drawable.image17, R.drawable.image18,
R.drawable.image19, R.drawable.image20,
R.drawable.image21, R.drawable.image22
};
答案 0 :(得分:2)
是的,随机是正确的。您可以让适配器跟踪哪些项目已变暗,并检查getView中特定项目的设置:
public class ImageAdapter extends BaseAdapter {
private Context mContext;
private int mSize;
private boolean [] mDim;
public ImageAdapter(Context c, int size) {
mContext = c;
mSize = size;
mDim = new boolean[size];
}
public void dimImage(int position) {
mDim[position] = true;
}
// 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;
}
imageView.setImageResource(mThumbIds[position]);
imageView.setAlpha(mDim[position] ? 0.5f : 1.0f);
return imageView;
}
}
在你的活动中,将适配器保存在一个字段中,用SIZE构造适配器,当你想调暗图像时,调用adapter.dimImage(position),然后调用notifyDataSetChanged:
public class QuizActivity extends Activity {
private ImageAdapter adapter;
private GridView gridview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
adapter = new ImageAdapter(this, SIZE);
gridview = (GridView) findViewById(R.id.gridview);
gridview.setAdapter(adapter);
...
}
public void dimImage(){
adapter.dimImage(location); // not sure if location represents the image index
adapter.notifyDataSetChanged();
}