android - ArrayList<T> 中更快的项目

标签 android performance arraylist android-canvas

我正在为我的应用编写一个小绘图仪。

该功能需要很多时间,是否有其他方法可以在 Canvas 中更快地绘制?

String auggabe = "x+2";
            float xMitte = step * (count_w/2); //stepp is 100px, count_w is 20
            float yMitte = step * (count_h/2);  //count_h is 10
            ArrayList<Float[]> yWerte = new ArrayList<>();
            for(float i = (count_w/2 * -1);i <= count_w/2; i = getF(i + 0.15f))
            {
                Log.d("FC", Float.toString(i));
                String a =  new math_compile(auggabe.replace("x", String.valueOf(i)), 0).getAufgabe();//is a class they give me the arithmetic problem value
                float y =  Float.parseFloat(a.replace(" ","")) ;
                yWerte.add(new Float[]{i, y*-1});
            }

这段代码是我的问题,需要 133 个项目 在那里画下面

 for(int inde = 0; inde <= yWerte.size()-2; inde++)
            {
                Float[] xy = yWerte.get(inde);
                Float[] xyII = yWerte.get(inde + 1);
                canvas.drawLine((xy[0] * step) + xMitte, (xy[1] * step) + yMitte, (xyII[0] * step) + xMitte, (xyII[1] * step) + yMitte, fc);
            }

四舍五入

private float getF(float y)
{
    DecimalFormat df = new DecimalFormat("#.##");
    String twoDigitNum = df.format(y).replace(",",".");
    Log.d("getF", twoDigitNum);
    return Float.parseFloat(twoDigitNum);
}

最佳答案

  • 不要在平局内进行分配。将它们作为对象初始化的一部分进行,或者在您知道在接收到一些数据时将绘制什么时立即进行。看起来整个第一部分可以移出您的绘图代码并仅在它发生变化时重新计算。

  • 不要为 yWerte 使用列表,您可以使用数组来完成。这将避免 get() 调用。

  • 不要使用 Float,因为 float 可以很好地发挥作用。正如其他人指出的那样,boxing and unboxing也影响了性能。

  • 您甚至可以根据绘制的线条预先计算每个 x,y 坐标的位置,并避免在 draw 方法本身中进行额外的算术运算。这意味着基本上将所有内容都移到绘图之外,并使用线条的 x、y 坐标预先计算一个 float[][] 数组。

关于android - ArrayList<T> 中更快的项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32269163/

相关文章:

javascript - JQuery ':contains' 将其转换为 Javascript

java - PriorityQueue<E> 类的 "public boolean add(E e)"的时间复杂度是多少?

java - 填充数组列表的随机索引出现 NullPointerException?

java - 如何在Java中拆分字符串并将其存储在不同的列表中

java - 遍历 List 时出现 ConcurrentModificationException,但不修改它

android - 我需要使用 Android 支持库吗

c++ - 将循环转换为数学方程

android - 单击放大 ImageView

java - 如何在较新版本的 Android 3.1 及更高版本中实现 Firebase Recycler Adapter?

java - Android:SQLite cursor.getPosition() 返回字符串?