简体   繁体   English

Android 2d图形帧率

[英]Android 2d Graphics frame rate

I'm in need of assistance with the program that I'm writing. 我需要有关正在编写的程序的帮助。

The code basically list of words from an array, chops it into specific Strings at certain indexes, then into Chars and displaying it into the canvas. 代码基本上是数组中单词的列表,将其切成特定索引处的特定字符串,然后分成Chars并将其显示在画布上。

Then, the chars animates while sliding down the canvas. 然后,字符在滑下画布的同时进行动画处理。

But, whenever it animates slowly, the characters are on top of each other, and whenever it animates fast, the characters are separated. 但是,每当动画缓慢时,角色就会彼此重叠,而每当动画快速时,角色就会分离。

How do I get the chars to be separated equally WHILE animating slowly? 在动画缓慢的情况下,如何使字符均匀分离? One solution I came up with was to find a way to edit the frame rate, but I don't know how to do it :(. 我想到的一个解决方案是找到一种编辑帧速率的方法,但是我不知道该怎么做:(。

 public sketchUp(Context context) {
        super(context);
    }
String arr [] = {"love","happiness"};
    protected void onDraw (Canvas canvas){
super.onDraw(canvas);
        Rect disRect = new Rect();
      disRect.set(0,0, canvas.getWidth(), canvas.getHeight()/2);
        Paint col = new Paint();
        Paint txt = new Paint();
        txt.setTextSize(20);
        txt.setColor(Color.BLACK);
        col.setColor(Color.GREEN);
        col.setStyle(Paint.Style.FILL);
        canvas.drawRect(disRect, col);

        if(y<canvas.getHeight()/2){
            y+=1;
        }
       else{
            y=0;
        }
//       if(x<canvas.getWidth()){
//            x+=10;
//        }
//        else{
//           x=0;
//       }
   for(int i=0; i<arr.length; i++){

    for(int j=0; j<arr[i].length(); j++){
        char result = arr[i].charAt(j);
//        System.out.println(result);
    canvas.drawText(String.valueOf(result), x, y+=0.5, txt);
//

    }
   }

invalidate();

    }

Think of it this way: 这样想:

That y+=0.5 sets the separation of the characters. y+=0.5设置字符的分隔。 It is the only thing that makes each character's position different. 这是使每个角色的位置不同的唯一原因。 If it was just y , the characters would be on top of each other. 如果只是y ,则字符将彼此重叠。

Why it affects animation speed 为什么会影响动画速度

Each y+=0.5 changes y. 每个y+=0.5改变y。 So after drawing the characters y is lower than it was before. 因此,绘制后的字符y比以前要低。

How to fix this 如何解决这个问题

First, you have to decide what y is supposed to be. 首先,您必须确定y应该是什么。 You should rename it, so you remember what it is. 您应该重命名它,以便记住它是什么。 For example it could become firstCharacterY . 例如,它可以成为firstCharacterY

Obviously, the first characters y should not change when drawing the characters, but should change each frame. 显然,在绘制字符时,第一个字符y不应改变,而应改变每一帧。 To fix the drawing part, you can either set y = firstCharacterY - 0.5 before the loop or calculate y from i and j each iteration. 要修复图形零件,可以在循环之前设置y = firstCharacterY - 0.5或在每次迭代时从i和j计算y。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM