我一直在为Android制作一个涂料应用程序,能够选择不同的颜色。 我的任务快要完成了。但我的应用程序中存在cetain延迟。在开始时它工作正常但在一些线之后它开始给出直线而不是曲线和使用滞后....
这是我的DrawIt的代码:
package app.paintit;
import java.util.ArrayList;
import java.util.LinkedList;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.view.MotionEvent;
import android.view.View;
public class DrawIt extends View{
float x,y;
Paint paint;
int count1=0,count2=0;
ArrayList<Lines> lines = new ArrayList<Lines>();
public int color= 0xFFFFFF;
Lines l;
Path path;
public DrawIt(Context context) {
super(context);
// TODO Auto-generated constructor stub
x=0;y=0;
paint=new Paint();
path = new Path();
paint.setAntiAlias(true);
paint.setStrokeWidth(2);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeJoin(Paint.Join.ROUND);
setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View arg0, MotionEvent event) {
// TODO Auto-generated method stub
if(event.getAction() == MotionEvent.ACTION_DOWN){
l = new Lines();
l.color = color;
l.xCoordinate = new ArrayList<Float>();
l.xCoordinate.add(event.getX());
l.yCoordinate = new ArrayList<Float>();
l.yCoordinate.add(event.getY());
lines.add(l);
}
else if(event.getAction() == MotionEvent.ACTION_MOVE)
{
l.color = color;
l.xCoordinate.add(event.getX());
l.yCoordinate.add(event.getY());
lines.add(l);
}
invalidate();
return true;
}
});
}
@Override
protected void onDraw(final Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
canvas.drawColor(Color.BLACK);
Lines p,q;
float nowX,nowY,prevX=0,prevY=0;
for(int j=0;j<lines.size();j++){
p = lines.get(j);
Path path1=new Path();
paint.setColor(p.color);
path1.moveTo(p.xCoordinate.get(0), p.yCoordinate.get(0));
for(int i=1;i<p.xCoordinate.size()-1;i++){
float midX=(p.xCoordinate.get(i)+p.xCoordinate.get(i-1))/2;
float midY=(p.yCoordinate.get(i)+p.yCoordinate.get(i-1))/2;
path1.quadTo(p.xCoordinate.get(i-1), p.yCoordinate.get(i-1), midX, midY);
canvas.drawPath(path1, paint);
}
}
}
}
Lines.java的代码包含有助于维护不同颜色的列表:
package app.paintit;
import java.util.ArrayList;
import java.util.LinkedList;
import android.graphics.Color;
public class Lines {
ArrayList<Float> xCoordinate;
ArrayList<Float> yCoordinate;
int color;
}
请帮助我如何减少滞后以及在完美绘制某些形状后发生的直线。