如何使Canvas Imageview单击下一个活动

时间:2017-07-11 05:19:53

标签: android android-canvas android-bitmap

imageView = (ImageView)findViewById(R.id.img);
imageView.setImageResource(R.drawable.aa);
// crash below into bitmap width and height > 0;
bitmap = Bitmap.createBitmap(imageView.getWidth(),imageView.getHeight(), Bitmap.Config.ARGB_8888);

canvas = new Canvas(bitmap);
// imageView.setImageBitmap(bitmap);
dw = imageView.getWidth();
dh = imageView.getHeight();

Log.e("bitmap", "bitmap" + imageView);
Log.e("canvas", "canvas" + canvas);
imageView.setOnTouchListener(this);
//imageView.setOnClickListener(this);
}

public boolean onTouch(View v, MotionEvent event) {

int action = event.getAction();
int x = (int) event.getX();
int y = (int) event.getY();

switch(action){
case MotionEvent.ACTION_DOWN:
     if (x >= dh && x < (dh + bitmap.getWidth())
     && y >= dw && y < (dw + bitmap.getHeight())) {
     }
break;
}
return true;
    }
}

1 个答案:

答案 0 :(得分:0)

碰撞时用下面的代码替换代码。

imageView.measure(0,0);
bitmap = Bitmap.createBitmap(imageView.getMeasuredWidth(),imageView.getMeasuredHeight(), Bitmap.Config.ARGB_8888);

此外:

dw = imageView.getMeasuredWidth();
dh = imageView.getMeasuredHeight();