1
我在視圖中使用以下類(Android中的簡單2D圖形)創建了兩個位圖並漫遊以實現該位圖可以獨立移動。我爲此調用了motionevent方法。MotionEvent在視圖上同時移動兩個位圖
目前的問題,我不明白爲什麼只有一個對象在下面的代碼中移動。例如與此代碼,只有「不」位圖移動,我想移動兩個位圖獨立彼此。
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設備與android 2.1操作系統 – 2013-02-20 06:58:38