我想在视图上设置一条线。例如:从(0,120)开始并且进展到(100,120)的渐进线。我已经用普通的canvas.drawLine()尝试了这个但是在绘制线条时无法应用动画。 我想看到的是一旦视图被加载了一组在视图上绘制的直线(不同长度)。我不希望它们像静态线一样。我想在画布上画出这些线条。
我尝试了其他一些选项,比如使用形状drawables并应用anim / xmls中的动画。他们中的大多数都没有解决我的问题。广泛的谷歌搜索显示直接应用于视图的动画。在这里,我不想创建多个图像视图,因为我最终在视图上有6条线,我需要使用不同的颜色为每条线设置动画。
将来我应该能够上下移动线条来改变颜色,并在每一条线上做其他类型的动画。
记住上述限制,你可以建议我如何编写相同的自定义实现。
答案 0 :(得分:0)
你是不是只使用canvas.drawline(...)或canvas.drawlines(...),你的View可以有代表行的变量,也许是Points或者某种东西的arraylist。我正在尝试在我正在制作的游戏中绘制一条移动值的线。我只是简单地改变放入drawline函数的值,并且随着这些值的改变,我的线条会动画化。然而,只要线条的斜率接近1,它就会接缝,我的循环减速,而我的其他动画也随之减慢。动画线条时是否遇到过类似的问题?或者你只是无法将它们画出来?
答案 1 :(得分:0)
听起来你的问题出现在你的游戏循环中...我使用一个线程来循环我的绘制和更新方法,因为我从来没有能够使invalidate()的东西工作......
所以下面是一个名为mainthread的类,它是在你的surfaceview中创建的,在这种情况下,surfaceview被称为DrawView ...
import android.graphics.Canvas;
import android.view.SurfaceHolder;
public class MainThread extends Thread {
// desired fps
private final static int MAX_FPS = 75;
// maximum number of frames to be skipped
private final static int MAX_FRAME_SKIPS = 0;
// the frame period
private final static int FRAME_PERIOD = 1000 / MAX_FPS;
// Surface holder that can access the physical surface
private SurfaceHolder surfaceHolder;
// The actual view that handles inputs
// and draws to the surface
private DrawView gamePanel;
// flag to hold game state
public boolean running;
public void setRunning(boolean running) {
this.running = running;
}
public MainThread(SurfaceHolder surfaceHolder, DrawView gamePanel) {
super();
this.surfaceHolder = surfaceHolder;
this.gamePanel = gamePanel;
}
@Override
public void run() {
Canvas canvas;
long beginTime; // the time when the cycle begun
long timeDiff; // the time it took for the cycle to execute
int sleepTime; // ms to sleep (<0 if we're behind)
int framesSkipped; // number of frames being skipped
sleepTime = 0;
while (running) {
canvas = null;
// try locking the canvas for exclusive pixel editing
// in the surface
try {
canvas = this.surfaceHolder.lockCanvas();
synchronized (surfaceHolder) {
beginTime = System.currentTimeMillis();
framesSkipped = 0; // resetting the frames skipped
// update game state
this.gamePanel.doUpdate();
// render state to the screen
// draws the canvas on the panel
this.gamePanel.doDraw(canvas);
// calculate how long did the cycle take
timeDiff = System.currentTimeMillis() - beginTime;
// calculate sleep time
sleepTime = (int)(FRAME_PERIOD - timeDiff);
if (sleepTime > 0) {
// if sleepTime > 0 we're OK
try {
// send the thread to sleep for a short period
// very useful for battery saving
Thread.sleep(sleepTime);
} catch (InterruptedException e) {}
}
while (sleepTime < 0 && framesSkipped < MAX_FRAME_SKIPS) {
// we need to catch up
this.gamePanel.doUpdate(); // update without rendering
sleepTime += FRAME_PERIOD; // add frame period to check if in next frame
framesSkipped++;
}
}
} catch(ClassCastException cce) {
}finally {
// in case of an exception the surface is not left in
// an inconsistent state
if (canvas != null) {
surfaceHolder.unlockCanvasAndPost(canvas);
}
} // end finally
}
}
}
使用这个线程它会调用你的更新并且approprietly绘制方法,因为onTouch只是覆盖了surfaceview(DrawView)的onTouch方法来正常访问它。为了阻止线程出于任何理由将运行设置为false。