简体   繁体   中英

Why did performance of my Android phone app suddenly degrade?

I wrote an Android app with an Activity that creates a heavily-populated scrollable screen image. While it used to take several seconds to display the result, as of two weeks ago, it now takes two minutes. And it is not responsive to scrolling motions. Does anyone have ideas on why the performance of my app would change so abruptly about two weeks ago?

Some detail... Several years ago I wrote an app that collects simple golf statistics. The result after this year is a file with 149 records. I also wrote an app to display those statistics. For one set of stats, I have an Activity that creates a low-tech stacked bar chart as follows.

  1. My layout xml has a horizonally scrollable LinearLayout within which I have another empty horizontal LinearLayout.
  2. In my program I add vertical LinearLayouts to the empty horizontal layout. Each vertical layout represent a stacked bar; there are 3 stacked bars for each record in the file or approximately 450 of them.
  3. Then, to each of the vertical layouts I add small TextViews representing elements of the stacked bar. Two thirds of the vertical layouts have 36 TextViews, and one third have as many as 80 TextViews.
  4. Then I color the TextViews to represent parts of the stacked bar.

So I build one scrollable view with almost 23000 TextViews.

I've used this app for several years. It took several seconds to display the resulting bar graph. Once the result was displayed it scrolled smoothly. That performance is acceptable to me, as my app doesn't need to be doing anything else during this processing.

Beginning about two weeks ago, it now takes some two minutes to display the result, and it only intermittently responds to a scrolling motion. This began when I started making changes in another Activity in the app. I haven't touched the Activity that creates the stacked bar chart, but, of course the whole app gets recompiled.

So I added some logging statements to the Activity, and, based on logcat, it now takes about 1 minute to create the LinearLayouts and TextViews and color all the TextViews. But the screen, remained black for another minute before the chart appears. During this time (ie. after the creation/coloring of bars) and also when I try to scroll the display, the logcat contains occasional messages of the form "Skipped xxxx frames! The application may be doing too much work on its main thread." This doesn't seem related to my code, as it has completed it's work by this time.

I've read about vsync. Is my problem related to that? If so, is vsync relatively new? If that's now the problem, is there a way to circumvent it?

As extra credit, I'm open to suggestions for more efficiently creating a stacked bar graph!

1 use recyclerview

Android recyclerview is the most advanced version of the listview. basically, an android listview is used to present a simple data set. if you want to display large data set in your app, you should use recyclerview.

2 Use constrain layout and avoid nested layout

It is a common misconception that using the basic layout structures leads to the most efficient layouts. However, each widget and layout you add to your application requires initialization, layout, and drawing. For example, using nested instances of LinearLayout can lead to an excessively deep view hierarchy.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM