gpt4 book ai didi

java - 为什么android的checkbox这么慢?

转载 作者:行者123 更新时间:2023-12-04 23:55:58 24 4
gpt4 key购买 nike

今天用的是安卓机,性能很差。我发现复选框加载速度很慢。例如:

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

</LinearLayout>

主 Activity .java

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
long t1 = System.currentTimeMillis();
setContentView(R.layout.activity_main);
System.out.println("main cost: " + (System.currentTimeMillis() - t1));
}
}

页面中只有一个Checkbox,耗时200ms。如果 activity_main.xml 中有 3 或 5 个复选框,则花费 1 秒。发生了什么事?

我将 Switch 与 Checkbox 进行了比较。显然 Switch 比 Checkbox 好很多。如果我想继续使用 Checkbox,我该怎么办?

最佳答案

为什么会这样?


发生这种情况是因为 Checkbox 类非常复杂(代码太多 😠)。

解决方案是什么?


  1. 您可以创建自定义布局。
  2. 您可以使用图书馆。

如何实现?


1.自定义布局

  • 首先创建一个具有水平方向的LinearLayout
  • 在 xml 中添加一个 ImageView 和一个 TextView
  • 代码引用
<LinearLayout
android:id="@+id/checkbox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="7dp">

<ImageView
android:id="@+id/checkImageView"
android:layout_width="30dp"
android:layout_height="30dp"
android:src="@drawable/checked"/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This is the text for checkbox."
android:layout_gravity="center_vertical"
android:layout_marginStart="15dp"/>

</LinearLayout>
  • 现在是让它工作的代码。

在你的类里面添加这个

boolean isChecked = true

现在它的功能是这样的

LinearLayout checkBox = findViewById( R.id.checkbox );
ImageView checkBoxImage = findViewById( R.id.checkImageView );
checkBox.setOnClickListener( v -> {
final Bitmap bmap = ((BitmapDrawable)checkBoxImage.getDrawable()).getBitmap();
Drawable myDrawable = getResources().getDrawable(R.drawable.checked);
final Bitmap myLogo = ((BitmapDrawable) myDrawable).getBitmap();
if (bmap.sameAs(myLogo)){
//it is checked
isChecked = false;
checkBoxImage.setImageResource( R.drawable.unchecked );
}else {
isChecked = true;
checkBoxImage.setImageResource( R.drawable.checked );
}
} );

2.使用库

  • 使用this图书馆。

示例用法

setChecked(boolean checked); //by default, it's wthout animation
setChecked(boolean checked, boolean animate); //pass true to animate
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sample);

final CustomCheckBox scb = (CustomCheckBox) findViewById(R.id.scb);
scb.setOnCheckedChangeListener(new CustomCheckBox.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CustomCheckBox checkBox, boolean isChecked) {
Log.d("CustomCheckBox", String.valueOf(isChecked));
}
});
}

属性

<表类="s-表"><头>属性类型描述<正文>时长整数动画时长描边宽度维度不勾选时的边框宽度颜色标记颜色勾选颜色(勾选时可见)颜色检查颜色选中时填充颜色color_unchecked颜色未选中时填充颜色color_unchecked_stroke颜色未选中时的边框颜色

引用资料


Github

编辑 1 (27/1/22)


由于 Decent Dabbler 收到的评论,我现在想起来它忘记了什么。我其实忘了说加载时间太长的文件是xml文件。这可以证明我们并不总是使用类吧?只是 xml 中的 View 。如果你打开复选框的xml文件,你会发现它太大了。

关于java - 为什么android的checkbox这么慢?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68388846/

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