这是代码:
public class MainActivity extends Activity implements OnTouchListener {
RelativeLayout rl;
int i, j = 0;
final int imageArray[] = { R.drawable.w1, R.drawable.w2, R.drawable.w3 };
int image;
final int imageCount = 3;
ImageView back, save, next;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_layout);
final int imageArray[] = { R.drawable.w1, R.drawable.w2, R.drawable.w3 };
image = imageArray[0];
rl = (RelativeLayout) findViewById(R.id.rlBackground);
back = (ImageView) findViewById(R.id.bBack);
save = (ImageView) findViewById(R.id.bSave);
next = (ImageView) findViewById(R.id.bNext);
back.setOnTouchListener(this);
save.setOnTouchListener(this);
next.setOnTouchListener(this);
}
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.bBack:
if (j == 0) {
j = imageCount;
}
image = imageArray[j - 1];
rl.setBackgroundResource(image);
j = j - 1;
break;
case R.id.bSave:
BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inSampleSize = 2;
Bitmap bm = BitmapFactory.decodeResource(getResources(), image,
opts);
// savePicture(bm, "image_name.jpg");
SaveImage savefile = new SaveImage();
savefile.SaveImagee(this, bm);
Toast.makeText(getApplicationContext(),
"Image saved on your gallery!", Toast.LENGTH_LONG).show();
break;
case R.id.bNext:
if (j != imageCount) {
rl.setBackgroundResource(imageArray[j]);
image = imageArray[j];
j = j + 1;
} else {
j = 0;
rl.setBackgroundResource(imageArray[j]);
image = imageArray[j];
j = j + 1;
}
break;
}
return false;
}
}
问题:如果我点击保存按钮,它会在第一次点击时起作用。如果我单击下一步按钮,我需要单击它两次才能触发该功能,但之后,如果我继续单击下一步按钮,则只需单击一次即可。但是当我切换回按钮时,它需要点击两次,然后只点击一次,如果我切换回按钮,同样的情况 - 两次,一次..我想这与焦点有关。
如果我将我的更改ImageView
为ImageButton
,它会触发该功能两次,如果我添加一个 if statemt(event.getAction() == MotionEvent.ACTION_DOWN)
然后我必须再次单击该按钮两次.. 我希望该按钮始终单击一次。我不明白为什么会发生这种情况,因为保存按钮总是一键工作..
编辑:如果我改变
image = imageArray[j];
至
image = imageArray[2];
然后按钮首先单击即可工作,但我还是无法得到它。