私はAndroidの初心者で、タッチ座標を検出して円とタッチのパスを描画する簡単なプログラムを作成したいと思います。タッチの周りに円を描画してそれに従う簡単なプログラムを作成しますが、それでもパスを実行できます。タッチの。パスを指定してプログラムを起動するとクラッシュし、パスを削除すると正常に動作します...
public class MainActivity extends Activity {
float x = 0;
float y = 0;
LinearLayout layout; //declarea variabilor pentru desenarea cercului
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); //specificarea
layout = (LinearLayout)findViewById(R.id.layout); //gasirea id
layout.addView(new CustomView(MainActivity.this));
}
public class CustomView extends View { //crearea unei mape pentru canvas
Bitmap mBitmap;
Paint paint;
Path path;
public CustomView(Context context) {
super(context);
mBitmap = Bitmap.createBitmap(640, 1024, Bitmap.Config.ARGB_8888);
paint = new Paint();
path = new Path();
paint.setColor(Color.BLUE);//culoare cercului desenat
paint.setStyle(Style.FILL);
}
protected void onDraw(Canvas canvas) {//desenarea cercului la atingere
super.onDraw(canvas);
canvas.drawPath(path,paint);
canvas.drawCircle(x, y, 25, paint);
}
public boolean onTouchEvent(MotionEvent event) {
int action = event.getAction();
switch (action){
case MotionEvent.ACTION_DOWN:
path.moveTo(event.getX(), event.getY());
path.lineTo(event.getX(), event.getY());
break;
case MotionEvent.ACTION_MOVE:
x = event.getX();
y = event.getY();
path.lineTo(x, y);
invalidate();
break;
case MotionEvent.ACTION_UP:
path.lineTo(event.getX(), event.getY());
break;
case MotionEvent.ACTION_CANCEL:
break;
default:
break;
}
return true;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}