简体   繁体   中英

how to load images in array to faster display android

Hello i am running a onuithread inside a thread and it has a times and it will be changing randomly each time slower until it select 2 items from the list. my problem is that this items has pictures and my idea is to everytime the name change the picture should change but when adding the pictures it will display something like this: Skipped 66 frames! The application may be doing too much work on its main thread.

is there a way to load the images in the array and then in the runuithread only display them without loading them again? here in my runuithread:

 while (i < 400) {
                try {
                    Thread.sleep(i);
                    // here you check the value of getActivity() and break up if needed
                    if (getActivity() == null) {
                        return;
                    }
                    getActivity().runOnUiThread(new Runnable() // start actions in UI thread
                    {

                        @Override
                        public void run() {
                            start_btn.setEnabled(false);
                            Collections.shuffle(numbers);
                            Collections.shuffle(players_list);
                            player1_tv.setText(players[players_list.get(0)]);
                            player2_tv.setText(players[players_list.get(1)]);
                            group1_tv.setText(all_teams.get(numbers.get(0)));

                            player1_iv.setImageResource(getResources().getIdentifier(pictures.get(numbers.get(0)),"drawable",con.getPackageName()));
                            group2_tv.setText(group.get(numbers.get(1)));
                            i += 30;

                            if (i > 420) {
                                start_btn.setEnabled(true);
                            }
                        }
                    });
                } catch (InterruptedException e) {
                    // ooops
                }

The image can be loaded and stored as a Bitmap object. You can load Bitmap from a resource like this:

Bitmap bm = BitmapFactory.decodeResource(getResources(), {resourceId});

Then you can set the image:

player1_iv.setImageBitmap(bm);

Hope this helps

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