background image in ListActivity slowness

Put problem concerning Views, Layouts and other XML-Resources (like AndroidManifest) here.

background image in ListActivity slowness

Postby supersk » Tue Dec 08, 2009 4:45 pm

Hi all,

I'm making an application which show book content, in my app there is a ListView which is used to show all books.
I decided to add a background picture for the list, and this is causing a very big slowness in the scrolling.
I tried .BMP,.PNG,.JPEG and that doesnt make a difference, i tried to lower the quality of the background but suprisingly when i make it very small it gets even slower!
I also use custom adapter to make different font but that doesnt matter, when i use default adapter its the same thing:(-slow.

Please help!

here is my code:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. public class BookNavigationActivity extends ListActivity implements OnItemClickListener{
  3.  
  4.  
  5.  
  6.  
  7.  
  8. @Override
  9.  
  10.         public void onCreate(Bundle savedInstanceState) {
  11.  
  12.                 super.onCreate(savedInstanceState);
  13.  
  14.                 setListAdapter(new FontableAdapter(this,
  15.  
  16.                                 R.layout.fontrow, ToraCode.BOOKS));
  17.  
  18.                 getListView().setBackgroundResource(R.drawable.test);
  19.  
  20.                 getListView().setTextFilterEnabled(true);
  21.  
  22.                 getListView().setOnItemClickListener(this);
  23.  
  24.                 getListView().setCacheColorHint(0);
  25.  
  26.         }
  27.  
  28.  
Parsed in 0.031 seconds, using GeSHi 1.0.8.4
supersk
Freshman
Freshman
 
Posts: 6
Joined: Tue Dec 08, 2009 4:34 pm

Top

Postby grzegorz.zajac » Tue Dec 08, 2009 7:57 pm

Hi,

Can you put the implementation of your custom adapter?
Where does the data for the adapter come from? - SQL db, file, network?

Regards,
Grzesiek
grzegorz.zajac
Developer
Developer
 
Posts: 39
Joined: Wed Feb 18, 2009 8:37 pm
Location: Krakow/Poland

Postby supersk » Tue Dec 08, 2009 8:34 pm

Hi,

The custom adapter is complicated, its not the reason for the slowdown.
I take my list values from an array of Strings which is located in the main class.

If your question is because you wish implementing different font inside a list please send me a PM and i help you out.

supersk.
supersk
Freshman
Freshman
 
Posts: 6
Joined: Tue Dec 08, 2009 4:34 pm

Postby grzegorz.zajac » Tue Dec 08, 2009 8:44 pm

I was asking about the implementation details as I just wanted to help you...
The piece of code you provided in your first post says nothing, and for sure it is not the reason of slow scroll.

Regards,
Grzesiek
grzegorz.zajac
Developer
Developer
 
Posts: 39
Joined: Wed Feb 18, 2009 8:37 pm
Location: Krakow/Poland

Postby supersk » Tue Dec 08, 2009 8:56 pm

Oh, okay.
Here is the code:

R.id.fontrow is the layout for the custom row in the listview.
Tora.menufont is the font im adding its of type: true type font(.ttf)

If you have any questions please ask.
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. package hatana.application;
  3.  
  4.  
  5.  
  6. import android.app.Activity;
  7.  
  8. import android.graphics.Typeface;
  9.  
  10. import android.view.LayoutInflater;
  11.  
  12. import android.view.View;
  13.  
  14. import android.view.ViewGroup;
  15.  
  16. import android.widget.ArrayAdapter;
  17.  
  18. import android.widget.TextView;
  19.  
  20.  
  21.  
  22. @SuppressWarnings("unchecked")
  23.  
  24. public class FontableAdapter extends ArrayAdapter{
  25.  
  26.         Activity context;
  27.  
  28.         String listContent[];
  29.  
  30.         LayoutInflater inflater;
  31.  
  32.         Typeface font;
  33.  
  34.  
  35.  
  36.         public FontableAdapter(Activity context, int textViewResourceId,
  37.  
  38.                         Object[] objects) {
  39.  
  40.                 super(context,textViewResourceId,objects);
  41.  
  42.                 listContent=(String[])objects;
  43.  
  44.                 inflater=context.getLayoutInflater();
  45.  
  46.                 font= Typeface.createFromAsset(context.getAssets(), Tora.menuFont);
  47.  
  48.                 this.context=context;
  49.  
  50.         }
  51.  
  52.         public View getView(int position, View convertView,
  53.  
  54.                         ViewGroup parent) {
  55.  
  56.                 TextView holder;
  57.  
  58.         if (convertView == null){
  59.  
  60.             convertView = inflater.inflate(R.layout.fontrow, null);
  61.  
  62.        
  63.  
  64.         holder=(TextView)convertView.findViewById(R.id.label);
  65.  
  66.         convertView.setTag(holder);
  67.  
  68.         }
  69.  
  70.         else{
  71.  
  72.                 holder = (TextView)convertView.getTag();
  73.  
  74.  
  75.  
  76.         }
  77.  
  78.         holder.setText(listContent[position]);
  79.  
  80.         holder.setTypeface(font);
  81.  
  82.  
  83.  
  84.         return convertView;
  85.  
  86. }
  87.  
  88.        
  89.  
  90. }
  91.  
  92.  
Parsed in 0.036 seconds, using GeSHi 1.0.8.4


Thanks,
supersk
supersk
Freshman
Freshman
 
Posts: 6
Joined: Tue Dec 08, 2009 4:34 pm

Postby grzegorz.zajac » Tue Dec 08, 2009 9:38 pm

Hi again,

What happens if you remove a line where the TextView has the font set? Does it work normally?

One suggestion to the provided code - if you are using the same font for all ListView items, why not to set the font once. I mean:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public View getView(int position, View convertView, ViewGroup parent) {
  2.  
  3.         TextView holder;
  4.  
  5.  
  6.  
  7.         if (convertView == null){
  8.  
  9.             convertView = inflater.inflate(R.layout.fontrow, null);
  10.  
  11.        
  12.  
  13.             holder=(TextView)convertView.findViewById(R.id.label);
  14.  
  15.             holder.setTypeface(font);
  16.  
  17.            
  18.  
  19.             convertView.setTag(holder);
  20.  
  21.         }
  22.  
  23.         else {
  24.  
  25.             holder = (TextView)convertView.getTag();
  26.  
  27.         }
  28.  
  29.  
  30.  
  31.         holder.setText(listContent[position]);
  32.  
  33.         //holder.setTypeface(font);
  34.  
  35.  
  36.  
  37.         return convertView;
  38.  
  39. }
Parsed in 0.035 seconds, using GeSHi 1.0.8.4


Regards,
Grzesiek
grzegorz.zajac
Developer
Developer
 
Posts: 39
Joined: Wed Feb 18, 2009 8:37 pm
Location: Krakow/Poland

Top

Postby qlimax » Wed Dec 09, 2009 12:23 am

Hi supersk :D

try to set this attribute to the listview

android:cacheColorHint="#00000000"

it should optimize your listview and remove the black background during the scroll.


bye ;D
¯`·.¸¸.><((((º>¯`·.¸¸. ><((((º>
User avatar
qlimax
Master Developer
Master Developer
 
Posts: 271
Joined: Mon Aug 31, 2009 10:54 am
Location: Swiss

Postby supersk » Thu Dec 10, 2009 1:52 am

Thanks for the advices,
qlimax:
about the: android:cacheColorHint="#00000000", I cant really put it into an XML as i use ListActivity but i do it here:
getListView().setCacheColorHint(0);

Grzesiek: You are right it is a waste to call that for each row, but it not deals a noticeable change in the scroll experience:(, I come to the conclusion that it has something to do with the picture being processed before actually shown on screen, as when i put smaller image or larger image it takes slightly more time than if i take image that is exactly with the same size as my resolution. I also tried regular layout, with activity instead of listactivity, same effect.
supersk
Freshman
Freshman
 
Posts: 6
Joined: Tue Dec 08, 2009 4:34 pm

Postby qlimax » Thu Dec 10, 2009 2:14 am

supersk wrote:Grzesiek: You are right it is a waste to call that for each row, but it not deals a noticeable change in the scroll experience:(, I come to the conclusion that it has something to do with the picture being processed before actually shown on screen, as when i put smaller image or larger image it takes slightly more time than if i take image that is exactly with the same size as my resolution. I also tried regular layout, with activity instead of listactivity, same effect.


yes, that can (is) really be the problem here...

I suggest you to watch this video
http://www.youtube.com/watch?v=N6YdwzAvwOA
from 13:00

bye ;)
¯`·.¸¸.><((((º>¯`·.¸¸. ><((((º>
User avatar
qlimax
Master Developer
Master Developer
 
Posts: 271
Joined: Mon Aug 31, 2009 10:54 am
Location: Swiss

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: No registered users and 5 guests