Bram
Bram

Reputation: 47

GridView scrolling stuttering

I have a problem with my image adapter I think.. Whenether I scroll trough the gridview it stutters badly. I think I overlooked something in my code but i cant figure it out.

My image adapter code:

public class ImageAdapter extends BaseAdapter
{
    private Context context;

    public ImageAdapter(Context c)
    {
        context = c;
    }

    //---returns the number of images---
    public int getCount() {
        return logoCount;
    }

    //---returns the ID of an item---
    public Object getItem(int position) {
        return position;
    }

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

    //---returns an ImageView view---
    public View getView(int position, View convertView, ViewGroup parent)
    {
        ImageView imageView;
        if (convertView == null) {
            imageView = new ImageView(context);
            imageView.setLayoutParams(new GridView.LayoutParams(300, 300));
            imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
            imageView.setPadding(5, 5, 5, 5);
        } else {
            imageView = (ImageView) convertView;
        }
        Drawable drawable = null;
        switch(GetLogoStatus(position)) {
            case 1:
                drawable = ContextCompat.getDrawable(context, R.drawable.check);
                break;
            case 2:
                drawable = ContextCompat.getDrawable(context, R.drawable.tipsused);
                break;
            case 3:
                drawable = ContextCompat.getDrawable(context, R.drawable.cross);
                break;
        }
        final Bitmap largeImage = ((BitmapDrawable) drawable).getBitmap().copy(Bitmap.Config.ARGB_8888, true);

        final Drawable logoDrawable = ContextCompat.getDrawable(context, GetImage(position));
        Bitmap smallImage = ((BitmapDrawable) logoDrawable).getBitmap().copy(Bitmap.Config.ARGB_8888, true);

        final Canvas canvas = new Canvas(largeImage);

        float ratioBitmap = (float) smallImage.getWidth() / (float) smallImage.getHeight();
        float ratioMax = (float) largeImage.getWidth() / (float) largeImage.getHeight();
        int finalWidth = largeImage.getWidth();
        int finalHeight = largeImage.getHeight();
        if (ratioMax > 1) {
            finalWidth = (int) ((float)largeImage.getHeight() * ratioBitmap);
        } else {
            finalHeight = (int) ((float)largeImage.getWidth() / ratioBitmap);
        }
        smallImage = Bitmap.createScaledBitmap(smallImage, finalWidth, finalHeight, true);

        int startX= (canvas.getWidth()-smallImage.getWidth())/2;
        int startY=(canvas.getHeight()-smallImage.getHeight())/2;
        canvas.drawBitmap(smallImage,startX, startY, null);
        imageView.setImageBitmap(largeImage);

        //imageView.setImageResource(GetImage(position));
        return imageView;
    }
}

Hope someone can help me out:)

Upvotes: 0

Views: 144

Answers (1)

Human
Human

Reputation: 10815

  • Use a custom view instead of trying to made the same every time with getView
  • Use a ViewHolder

Upvotes: 1

Related Questions