动画在用帆布/ OnDraw中指定的速率 [英] Animation at a specified rate using canvas / Ondraw

查看:195
本文介绍了动画在用帆布/ OnDraw中指定的速率的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Andr​​oid应用程序,我试图表现出的信件逐一与每间短暂的延迟,同时还打了每个字母声音。我所拥有的一切工作,和声音与正确发挥延迟,但文本始终打印在屏幕上,过猛。画布似乎,即使我没有专门无效视图进行更新。

In my Android app, I am trying to show letters one by one with a short delay between each, while also playing a sound for each letter. I have everything working, and the sounds play with the correct delay, but the text always prints to the screen far too fast. The canvas seems to be updated even when i am not specifically invalidating the view.

下面是我迄今为止 - !我也试过这个基础上,蛇例如一个变体,并有相同的结果...任何帮助,将AP preciated

Here is what I have so far - I also tried a variant of this based on the "snake" example and had the same results... any help would be appreciated!

public class SpellingView extends View {
private static final String WORD = "TRUCK";
int width;
int height;
String textToPrint;
float textspace;
int j=0;

private final Path arc;
private final Paint tPaint;

//constructor for SpellingView
public SpellingView(Context context) {
    super(context);
    arc = new Path();
    tPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    displayLetterLoop();
}

public void displayLetterLoop(){

    for (int i = 0; i < WORD.length(); i++){
        final Runnable mUpdateUITimerTask = new Runnable() {
            public void run() { 
                Spelling.mp.start();    
            }
        };
        final Handler mHandler = new Handler();
        mHandler.postDelayed(mUpdateUITimerTask, i*1500);
    }
}

@Override
protected void onDraw(Canvas canvas) {
    int k;
    // Drawing commands go here
    width = canvas.getWidth();
    height = canvas.getHeight();

    arc.addArc(new RectF((width*.15f), (height*.15f), (width*.85f), (height*.4f)), 180,180);

    tPaint.setStyle(Paint.Style.FILL_AND_STROKE);
    tPaint.setColor(Color.RED);
    tPaint.setTextSize(height * 0.1f);
    tPaint.setTextAlign(Paint.Align.LEFT);

    setBackgroundColor(Color.BLACK);

    for (k = 0; k < j; k++){
        char c = WORD.charAt(k);        
        String cs = Character.toString(c);
        textToPrint+= cs;
        textspace =(float) (k*(width/WORD.length())*.9);

        canvas.drawTextOnPath(cs, arc, textspace , 0, tPaint);
    }
    if(j<WORD.length()){
        j++;
    }
}

}

推荐答案

当由于某种原因重绘自身布局的一部分,自定义视图将自行失效。因此,你可以在包裹你的onDraw code()具有条件和标志,以便它吸引你的东西,只有当定时器设置标志,并呼吁废除。一个字母绘制在此之后该标志768,16假像​​进行设置:

Custom view will invalidate itself when is a part of a layout which for some reason redraw itself. Therefore you could envelop your code in onDraw() with a condition and a flag so that it draws your stuff only when the timer sets the flag and calls invalidate. After one letter is drawn then the flag shoud be set on false like:

if (drawLetter){
  drawLetter = false;

  /code...

}

然而,这也可能需要一个sychronized块

However this also may need to be a sychronized block.

这篇关于动画在用帆布/ OnDraw中指定的速率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆