Alfredo Improta
Alfredo Improta

Reputation: 3

Lag after setImageDrawable

I have a problem with my code. The point is that I have 16 images and when the load initially my Scrolling acitivity no lag issues, but after several setimageas becomes very slow. I hope you can help me. ps: the 16 images are loaded on a fragment the problem of lag occurs when I move from one fragment to 'another scroll through.

Upvotes: 0

Views: 458

Answers (3)

1shubhamjoshi1
1shubhamjoshi1

Reputation: 556

I suggest you to use Picaso library to load images as its very fast and efficient

its as simple as

Picasso.with(context).load(R.drawable.landing_screen).into(imageView1);

for more information I suggest you to go to Picaso documentation

Upvotes: 1

Alfredo Improta
Alfredo Improta

Reputation: 3

This is the code, then if the load images via xml src nothing lag, just use setImageDrawable lag comes out..

 for(int h=0;h<16;h++){
        final int finalH = h;


        button[h].setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_DOWN) {
                    for (int i = 0; i < 8; i++) {
                        if (tresy[i] == ((int) button[finalH].getTag())) {
                            return true;
                        }

                    }


                    if ((test)&&(tempo)) {
                        Log.d("id", String.valueOf(button[finalH].getTag()));
                        button[finalH].setImageDrawable(carte[((int) button[finalH].getTag())]);
                        //set image resource tag!
                        logTaG[0] = (int) button[finalH].getTag();
                        logBtn = finalH;
                        test = false;


                    } else if(tempo) {
                        test = true;
                        button[finalH].setImageDrawable(carte[((int) button[finalH].getTag())]);
                        //set image resource tag!
                        logTaG[1] = (int) button[finalH].getTag();
                        if ((logTaG[0] == logTaG[1] && (logBtn != finalH))) {
                            tresy[j] = logTaG[0];
                            Intent intent = new Intent(v.getContext(), Browser_descrizione.class);
                            intent.putExtra("Tag", tresy[j]);
                            startActivity(intent);


                            j++;
                            Log.d("bravo", "hai vinto");

                            if (j == 7) {
                                Log.d("Bravo", "hai completato il gioco");
                                //Far comparire warning con scritto ricomincia o esci
                                attempt++;
                                SM.sendData(attempt);

                            }
                        } else {

                            Log.d("Riprova", "sarai più fortunato");
                            final Handler handler = new Handler();
                            tempo = false;

                            handler.postDelayed(new Runnable() {
                                @Override
                                public void run() {
                                    // Do something after 5s = 5000ms
                                    button[finalH].setImageResource(R.drawable.back_card);
                                    button[logBtn].setImageResource(R.drawable.back_card);
                                    tempo = true;

                                }
                            }, 500);
                            attempt++;
                            SM.sendData(attempt);
                        }


                    }
                    return true;
                }


                return false;
            }
        });
    }

Upvotes: 0

Nafiz Bayındır
Nafiz Bayındır

Reputation: 495

You can use Android Universal Image Loader library to get rid off this problem. Also try to use List View or Grid View.

Upvotes: 0

Related Questions