我差不多完成了我在Eclipse中制作的应用程序。现在可以在操作栏中选择笔划宽度,颜色和清除屏幕。 我遇到的问题是每当我点击操作栏时画布重置并且我丢失了我的画,当我选择不同的颜色或笔画大小时也会出现这种情况。当我启动应用程序时,我必须先单击操作栏才能绘制。 这就是我遇到的问题。
另外,当我选择不同的笔划大小时,黑色笔触大小会正确改变,但是当我改变颜色后,笔划大小会调整为默认值。
以下是我的代码。我希望有人能够帮助我。谢谢!
SingleTouchActivity.java
public class SingleTouchActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
SingleTouchEventView stev = new SingleTouchEventView(this, null);
setContentView(stev);
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.ResetAction) {
invalidate();
return true;
}if (id == R.id.LijnDun) {
stev.setStrokeWidth(4);
return true;
}if (id == R.id.LijnDik) {
stev.setStrokeWidth(5);
return true;
}
if (id == R.id.LijnGroen) {
stev.setColor(3);
return true;
}
if (id == R.id.LijnBlauw) {
stev.setColor(2);
return true;
}
if (id == R.id.LijnRood) {
stev.setColor(1);
return true;
}
return super.onOptionsItemSelected(item);
}
private void invalidate() {
// TODO Auto-generated method stub
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.main_fragment, container,
false);
return rootView;
}
}
}
SingleTouchEventView.Java
public class SingleTouchEventView extends View {
private Paint paint = new Paint();
private Path path = new Path();
public SingleTouchEventView(Context context, AttributeSet attrs) {
super(context, attrs);
paint.setAntiAlias(true);
paint.setStrokeWidth(5f);
paint.setColor(Color.BLACK);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeJoin(Paint.Join.ROUND);
}
@Override
protected void onDraw(Canvas canvas) {
canvas.drawPath(path, paint);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
float eventX = event.getX();
float eventY = event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
path.moveTo(eventX, eventY);
return true;
case MotionEvent.ACTION_MOVE:
path.lineTo(eventX, eventY);
break;
case MotionEvent.ACTION_UP:
// nothing to do
break;
default:
return false;
}
// Schedules a repaint.
invalidate();
return true;
}
// KLEUR switch statement
void setColor(int i){
switch (i){
case 1:
paint.setColor(Color.RED);
break;
case 2:
paint.setColor(Color.BLUE);
break;
case 3:
paint.setColor(Color.GREEN);
break;
default:
paint.setColor(Color.BLACK);
break;
}
}
// LIJN DIKTE switch statement
void setStrokeWidth(int k){
switch (k){
case 5:
paint.setStrokeWidth(12f);
break;
case 4:
paint.setStrokeWidth(5f);
break;
default:
paint.setStrokeWidth(5f);
break;
}
}
}
答案 0 :(得分:0)
在onOptionsItemSelected中,您每次都要创建一个新的View实例。这会将所有值重置为默认值,然后执行选项按钮操作。您必须对视图的现有实例执行更改。使你的视图单身。