【发布时间】:2015-05-16 11:28:31
【问题描述】:
我对 android 还很陌生。我有这段代码要在我的应用程序中绘制,但我想用一个按钮激活它。如果我单击操作栏按钮,我应该绘制,否则我不应该这样做。我该怎么做?
//DRAW
public class CustomView extends View {
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.YELLOW);
//paint.setStyle(Style.FILL); //if I want to fill but I don't
paint.setStyle(Style.STROKE);
paint.setStrokeWidth(3);
}
protected void onDraw(Canvas canvas) {
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 onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_edit:
Log.i("ActionBar", "Edit");
// FROM HERE I WANT TO ACTIVATE
return true;
case R.id.menu_save:
Log.i("ActionBar", "Guardar!");;
return true;
default:
return super.onOptionsItemSelected(item);
}
编辑:我的应用程序由文本组成。我想让用户在这些文本下划线,要做到这一点,您必须首先启用触摸操作栏按钮。如果您不想再下划线,请再次单击该按钮。
【问题讨论】:
标签: android onclick draw activation