Android绘制一条动画线
我目前正在处理图形和路径,我可以成功地显示我想要的任何内容。
但我不想直接在 SurfaceView 上绘制一条线,而是想在动画中逐步绘制它。
到目前为止,我所做的是创建一个路径,然后使用 PathMeasure 沿着路径逐步检索坐标。这基本上是我到目前为止所做的事情
PathMeasure pm = new PathMeasure(myPath, false);
float position = 0;
float end = pm.getLength();
float[] coord = {0,0,0,0,0,0,0,0,0};
while (position < end){
Matrix m = new Matrix();
// put the current path position coordinates into the matrix
pm.getMatrix(position, m, PathMeasure.POSITION_MATRIX_FLAG | PathMeasure.TANGENT_MATRIX_FLAG);
// put the matrix data into the coord array (coord[2] = x and coord[5] = y)
m.getValues(coord);
????
position += 1;
}
问号是我陷入困境的地方。我想逐步绘制路径并在屏幕上看到它的动画。我在互联网上找不到太多有关它的信息,所以如果您已经遇到过同样的情况,任何线索将不胜感激。我想要创建的最终效果就像铅笔自动逐渐绘制文本一样。
I'm currently working with graphics and paths, and I can successufully display whatever I want.
But instead of drawing a line directly on my SurfaceView, I'd like to draw it progressively in an animation.
What I've done so far is to create a Path and then to use PathMeasure to retrieve the coordinates progressively along the path. Here is basically what I've done so far
PathMeasure pm = new PathMeasure(myPath, false);
float position = 0;
float end = pm.getLength();
float[] coord = {0,0,0,0,0,0,0,0,0};
while (position < end){
Matrix m = new Matrix();
// put the current path position coordinates into the matrix
pm.getMatrix(position, m, PathMeasure.POSITION_MATRIX_FLAG | PathMeasure.TANGENT_MATRIX_FLAG);
// put the matrix data into the coord array (coord[2] = x and coord[5] = y)
m.getValues(coord);
????
position += 1;
}
The question marks is where I'm stuck. I want to draw the path progressively and see it animated on the screen. I couldn't find much info about it on the internet, so any clue would be much appreciated if you have already come across the same situation. The final effect I want to create is like a pencil drawing progressively a text automatically.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
每次您想要绘制更多的路径时,您可以使用 ObjectAnimator 类回调您的类的方法之一,而不是创建 for 循环。
然后,只需调用 init() 即可开始动画,如下所示(或者如果您希望它在视图膨胀后立即启动,请将 init() 调用放入构造函数中):
另请参阅此问题 此处,以及this 示例,我的代码基于该示例。
Instead of creating a for loop, you can use the ObjectAnimator class to callback to one of your class's methods every time you'd like to draw a bit more of the path.
Then, just call init() to begin the animation, like this (or if you'd like it to start as soon as the view is inflated, put the init() call inside the constructors):
Also see this question here, and this example, which I've based my code on.
我刚刚解决了这个问题,这里是我所做的:
这个例子是对我所做的事情的改编(为了简化示例)。希望你能理解。由于我刚刚使这个功能正常工作,它缺乏优化之类的东西。
希望它会有所帮助;-)
I just have resolve this problem, here what I do:
This example, is an adaptation of what I've done (to simplify the example). Hope you will understand it. Since I've just made this function working, It lacks of optimizations and things like that.
Hope it will help ;-)
这是对我有用的替代解决方案
}
并使用
here is an alternative solution that worked for me
}
and use
您必须将此视图添加到布局中,将高度设置为 1,宽度设置为与父视图匹配。该线将从左到右进行动画处理。后一行将放置在第一行之上。
You will have to add this view to the layout, setting height to 1 and width to match parent. The line will be animated from left to right. The later line will be placed over the first one.