다음 클래스 (안드로이드의 Simple 2D Graphics)를 사용하여 내보기에서 두 비트 맵을 만들고 비트 맵을 독립적으로 이동할 수 있도록 방황했습니다. 나는 이것을 위해 motionevent 메서드를 호출하고있다.MotionEvent가있는 뷰에서 동시 2 비트 맵 이동
현재 문제는 왜 하나의 객체가 다음 코드에서 올바르게 움직이는 지 이해할 수 없습니다. 예 : 이 코드와 함께, "안"비트 맵 이동, 나는 서로 독립적으로 두 비트 맵을 이동하고 싶습니다.
scenrio : 각 객체마다 하나씩 내 두 손가락을 사용하여 비트 맵을 독립적으로 이동할 수 있습니다. 그러나 나는 이것을 달성하는 방법을 모른다.
public class TouchView extends View {
private Drawable cross;
private Rect crossBounds = null;
private Drawable not;
private Rect notBounds = null;
private int x1, y1, x2, y2 ;
boolean flag = true;
private void intialize()
{
int w1 = cross.getIntrinsicWidth();
int h1 = cross.getIntrinsicHeight();
x1 = 100;
y1 = 100;
crossBounds = new Rect(x1-w1/2, y1-w1/2, x1+w1/2, y1+h1/2);
int w = not.getIntrinsicWidth();
int h = not.getIntrinsicHeight();
x2 = 300;
y2 = 300;
notBounds = new Rect(x2-w/2, y2-w/2, x2+w/2, y2+h/2);
}
public TouchView(Context context) {
super(context);
// Get a representation of the image
Resources resources = context.getResources();
cross = (Drawable) resources.getDrawable(R.drawable.cross);
not = (Drawable) resources.getDrawable(R.drawable.not);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
Log.i("TouchView.onTouchEvent", "event = " + event);
if(event.getAction() == MotionEvent.ACTION_DOWN ||
event.getAction() == MotionEvent.ACTION_MOVE) {
int touchCounter = event.getPointerCount();
if (touchCounter ==2 && getHeight()==y1){
int w = cross.getIntrinsicWidth();
int h = cross.getIntrinsicHeight();
x1 = (int) event.getX();
crossBounds = new Rect(x1-w/2, y1-w/2, x1+w/2, y1+h/2);
}
else
{
int w1 = not.getIntrinsicWidth();
int h1 = not.getIntrinsicHeight();
x2 = (int) event.getX();
notBounds = new Rect(x2-w1/2, y2-w1/2, x2+w1/2, y2+h1/2);
}
// Request the system to redraw the view (call onDraw at
// some point in the future)
// From a non-UI thread, call postInvalidate instead
invalidate();
return true;
}
return false;
}
@Override
protected void onDraw(Canvas canvas) {
Log.i("TouchView.onDraw", "");
// Background
Paint bgPaint = new Paint();
bgPaint.setColor(Color.WHITE);
canvas.drawPaint(bgPaint);
if (flag == true){
intialize();
cross.setBounds(crossBounds);
cross.draw(canvas);
not.setBounds(notBounds);
not.draw(canvas);
flag=false;
}
if(crossBounds != null) {
cross.setBounds(crossBounds);
cross.draw(canvas);
not.setBounds(notBounds);
not.draw(canvas);
}
}
}
나는 android 2.1 os가있는 안드로이드 장치가 있습니다. –