* MyImage类扩展视图和实现ontouchListener *
enter code here
公共类MyImage扩展了View实现的OnTouchListener {
Bitmap orignal;
Bitmap scaled;
Matrix m;
Context ctext;
ArrayList<Rect> rect;
我有四个矩形,所有矩形都用于移动 diff diff Activity Rect rectangle0; Rect rectangle1; Rect rectangle2; 矩形矩形3; 矩形矩形4; private int X_POSITION; private int Y_POSITION;
public MyImage(Context context) {
super(context);
ctext = context;
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// TODO Auto-generated method stub
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
@Override
public View findFocus() {
// TODO Auto-generated method stub
return super.findFocus();
}
@Override
public boolean isPressed() {
// TODO Auto-generated method stub
return super.isPressed();
}
@Override
public boolean isSelected() {
// TODO Auto-generated method stub
return super.isSelected();
}
@Override
public boolean onTouchEvent(MotionEvent event) {
X_POSITION = (int)event.getX();
Y_POSITION = (int)event.getY();
System.out.println(X_POSITION + "\t" +Y_POSITION);
问题出现在这里我不知道为什么所有矩形都会触发相同的活动,以及如何解决这个问题,以便所有矩形能够激发差异
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
Intent i = new Intent(ctext , SelecterImage.class);
ctext.startActivity(i);
}
return true;
}
@Override
public void onDraw(Canvas canvas) {
Paint paint = new Paint();
int X_WIDTH = canvas.getWidth();
int Y_HEIGHT = canvas.getHeight() / 2;
System.out.println(X_WIDTH + "\n" + Y_HEIGHT);
Paint rectanglePaint = new Paint();
rectanglePaint.setARGB(255, 0, 0, 0);
rectanglePaint.setStrokeWidth(2);
rectanglePaint.setColor(Color.BLUE);
rectangle0 = new Rect(10, 420, 60, 380);
rectangle1 = new Rect(70, 420, 120, 380);
rectangle2 = new Rect(130, 420, 180, 380);
rectangle3 = new Rect(190, 420, 240, 380);
rectangle4 = new Rect(250, 420, 300, 380);
canvas.drawRect(rectangle0, rectanglePaint);
canvas.drawRect(rectangle1, rectanglePaint);
canvas.drawRect(rectangle2, rectanglePaint);
canvas.drawRect(rectangle3, rectanglePaint);
canvas.drawRect(rectangle4, rectanglePaint);
scaled = Bitmap.createScaledBitmap(orignal, X_WIDTH, Y_HEIGHT, false);
canvas.drawBitmap(scaled, 0, 0, paint);
}
void setBitmap(Bitmap bitmap) {
// TODO Auto-generated method stub
orignal = bitmap;
}
@Override
public boolean onTouch(View v, MotionEvent event) {
return false;
// TODO Auto-generated method stub
}
}
强文
答案 0 :(得分:0)
首先确保onTouchEvent正常工作(确实像你一样返回X和Y)然后
@Override
public boolean onTouchEvent(MotionEvent event) {
X_POSITION = (int)event.getX();
Y_POSITION = (int)event.getY();
if (event.getAction == MotionEvent.ACTION_DOWN)
{
if (rectangle0.contains(X_POSITION,Y_POSITION))
{
Intent i = new Intent(ctext , Activity1.class);
ctext.startActivity(i);
}
else if (rectangle1.contains(X_POSITION,Y_POSITION))
{
Intent i = new Intent(ctext , Activity2.class);
ctext.startActivity(i);
}
....
}
}