gpt4 book ai didi

android - 如何用手指画多条线? (安卓)

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:26:51 24 4
gpt4 key购买 nike

我试过像这样画多条线:

`

l1 = new Path();

l2 = new Path();
l3 = new Path();
l4 = new Path();`
---
`mPathList.add(l1...l4);`
---

`public void onDraw(Canvas canvas) {
...

for (Path path : mPathList) {
canvas.drawPath(path, mOverlayPaint);

}
...
}`

---
`case MotionEvent.ACTION_MOVE:
int X = (int)me.getRawX();
int Y = (int)me.getRawY();

l1.moveTo(X, Y);
l2.moveTo(X + 5, Y);
l3.moveTo(X + 10, Y);
l4.moveTo(X + 15, Y);
break;`

但是当我尝试画东西时,FPS 会慢慢降低。任何想法如何使它正常工作?附言对不起,我的英语不好

最佳答案

您需要做的是将行存储在数组列表中,然后在 onDraw() 中读取数组列表。为您的 View 类尝试此代码:

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import java.util.ArrayList;

class Line {
float startX, startY, stopX, stopY,
float joinX, joinY = 0;
public Line(float startX, float startY, float stopX, float stopY) {
this.startX = startX;
this.startY = startY;
this.stopX = stopX;
this.stopY = stopY;
}
public Line(float startX, float startY) { // for convenience
this(startX, startY, startX, startY);
}
}

public class DrawView extends View {
Paint paint = new Paint();
ArrayList<Line> lines = new ArrayList<Line>();

public DrawView(Context context, AttributeSet attrs) {
super(context, attrs);

paint.setAntiAlias(true);
paint.setStrokeWidth(6f);
paint.setColor(Color.BLACK);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeJoin(Paint.Join.ROUND);
}

@Override
protected void onDraw(Canvas canvas) {
for (Line l : lines) {
canvas.drawLine(l.startX, l.startY, l.stopX, l.stopY, paint);
}
}

@Override
public boolean onTouchEvent(MotionEvent event) {

if (event.getAction() == MotionEvent.ACTION_DOWN) {
if (joinX <= 0 || joinY <= 0) lines.add(new Line(event.getX(), event.getY()));
else lines.add(new Line(joinX, joinY);
return true;
}
else if ((event.getAction() == MotionEvent.ACTION_MOVE) &&
lines.size() > 0) {
Line current = lines.get(lines.size() - 1);
current.stopX = event.getX();
current.stopY = event.getY();
Invalidate();
return true;
}
else if ((event.getAction() == MotionEvent.ACTION_UP) &&
lines.size() > 0 {
Line current = lines.get(lines.size() - 1);
current.stopX = event.getX();
current.stopY = event.getY();
joinX = event.getX();
joinY = event.getY();
Invalidate();
return true;
}
else {
return false;
}
}
}

关于android - 如何用手指画多条线? (安卓),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18810229/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com