0
首先:我們是Android開發新手,我們有一個練習,我們不明白我們需要做什麼。Android OnTouch提供了一個錯誤
我們需要一個OnTouchListener添加到TouchView中,根據課程的PPT這可以在下面所描述的,但它是不工作來完成:
public class MainActivity extends AppCompatActivity{
TouchView touchView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_main);
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
this.setContentView(ll);
float x = 500;
float y = 500;
float size = 1000;
touchView = new TouchView(x,y,size,this);
touchView.setOnTouchListener(this);
ll.addView(touchView);
}
public boolean onTouch(View view, MotionEvent motionEvent){
float x = motionEvent.getX();
float y = motionEvent.getY();
return true;
}
隨着TouchView中類,如下圖所示:
public class TouchView extends View {
public float x,y,size;
Paint paint = new Paint();
public TouchView(float xcor, float ycor, float sizenum, Context context) {
super(context);
x = xcor;
y = ycor;
size = sizenum;
paint.setAntiAlias(true);
}
@Override
protected void onDraw(Canvas canvas){
super.onDraw(canvas);
paint.setColor(Color.BLUE);
canvas.drawCircle(x, y, size, paint);
}
public TouchView(Context context, AttributeSet attributeSet){
super(context, attributeSet);
}
}
不幸的是,它不工作,我們完全喪失瞭如何使它工作。有人可以幫助我們嗎?
Thankyou這確實解決了我們目前的問題,由於某些原因,當我們早些時候嘗試時,這沒有奏效。幸運的是,現在,我必須做一些與已經做出的另一個編輯有關的事情。 – josap
有誰知道爲什麼onTouch會被觸發兩次,而不是一次點擊一次? – josap
因爲首先當你按下視圖時它會調用'ACTION_DOWN',它會調用'ACTION_UP'從視圖中取下你的手指 –