[英]Performing path animation in custom view
So I have a custom view where I am trying to perform a drawing animation using the Path methods of moveto() and lineto(). 因此,我有一个自定义视图,尝试使用moveto()和lineto()的Path方法执行绘图动画。 I have an array of Points that I iterate through and call invalidate() at every iteration.
我有一组要迭代的点,并在每次迭代时都调用invalidate()。 In onDraw the path is drawn with drawPath().
在onDraw中,路径是使用drawPath()绘制的。 Of course onDraw is not called until the loop is done.
当然,直到循环完成才调用onDraw。 I believe I need to perform the iterations in a background thread.
我相信我需要在后台线程中执行迭代。 But at the same time I have read that it is bad practice perform UI changes in background thread.
但是同时,我读到在后台线程中执行UI更改是一种不好的做法。 How can I can I redraw in every iteration?
如何在每次迭代中重绘?
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawPaint(mBackgroundPaint);
canvas.drawPath(mPath, mStrokePaint);
}
public void drawPoints(){
mPath = new Path();
for (Stroke stroke : mStrokes){
mDuration = stroke.getStrokeDuration();
mInitialX = stroke.getInitialX();
mInitialY = stroke.getInitialY();
mPath.moveTo(mInitialX, mInitialY);
invalidate();
mPoints = new ArrayList<Point>();
mPoints = stroke.getStrokePoints();
int s = mPoints.size();
long delayTime = mDuration / (long) s;
for(Point point : mPoints){
mX = point.getX();
mY = point.getY();
handler.postDelayed(new Runnable() {
@Override
public void run() {
mPath.lineTo(mX, mY);
// want to call invalidate to redraw canvas
invalidate();
}
}, delayTime);
}
}
}
I had the same prob, put this into your onDraw: 我有同样的概率,将其放入您的onDraw中:
((Main) context).runOnUiThread(new Runnable() {
@Override
public void run() {
CustomView.this.invalidate();
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.