gpt4 book ai didi

android - Android-应用程序可能在其主线程上做了很多工作

转载 作者:行者123 更新时间:2023-12-03 13:09:50 25 4
gpt4 key购买 nike

我知道我的主线程需要做很多事情,但是我正在寻找解决方法。

这是我的应用程序的结构:

当前,我有我的主要 Activity ,它具有6个可单击的 ImageView (用于打开新 Activity ),当其中一个 Activity 被打开时会发生问题。在本 Activity 中,我将使用带有3个选项卡的SlidingTabLayout。

这是我要实现的目标:

我正在用可绘制图形(形状)创建钢琴

例如,这是黑键:

<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<stroke
android:width="1dp"
android:color="#FF000000" />

<solid
android:color="#FF000000"/>
</shape>

然后,在我的布局中将其称为可绘制对象以创建钢琴:
<ImageView
android:layout_width="8dp"
android:layout_height="37dp"
android:layout_marginLeft="40dp"
android:layout_marginStart="10.5dp"
android:src="@drawable/key_black" />

我正在为每个钢琴显示7个Imageviews,为黑键显示10 Imageview,每个标签有11个钢琴。因此,每个标签为187个Imageview。

因此,我知道这对我的CPU来说将是严酷的。我正在寻找一种方法来执行我的主线程?要在后台执行?要做到这一点而不会遇到滞后?

最佳答案

因此,如上所述,我将为钢琴创建一个基本图像,并根据当前按下的键对其进行调整。下面的代码显示了一般的想法:从R.drawable加载基本图像,使用索引的按下键以及图像的大小创建Paths,绘制路径,返回图像。如果您需要澄清,请告诉我。

public Bitmap getPianoImage(Context context, int[] pressedKeyIndices){
Bitmap pianoImage = BitmapFactory.decodeResource(context.getResources(), R.drawable.pianoBase);
Canvas canvas = new Canvas(pianoImage);
Paint paint = new Paint();
paint.setColor(Color.RED);
paint.setStyle(Paint.Style.FILL);

for(int pressedKeyIndex : pressedKeyIndices){
Path path = getPathForKeyIndex(pressedKeyIndex, pianoImage.getWidth(), pianoImage.getHeight());
canvas.drawPath(path, paint);
}

return pianoImage;
}

private Path getPathForKeyIndex(int idx, int w, int h){

Path path = new Path();

switch(idx){
case 0:
path.moveTo(0f*w, 0f*h); // used for first point
path.lineTo(0f*w, 1f*h);
path.lineTo(0.1f*w, 1f*h);
path.lineTo(0.1f*w, 0.5f*h);
path.lineTo(0.08f*w, 0.5f*h);
path.lineTo(0.08f*w, 0f*h);
path.lineTo(0f*w, 0f*h);
break;
case 1: ...
}

return path;
}

关于android - Android-应用程序可能在其主线程上做了很多工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38238498/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com