osc*_*car 2 android gridview image drawable sharedpreferences
我刚刚问了一个小时前的一个问题,在等待回复时,我想也许我可以达到我想要的不同.我正在考虑改变图像但是如果我可以在gridview中覆盖整个层次顶部的东西会更好,即一个小刻度图标此刻,当一个级别完成时我将使用共享偏好存储它
所以我有一个gridView布局来显示代表关卡的图像.我们只是说这个例子我有20个级别.当一个级别完成时,可以覆盖刻度图标或以某种方式突出显示级别图像.也许改变图像的边界??
这是我使用的图像数组
int[] imageIDs = {
R.drawable.one,
R.drawable.two,
R.drawable.three,
R.drawable.four,
R.drawable.five,
R.drawable.six
etc.......
Run Code Online (Sandbox Code Playgroud)
然后我有我的代码在gridView中设置图像.显然,介于两者之间的代码更多.
public View getView(int position, View convertView, ViewGroup parent)
{
ImageView imageView;
if (convertView == null) {
imageView = new ImageView(context);
imageView.setLayoutParams(new GridView.LayoutParams(140, 140));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(5, 5, 5, 5);
} else {
imageView = (ImageView) convertView;
}
imageView.setImageResource(imageIDs[position]);
return imageView;
Run Code Online (Sandbox Code Playgroud)
是否有可能做到上述任何一种,即使边境方法也没问题.谢谢你的帮助
当关卡打开时,我使用开关盒来执行其他任务,以及将项字符串更改为它所代表的图像的名称.
switch(question){
case 0:
iv.setImageResource(R.drawable.one);
item = imageOne;
answer1 = "one"
break;
case 1:
iv.setImageResource(R.drawable.two);
item = imageTwo;
answer1 = "two"
break;
default:
break;
}
Run Code Online (Sandbox Code Playgroud)
然后我调用该字符串来保存一个名为image的布尔值
final boolean answerStatusCase = preferences.getBoolean(item, false);
final SharedPreferences.Editor editor = preferences.edit();
Button checkAnswer = (Button) findViewById(R.id.bCheckAnswer);
checkAnswer.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String yourAnswerCheck = yourAnswerReview.toString();
if (yourAnswerCheck.equals(answer1)) {
Toast.makeText(getBaseContext(), "That's correct", 2000).show();
editor.putBoolean(item, true);
editor.commit();
}else{
Toast.makeText(getBaseContext(), "That's incorrect", 2000).show();
}
}
});
Run Code Online (Sandbox Code Playgroud)
是的,绝对有可能.我想你可以简单地子类化ImageView并处理onDraw(Canvas)方法中的绘图.例如(这只是一个快速,实际上我可能会处理不同大小的绘制和不同画布尺寸的固定偏移):
public class MarkableImageView extends ImageView {
private boolean checked = true;
public MarkableImageView(Context context) {
super(context);
}
public MarkableImageView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MarkableImageView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public void setChecked(boolean checked) {
this.checked = checked;
invalidate();
}
public boolean isChecked() {
return checked;
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if(checked) {
Bitmap check = BitmapFactory.decodeResource(
getResources(), R.drawable.check);
int width = check.getWidth();
int height = check.getHeight();
int margin = 15;
int x = canvas.getWidth() - width - margin;
int y = canvas.getHeight() - height - margin;
canvas.drawBitmap(check, x, y, new Paint());
}
}
}
Run Code Online (Sandbox Code Playgroud)

编辑:然后你的代码将是这样的:
public View getView(int position, View convertView, ViewGroup parent)
{
MarkableImageView imageView;
if (convertView == null) {
imageView = new MarkableImageView(context);
imageView.setLayoutParams(new GridView.LayoutParams(140, 140));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(5, 5, 5, 5);
} else {
imageView = (MarkableImageView) convertView;
}
imageView.setImageResource(imageIDs[position]);
imageView.setChecked(shouldBeChecked);
return imageView;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4257 次 |
| 最近记录: |