老师,向你请教,谢谢!
你讲的Tab主界面对现阶段的我来说很实用。下面是我学习过程中遇到的问题:
我在学习android时,编写一个画图板,先新建了一个画图组件,以下就是代码部分(组件,MainActivity)。然后在在activity-main.xml添加几个ImageButton,然后我想的是在activity程序设置ImageButton的监听,通过单机ImageButton来改变画笔的颜色,在画布上画出不同颜色的图形。但是我遇到一个问题:当点击一个Button改变画笔颜色在画布上绘图时,之前在画布上所绘的图形颜色都变成现在的颜色。很是困扰,求解答。
——————————————————————————————————————————————
画图组件:
public class MyPaintView extends View{
Path path=new Path();
Paint paint=new Paint();
public MyPaintView(Context context, AttributeSet attrs) {
super(context, attrs);
paint.setAntiAlias(true);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeCap(Paint.Cap.ROUND);
paint.setStrokeJoin(Paint.Join.ROUND);
paint.setStrokeWidth(3f);
super.setBackgroundColor(Color.WHITE);
super.setOnTouchListener(new OnTouchListenerImp());//设置触摸事件
}
class OnTouchListenerImp implements OnTouchListener{
public boolean onTouch(View v, MotionEvent event) {
float pointX=event.getX();
float pointY=event.getY();
switch(event.getAction()){
case MotionEvent.ACTION_DOWN:
path.moveTo(pointX, pointY);
return true;
case MotionEvent.ACTION_MOVE:
path.lineTo(pointX, pointY);
break;
default:
return false;
}
postInvalidate();
return false;
}
}
protected void onDraw(Canvas canvas) {
canvas.drawPath(path, paint);
}
}
————————————————————————————————————————————————
MainActivity:
public class EffectoffurnitruesystemActivity extends Activity implements OnClickListener {
private ImageButton img01;
private ImageButton img02;
private ImageButton img03;
private ImageButton img04;
private MyPaintView mp;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.setContentView(R.layout.main);
initEvent();
}
private void initEvent() {
img01=(ImageButton) findViewById(R.id.img1);
img02=(ImageButton) findViewById(R.id.img2);
img03=(ImageButton) findViewById(R.id.img3);
img04=(ImageButton) findViewById(R.id.img4);
mp=(MyPaintView) findViewById(R.id.paintView);
img01.setOnClickListener(this);
img01.setOnClickListener(this);
img01.setOnClickListener(this);
img01.setOnClickListener(this);
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.img1:
mp.paint.setColor(Color.BLUE);
break;
case R.id.img2:
mp.paint.setColor(Color.RED);
break;
case R.id.img3:
mp.paint.setColor(Color.GREEN);
break;
case R.id.img4:
mp.paint.setColor(Color.YELLOW);
break;
default:
break;
}
}
}