0

私はアンドロイドが初めてです。ディレクトリに 1,000 を超える画像があります。グリッドビューで画像の親指を表示しています。次のコードで動作します。しかし問題は、ビューをロードするのに 45 秒かかることです。私はそれが必要です:ローダーでグリッドを表示し、画像を1つずつロードします。そのため、ユーザーは最後の画像が読み込まれるのを待つことができません。

コードは次のとおりです。

public View getView(int position, View convertView, ViewGroup parent) {

        LayoutInflater layoutInflater = (LayoutInflater) ctx
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        ListRowHolder listRowHolder;
        if (convertView == null) {
            convertView = layoutInflater.inflate(R.layout.ll_sponsor_list_item,
                    parent, false);
            listRowHolder = new ListRowHolder();
            listRowHolder.imgSponsor = (ImageView) convertView
                    .findViewById(R.id.imggrid_item_image);
            convertView.setTag(listRowHolder);

        } else {
            listRowHolder = (ListRowHolder) convertView.getTag();
        }
        try {

            listRowHolder.imgSponsor
                    .setImageBitmap(decodeSampledBitmapFromResource(
                            ImageName.get(position)));
        } catch (Exception e) {
            Toast.makeText(ctx, e + "", Toast.LENGTH_SHORT).show();
        }

        return convertView;
    }


    public static Bitmap decodeSampledBitmapFromResource(String fileName) {
        Bitmap picture = BitmapFactory.decodeFile(fileName);
        int width = picture.getWidth();
        int height = picture.getWidth();
        float aspectRatio = (float) width / (float) height;
        int newWidth = 98;
        int newHeight = (int) (98 / aspectRatio);
        return picture = Bitmap.createScaledBitmap(picture, newWidth,
                newHeight, true);
    }
4

1 に答える 1

1

遅い理由は、多くの画像に対して、ファイルをデコードし、UI スレッドでスケーリングされたビットマップを生成しているためです。長時間の操作を行っているため、画像の遅延読み込みを行う必要があります。

前提はリンクのソリューションに似ていますが(を使用できますHandler)、そこに画像をダウンロードする代わりに、ファイルをデコードしてビットマップをスケーリングします。

于 2013-01-05T07:12:08.893 に答える