我写了一些代码来捕捉图像上用户触摸的坐标。我希望获得10分,之后我想开始一项新的活动。我的源代码如下:
public class Image extends Activity{
// global variable for storing the points
public static int a = 0;
public static int b = 0;
public static float points[][] = new float[10][2];
private static final String TAG = "myApp";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_image);
Bundle bundle = this.getIntent().getExtras();
String fileName = bundle.getString("message");
//showImage(fileName);
String directory = "/sdcard/DCIM/cat/" + fileName;
File imageFile = new File(directory);
Bitmap imageBitmap = BitmapFactory.decodeFile(imageFile.getAbsolutePath());
//rotating the bitmap by 90 degree clockwise. Assume the bitmap is 270 degree clockwise.
Matrix mtx = new Matrix();
mtx.preRotate(90);
int w = imageBitmap.getWidth();
int h = imageBitmap.getHeight();
imageBitmap = Bitmap.createBitmap(imageBitmap,0,0,w,h,mtx,false);
imageBitmap = imageBitmap.copy(Bitmap.Config.ARGB_8888,true);
ImageView imageView = (ImageView)findViewById(R.id.imageView1);
imageView.setImageBitmap(imageBitmap);
imageView.setOnTouchListener(new OnTouchListener(){
@Override
public boolean onTouch(View v, MotionEvent e){
float x = e.getX();
float y = e.getY();
Context context = getApplicationContext();
if (a < 10){
if (a>0 && x != points[a-1][0]){
points[a][0] = x;
points[a][1] = y;
String text = a + " is added, x:" +x + " y:" + y;
Toast toast2 = Toast.makeText(context, text, Toast.LENGTH_SHORT);
toast2.show();
a++;
Log.v(TAG, "in first else");
}
if (a ==0) {
points[a][0] = x;
points[a][1] = y;
String text1 = a + " is added, x:" +x + " y:" + y;
Toast toast3 = Toast.makeText(context, text1, Toast.LENGTH_SHORT);
toast3.show();
a++;
Log.v(TAG, "in if a==0 else");
}
}else {
Toast toast1 = Toast.makeText(context, "array full", Toast.LENGTH_SHORT);
toast1.show();
Log.v(TAG, "in final else 4");
}
Log.v(TAG, "in final else 5");
return true;
}});
}
我希望在捕捉到10分之后开始一项活动。我试图将意图放在onTouch函数的最后一个else语句中,但它每次都会崩溃。我应该把意图放在哪里?感谢。