我想实现以下功能:从屏幕左侧开始,沿着水平方向不停画图,画到最右侧以后,清屏。继续从左侧画图,如此循环。
现在遇到了问题,清屏不能清干净。现在程序会有三根线同时存在。前两根是原来画的,第三根是正在画的。我想要的结果是只保存正在画的这根。代码如下:
java代码:
package com.example.testdraw;
import java.util.Timer;
import java.util.TimerTask;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff.Mode;
import android.graphics.PorterDuffXfermode;
import android.os.Bundle;
import android.view.Menu;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
private SurfaceView surfaceview;
private SurfaceHolder surfaceholder;
private Button btnStart;
Timer mTime;
TimerTask mTimerTask;
int oldX=0,oldY=0,newX=0,newY=3;
int flag1=0,flag2=0;//flag1代表水平画0和垂直画1;flag2表示往右下画0和往右上画1
int count=0;
Canvas canvas;
Paint paint;
Bitmap bitmap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
surfaceview=(SurfaceView)findViewById(R.id.surfaceview);
surfaceholder=surfaceview.getHolder();
btnStart=(Button)findViewById(R.id.btnStart);
paint=new Paint();
btnStart.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
mTime=new Timer();
mTimerTask=new TimerTask() {
@Override
public void run() {
// TODO Auto-generated method stub
if(newX>getWindowManager().getDefaultDisplay().getWidth()){
//清屏
ClearDraw();
}
draw(oldX, oldY, newX, newY);
oldX=newX;
oldY=newY;
if(flag1==0){
flag1=1;
if(flag2==0){
newX=oldX+3;
newY=oldY;
}else{
newX=oldX+3;
newY=oldY;
}
}else if(flag1==1){
flag1=0;
if(flag2==0){
newX=oldX;
newY=oldY+3;
}else{
newX=oldX;
newY=oldY-3;
}
}
count++;
if(newY>200){
flag2=1;
}
if(newY<=0){
flag2=0;
}
}
};
mTime.schedule(mTimerTask, 0,1);
}
});
}
public void draw(int oldX,int oldY,int newX,int newY){
canvas = surfaceholder
.lockCanvas();// 关键:获取画布
//bitmap=Bitmap.createBitmap(1000,1000,Config.ARGB_8888 );
//canvas.setBitmap(bitmap);
//Paint mPaint = new Paint();
paint.setAntiAlias(true);
paint.setColor(Color.GREEN);// 画笔为绿色
paint.setStrokeWidth(2);// 设置画笔粗细
canvas.drawLine(oldX, oldY, newX, newY, paint);
//surfaceholder.unlockCanvasAndPost(canvas);
//canvas=surfaceholder.lockCanvas(new Rect(300, 0, 600,getWindowManager().getDefaultDisplay().getHeight()));// 关键:获取画布
//canvas.drawLine(300, 200,500, 400, mPaint);
surfaceholder.unlockCanvasAndPost(canvas);
}
@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;
}
// TODO Auto-generated constructor stub
public void ClearDraw(){
Canvas canvas = surfaceholder.lockCanvas();
//canvas.drawColor(Color.TRANSPARENT);
Paint p = new Paint();
//清屏
p.setXfermode(new PorterDuffXfermode(Mode.CLEAR));
canvas.drawPaint(p);
p.setXfermode(new PorterDuffXfermode(Mode.SRC));
oldX=0;
newX=3;
surfaceholder.unlockCanvasAndPost(canvas);
}
}
配置文件:
<LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android”
android:layout_width=”fill_parent”
android:layout_height=”fill_parent”
android:orientation=”vertical” >
<SurfaceView
android:layout_width=”fill_parent”
android:layout_height=”600px”
android:id=”@+id/surfaceview”/>
<RelativeLayout android:layout_width=”fill_parent”
android:layout_height=”wrap_content”>
<Button
android:layout_marginLeft=”7dip”
android:id=”@+id/btnStart”
android:text=”简单绘图”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:textSize=”20dip”/>
</RelativeLayout>
</LinearLayout>