1

だから私はAndroid開発にかなり慣れていないので、ImageViewの使用で奇妙なことに遭遇しました。ポインタや提案は大歓迎です!

ImageViews のビットマップを動的に設定していますが、これはうまく機能しています。たまにしか画像が表示されないことを除いて、残りの時間は、以下に示すように、おおよその画像色でフルカラーで塗りつぶされます。

スクリーンショット

Androidフォーラムから入手したこのコードを使用して適切にスケーリングされていると思うので、メモリの問題は発生していないと思います....

public static Bitmap decodeSampledBitmapFromStream(InputStream inputStream, int reqWidth, int reqHeight) {

        // First decode with inJustDecodeBounds=true to check dimensions
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeStream(inputStream,null,options);

        // Calculate inSampleSize
        options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

        // Decode bitmap with inSampleSize set
        options.inJustDecodeBounds = false;
        return BitmapFactory.decodeStream(inputStream,null,options);
    }
4

1 に答える 1

1

この問題に何時間も取り組んだ後、私は最終的に、これらのビットマップのロードを容易にするためにAsyncTaskを使用していたため、メインUIスレッドよりも高速であることがあることに気付きました。myImageView.getHeight()&width()を呼び出したとき、これは私を台無しにしました。

それで、これが途中で他の誰かを助けるかもしれないことを期待して私が思いついた解決策です:

public class DecodeTask extends AsyncTask<String, Void, Bitmap> {

public ImageView currentImage;
private static AssetManager mManager;

public DecodeTask(ImageView iv, AssetManager inputManager) {
    currentImage = iv;
    mManager = inputManager;
}

protected Bitmap doInBackground(String... params) {

    int bottomOut = 1000;
    while(currentImage.getMeasuredWidth() == 0 && currentImage.getMeasuredHeight() == 0 && (--bottomOut) > 0)
    {
        try {
            Thread.sleep(10);
        } catch (InterruptedException e1) {
            e1.printStackTrace();
        }
    }

    InputStream iStream = null;
    Bitmap bitmap = null;
    try {
        iStream = mManager.open("photos" + File.separator + params[0]);

        bitmap = ImageUtils.decodeSampledBitmapFromStream(iStream, currentImage.getMeasuredWidth(), currentImage.getMeasuredHeight());

        iStream.close();

    } catch (IOException e) {
        e.printStackTrace();
    }

    return bitmap;
}

@Override
protected void onPostExecute(Bitmap result) {
    if(currentImage != null) {
        currentImage.setImageBitmap(result);
        currentImage.invalidate();
    }
}

}
于 2012-12-26T23:16:45.667 に答える