[英]Why checkbox of android is so slow?
activity_main.xml 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><\/code><\/pre>
MainActivity.java MainActivity.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)); } }<\/code><\/pre>
There is only one Checkbox in the page, this will cost 200ms.页面中只有一个复选框,这将花费 200 毫秒。 If there are 3 or 5 checkbox in activity_main.xml, will cost 1s.如果 activity_main.xml 中有 3 或 5 个复选框,将花费 1s。 What happend?发生了什么事?
I compared Switch to Checkbox.我将 Switch 与 Checkbox 进行了比较。 Obviously Switch is very very better than Checkbox.显然 Switch 比 Checkbox 好得多。 If i want to keep to use Checkbox, what should i do?如果我想继续使用复选框,我该怎么办?
"
This is happening because the Checkbox<\/code> class is very complicated(too much code 😠).
发生这种情况是因为
Checkbox<\/code>类非常复杂(代码太多😠)。
What is the solution ?解决办法是什么 ?<\/h2>
- You can create a custom layout.您可以创建自定义布局。<\/li>
- You can use a library.您可以使用库。<\/li><\/ol>
How to implement ?如何实施?<\/h2>
1.Custom layout 1.自定义布局<\/h3>
- First create a
LinearLayout<\/code> with horizontal<\/code> orientation.
首先创建一个horizontal<\/code>方向的LinearLayout<\/code> 。
<\/li>
Add an ImageView<\/code> and a TextView<\/code> in the xml.
在 xml 中添加一个ImageView<\/code>和一个TextView<\/code> 。
<\/li>
Code reference代码参考<\/li><\/ul>
<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><\/code><\/pre>
- Now the code for it to work.现在让它工作的代码。<\/li><\/ul>
In your class add this在你的课堂上添加这个
boolean isChecked = true<\/code><\/pre> Now it will function like this现在它将像这样运行
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 ); } } );<\/code><\/pre>
2.Using a library 2.使用图书馆<\/h3>
- Use this<\/a> library.使用这个<\/a>库。 <\/li><\/ul>
Sample usage示例使用
<\/blockquote>
setChecked(boolean checked); \/\/by default, it's wthout animation setChecked(boolean checked, boolean animate); \/\/pass true to animate<\/code><\/pre>
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)); } }); }<\/code><\/pre> Attrs属性<\/h3>
Attr属性<\/th>
Type类型<\/th>
Description描述<\/th><\/tr><\/thead>
duration期间<\/td>
integer整数<\/td>
Animation Duration动画时长<\/td><\/tr>
stroke_width行程宽度<\/td>
dimension方面<\/td>
The border width when unchecked未选中时的边框宽度<\/td><\/tr>
color_tick color_tick<\/td>
color颜色<\/td>
Tick color (visible only when checked)勾选颜色(仅在选中时可见)<\/td><\/tr>
color_checked color_checked<\/td>
color颜色<\/td>
Fill color when selected选中时填充颜色<\/td><\/tr>
color_unchecked color_unchecked<\/td>
color颜色<\/td>
Fill color when unchecked未选中时填充颜色<\/td><\/tr>
color_unchecked_stroke color_unchecked_stroke<\/td>
color颜色<\/td>
Border color when unchecked未选中时的边框颜色<\/td><\/tr><\/tbody><\/table>References参考<\/h2>
"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.