BKJIA Editor's note: This article is an article by a developer who has participated in the Google IO conference in to adapt a PPT with a good topic on mobile apps, it is very helpful for mobile app developers. In fact, BKJIA also has an official tutorial on "Android UI" design. If you are interested, you can also take a look.
Android UI optimization can begin with the following five aspects:
◆ Adapter Optimization
◆ Background and image Optimization
◆ Drawing Optimization
◆ View and Layout Optimization
◆ Memory allocation optimization
Adapter Optimization
What is Adapter?
Adapter plays an important role in Android. It is an important bond between data and UIView. Adapter is required in common View (ListView, GridView) and other places. 1 intuitively expresses the relationship among Data, Adapter, and View.
Figure 1 Relationship Among Adapter, data, and UI
I. Adapter in Android
Figure 2: Adapter type hierarchy in Android
In Figure 2, we can see the complete hierarchy of all interfaces and classes related to Adapter in Android. During use, we can implement certain extensions of interfaces or inheritance classes according to our own needs. Commonly used include BaseAdapter, ArrayAdapter, and SimpleCursorAdapter.
BaseAdapter is an abstract class that inherits from it and requires many methods, so it has high flexibility;
ArrayAdapter supports generic operations. Generally, You need to implement the getView method and combine the data row id in special cases). In order to make it easier to handle ui events, it is best to rewrite getItemId;
SimpleCursorAdapter can be used for simple text-only ListView. It requires the Cursor field to correspond to the UI id. You can rewrite other methods to implement a more complex UI.
2. A code segment that inherits the BaseAdapter class
- 1 :/**
- 2: * song list Adapter
- 3 :*
- 4: * @ version 05:13:33 pm
- 5: * @ author Hal
- 6 :*/
- 7: public class AudioListAdapter extends BaseAdapter {
- 8:
- 9: private Context mContext;
- 10:
- 11: // song set
- 12: private ArrayList <Audio> mAudios;
- 13:
- 14: public AudioListAdapter (Context mContext, ArrayList <Audio> mAudios ){
- 15: this. mContext = mContext;
- 16: this. mAudios = mAudios;
- 17 :}
- 18:
- 19: @ Override
- 20: public int getCount (){
- 21: return mAudios! = Null? MAudios. size (): 0;
- 22 :}
- 23:
- 24: @ Override
- 25: public Object getItem (int position ){
- 26: if (mAudios! = Null & mAudios. size ()> 0) & (position> = 0 & position <mAudios. size ())){
- 27: return mAudios. get (position );
- 28 :}
- 29: return null;
- 30 :}
- 31:
- 32 :/**
- 33: * if the object data in the set comes from the database, we recommend that you use this method to return the ID of the object in the database.
- 34 :*/
- 35: @ Override
- 36: public long getItemId (int position ){
- 37: if (mAudios! = Null & mAudios. size ()> 0) & (position> = 0 & position <mAudios. size ())){
- 38: return mAudios. get (position). getId ();
- 39 :}
- 40: return position;
- 41 :}
- 42:
- 43: @ Override
- 44: public View getView (int position, View convertView, ViewGroup parent ){
- 45: // TODO returned custom View
- 46 :}
The connection between the Adapter and the View mainly relies on the getView method to return the custom view we need. ListView is one of the most commonly used controls in Android apps. Therefore, it is very important to get a good user experience to make ListView run smoothly. Optimizing ListView is to optimize the getView method in the Adapter. The optimization suggestions provided by the Google IO conference in are as follows:
Sample Code for Adapter optimization:
- @Override
- public View getView(int position, View convertView, ViewGroup parent) {
- Log.d("MyAdapter", "Position:" + position + "---"
- + String.valueOf(System.currentTimeMillis()));
- ViewHolder holder;
- if (convertView == null) {
- final LayoutInflater inflater = (LayoutInflater) mContext
- .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
- convertView = inflater.inflate(R.layout.list_item_icon_text, ull);
- holder = new ViewHolder();
- holder.icon = (ImageView) convertView.findViewById(R.id.icon);
- holder.text = (TextView) convertView.findViewById(R.id.text);
- convertView.setTag(holder);
- } else {
- holder = (ViewHolder) convertView.getTag();
- }
- holder.icon.setImageResource(R.drawable.icon);
- holder.text.setText(mData[position]);
- return convertView;
- }
-
- static class ViewHolder {
- ImageView icon;
-
- TextView text;
The above are the optimization suggestions provided at the Google I/O conference. After trying, The ListView is indeed a lot smoother.
- @Override
- public View getView(int position, View convertView, ViewGroup parent) {
- Log.d("MyAdapter", "Position:" + position + "---"
- + String.valueOf(System.currentTimeMillis()));
- final LayoutInflater inflater = (LayoutInflater) mContext
- .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
- View v = inflater.inflate(R.layout.list_item_icon_text, null);
- ((ImageView) v.findViewById(R.id.icon)).setImageResource(R.drawable.icon);
- ((TextView) v.findViewById(R.id.text)).setText(mData[position]);
- return v;
- }
The above is not recommended practice !!
But we still need to doubt it. SO, let's test and compare it.
Test Description:
You can see that the position and current system time are printed by log during getView. We initialize 1000 pieces of data to display on the Adapter to the ListView, scroll to the bottom, and calculate the time interval between position = 0 and position = 999.
Test HOST: HTC Magic
Test Recording: Enable sequencing to keep ListView rolling at the bottom.
Test results:
In both cases, the experience is significantly different during the operation process, and the optimization is much smoother!
1. Test results of optimization suggestions
- 12-05 10:44:46. 039: DEBUG/MyAdapter (13929): Position: 0---1291517086043
- 12-05 10:44:46. 069: DEBUG/MyAdapter (13929): Position: 1---1291517086072
- 12-05 10:44:46. 079: DEBUG/MyAdapter (13929): Position: 2---1291517086085
-
- ......
-
- 12-05 10:45:04. 109: DEBUG/MyAdapter (13929): Position: 997---1291517104112
- 12-05 10:45:04. 129: DEBUG/MyAdapter (13929): Position: 998---1291517213135
- 12-05 10:45:04. 149: DEBUG/MyAdapter (13929): Position: 999---1291517101044
-
- Duration: 17967
-
2. Test results not optimized
- 12-05 10:51:42. 569: DEBUG/MyAdapter (14131): Position: 0---1291517502573
- 12-05 10:51:42. 589: DEBUG/MyAdapter (14131): Position: 1---1291517502590
- 12-05 10:51:42. 609: DEBUG/MyAdapter (14131): Position: 2---1291517502617
-
- ......
-
- 12-05 10:52:07. 079: DEBUG/MyAdapter (14131): Position: 998---1291517527082
- 12-05 10:52:07. 099: DEBUG/MyAdapter (14131): Position: 999---1291517527108
-
- Duration: 24535
-
There is such a gap in the case of 1000 records. Once the data nW + and the ListView Item layout are more complex, the optimization function becomes more prominent!
Phone Club -- BKJIA mobile development offline technology salon activity Date: December 19
Topic: advanced Android Application Development Technology
Location: Floor 18, Block B, Third Pole building, No. 66, beisihuan West Road, Haidian District, Beijing
Lecturer: Wang mingli innovation Workshop) Fan huaiyu Netease click to register