0

I have a simple ListView which has image on the left and text in the right. Currently i am using a LazyAdapter to load images from drawable resource according to name of dishes.

I have created a array of image ID's and would be using that to setImageView.

The problem is, the scrolling is not smooth which i know due to the fact that all the image loading is done by the main thread. I googled a lot about AsyncTask, which allows us run background thread. I am not getting how to use AsyncTask to achieve by task as all the examples that i have seen on AsyncTask has image downloaded from URL.

Please help me in using AsyncTask to load list of images from drawable to Bitmap.

Here is my mail class:

ARRAY.image is an integer Array which has image ID's which is used in adapter to set ImageView

package love.cookbook.FirstPage;

import com.actionbarsherlock.app.SherlockListActivity;
import com.actionbarsherlock.view.MenuItem;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.*;
import android.view.*;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.*;

public class ListViewSampleActivity extends SherlockListActivity {

    LazyAdapter adapter;

    public String packageName;
    public int imageID;
    String ingredientsImageName;

    public MySqliteHelper dbHelper;
    Cursor cur;


    String eachIngredientsImageName [];

    String listItemName;


    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        Intent intent = getIntent();

        super.onCreate(savedInstanceState);

        setContentView(R.layout.list_main);
    dbHelper = new MySqliteHelper(this);

    ListView list=(ListView)findViewById(android.R.id.list);

    final FirstPageActivity firstPage = new FirstPageActivity();


        ARRAY.dishes = intent.getStringArrayExtra("DISHES");
        ARRAY.description = intent.getStringArrayExtra("DESCRIPTION");
        ARRAY.timeToPrepare = intent.getStringArrayExtra("TIMETOPREPARE");
        ARRAY.lock = intent.getStringArrayExtra("LOCKVALUE");
        ARRAY.imageName = intent.getStringArrayExtra("IMAGENAME");

        ARRAY.nonVeg = intent.getStringArrayExtra("NONVEG");
        ARRAY.isFavourite = intent.getStringArrayExtra("ISFAVOURITE");

        /*
         * This code snippet is used to fetch the image id from the resource folder from the name fetched from database.
         */
        ARRAY.image = new int[ARRAY.imageName.length];

        packageName=this.getPackageName();
        for(int i=0;i<ARRAY.imageName.length;i++){
            ARRAY.image[i]=getResources().getIdentifier(ARRAY.imageName[i].toLowerCase(), "drawable", packageName);

        }


        adapter=new LazyAdapter (this, ARRAY.dishes,ARRAY.description,ARRAY.timeToPrepare,ARRAY.image,ARRAY.lock,ARRAY.nonVeg);
        list.setAdapter(adapter);

        list.setOnItemClickListener(new OnItemClickListener(){


            public void onItemClick(AdapterView<?> parent, View view, int position,
                    long id) {

            }

        });
    }

}

Here is my Adapter class:

package love.cookbook.FirstPage;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

    public class LazyAdapter extends BaseAdapter {

        private Activity activity;
        private String[] dishes;
        private String[] recipeDescripion;
        private String[] timeToPrepare;
        private String[] lock;
        private String[] nonVeg;
        private int [] images;
        ViewHolder holder;
        Bitmap bitmap;
        private static LayoutInflater inflater=null; 

        public LazyAdapter(Activity a, String[] dishes, String[] recipeDescription,String[] timeToPrepare,int[] images,String [] lock,String [] nonVeg) {
            activity = a;
            this.dishes=dishes;
            this.images=images;
            this.bitmap = bitmap;
            this.recipeDescripion=recipeDescription;
            this.timeToPrepare=timeToPrepare;
            this.lock=lock;
            this.nonVeg=nonVeg;
            inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        }

        public View getView(int position, View convertView, ViewGroup parent) {
            //View vi=convertView;
            if(convertView==null){
                //vi = inflater.inflate(R.layout.item, null);
                convertView = inflater.inflate(R.layout.item,parent, false);
                //image = (ImageView)vi.findViewById(R.id.imageView);
                holder = new ViewHolder();
                holder.image = (ImageView) convertView.findViewById(R.id.imageView2);
                holder.vegNonveggImageView = (ImageView) convertView.findViewById(R.id.imageView3);

                holder.textView1 = (TextView)convertView.findViewById(R.id.textView1);
                holder.textView2 = (TextView)convertView.findViewById(R.id.textView2);
                holder.textView3 = (TextView)convertView.findViewById(R.id.textView3);
                convertView.setTag(holder);

            }
            else
                holder = (ViewHolder)convertView.getTag();


            holder.textView1.setText(dishes[position]);

            holder.image.setImageResource(images[position]);

            holder.textView2.setText(recipeDescripion[position]);
            holder.textView3.setText(timeToPrepare[position]); 

            if(nonVeg[position].equals("1")){
                holder.vegNonveggImageView.setImageResource(R.drawable.non_veg_symbol);
            }
            else{
                holder.vegNonveggImageView.setImageResource(R.drawable.veg_symbol);
            }


            return convertView;
        }

        public int getCount() {

            return dishes.length;
        }

        public Object getItem(int position) {
            return position;
        }

        public long getItemId(int position) {
            return position;
        }

        public class ViewHolder{
            ImageView image;
            TextView textView1;
            TextView textView2;
            TextView textView3;
            ImageView vegNonveggImageView;
        }


}
4

1 回答 1

0

您的滚动不流畅问题可能是由于图像占用过多内存造成的。

您可以通过 DDMS 透视图在 Eclipse 中检查这一点,并查看分配的内存大小以查看它是否看起来太大。

这可以通过以下方式检查:

  • 打开 DDMS 透视图:窗口 > 打开透视图 > DDMS,
  • 找到设备视图:窗口 > 显示视图 > 设备
  • 在列表中选择您的应用程序(模拟器必须正在运行)
  • 点击更新堆(看起来像一个半满的绿色容器)
  • 找到堆视图:窗口 > 显示视图 > 堆
  • 单击原因 GC

如果类型的总大小1-byte array(byte[], boolean[])太大,这可能会导致滚动问题。

我不认为 anAsyncTask会解决您的问题,因为无论如何更新视图都必须在 UI(主)线程上完成。

如果您加载的图像太大,您可以使用以下方法最小化它们:

Bitmap smallIcon = Bitmap.createScaledBitmap(bigIcon, width, height, false);

希望这可以帮助

于 2013-09-07T20:05:27.797 回答