gpt4 book ai didi

android - 定期画线

转载 作者:太空宇宙 更新时间:2023-11-03 11:53:02 25 4
gpt4 key购买 nike

我的情况是我想在一段时间内一条一条地画线。我试图通过使用 thread 来做到这一点。但它对我不起作用。目标是我有 5 行。这些线应该一个接一个地绘制,延迟 5 秒。在 onDraw() 方法中使用 Thread.sleep(5000) 但所有的线都是在 5 秒后绘制的,那些不是定期绘制的……我如何定期绘制线……

代码 fragment::

  public class PaintDemoActivity extends Activity {
DragView drawView;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
requestWindowFeature(Window.FEATURE_NO_TITLE);

drawView = new DragView(this);
setContentView(drawView);
drawView.requestFocus();
}

拖动 View 类::

     public class DragView extends View implements OnTouchListener {
Paint paint = new Paint();
public DragView(Context context) {
super(context);
setFocusable(true);
setFocusableInTouchMode(true);

this.setOnTouchListener(this);

paint.setColor(Color.GREEN);
paint.setStyle(Paint.Style.FILL);
//paint.setStyle(Style.STROKE);
paint.setAntiAlias(true);
}
@Override
public void onDraw(final Canvas mCanvas) {
canvas.drawCircle(p.x, p.y, 5, paint);
canvas.drawLine(60, 60, 120,60, paint);
canvas.drawLine(60, 60, 60, 120, paint);
canvas.drawLine(60, 120, 120, 120, paint);
canvas.drawLine(120, 120, 120, 180, paint);
canvas.drawLine(120, 180, 60, 180, paint);
}
}

谢谢。

最佳答案

另一种更通用的方式:

public class LinesActivity extends Activity {
DemoView demoview;
int mCount = 0;
int mListSize = 0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Paint paint = new Paint();
paint.setColor(Color.RED);

List<Coords> coordList = new ArrayList<Coords>();
// Load up the coordinates
coordList.add(new Coords(60, 60, 120, 60));
coordList.add(new Coords(60, 60, 60, 120));
coordList.add(new Coords(60, 120, 120, 120));
coordList.add(new Coords(120, 120, 120, 180));
coordList.add(new Coords(120, 180, 60, 180));
mListSize = coordList.size();
demoview = new DemoView(this, paint, coordList);
setContentView(demoview);
Timer timer = new Timer();

timer.schedule(new TimerTask() {
public void run() {
mCount++;
demoview.postInvalidate();
Log.d("LINES", "Timer triggered");
if (mCount >= mListSize) {
Log.d("LINES", "All done, cancelling timer");
cancel();
}
}
}, 5000, 5000);

}

private class Coords {
// little class to hold the coordinates
float mSx; float mSy; float mEx; float mEy;

public Coords(float sx, float sy, float ex, float ey) {
mSx = sx; mSy = sy; // start/end x/y
mEx = ex; mEy = ey;
}
}

private class DemoView extends View {

Paint mPaint;
List<Coords> mcList;

public DemoView(Context context, Paint paint, List<Coords> cList) {
super(context);
mPaint = paint;
mcList = cList;
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
int count = 0;
Log.d("LINES", "mcount" + mCount);
for (Coords c : mcList) { // draw all the lines
if (count >= mCount)
break; // up to the number in mCount
canvas.drawLine(c.mSx, c.mSy, c.mEx, c.mEy, mPaint);
count++;
}

}
}
}

关于android - 定期画线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7130888/

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