0

Loading Bitmaps Efficiently from developer's Guide を使用し、そのチュートリアルのポイントごとの指示に従いました。

ご存知かもしれませんが、これは私が使用したコードです。

public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth,int reqHeight){
    //Raw height and width of the Image
    final int height = options.outHeight;
    final int width = options.outWidth;
    int inSampleSize = 1;

    if (height>reqHeight || width>reqWidth) {
        final int heightratio = Math.round((float)height / (float)reqHeight);
        final int widthRatio = Math.round((float)width / (float)reqWidth);

        inSampleSize = heightratio < widthRatio ? heightratio : widthRatio;
    }
    return inSampleSize;
}
public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId,int reqWidth,int reqHeight){
    //first decode with inJustdecodeBounds = true to check dimensions.

    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeResource(res, resId, options);
    //Calculate inSampleSize
    options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

    //Decode bitmap with inSampleSize
    options.inJustDecodeBounds = false;

    return BitmapFactory.decodeResource(res, resId, options);
}

そして、私はスイッチケースでこのメソッドを次のように呼び出しています:

handler.postDelayed(new Runnable() {

        @Override
        public void run() {
            // TODO Auto-generated method stub
            learn_full_iv.setVisibility(View.VISIBLE);
            learn_full_iv.startAnimation(anim);

            switch (id) {
            case R.id.a:
                //learn_full_iv.setImageResource(R.drawable.aeroplane);
                learn_full_iv.setImageBitmap(decodeSampledBitmapFromResource(
                        getResources(), R.drawable.aeroplane, learn_full_iv.getWidth(), learn_full_iv.getWidth()));
                Toast.makeText(getApplicationContext(), "Width : " + learn_full_iv.getWidth() + "Height : " + learn_full_iv.getHeight(), Toast.LENGTH_SHORT).show();
                playSound(1, 2);
                break;
            case R.id.b:
                //learn_full_iv.setImageResource(R.drawable.ball);
                learn_full_iv.setImageBitmap(decodeSampledBitmapFromResource(
                        getResources(), R.drawable.ball, learn_full_iv.getWidth(), learn_full_iv.getWidth()));
                Toast.makeText(getApplicationContext(), "Width : " + learn_full_iv.getWidth() + "Height : " + learn_full_iv.getHeight(), Toast.LENGTH_SHORT).show();
                playSound(3, 4);
                break;
            case R.id.c:
                //learn_full_iv.setImageResource(R.drawable.camel);
                learn_full_iv.setImageBitmap(decodeSampledBitmapFromResource(
                        getResources(), R.drawable.camel, learn_full_iv.getWidth(), learn_full_iv.getWidth()));
                Toast.makeText(getApplicationContext(), "Width : " + learn_full_iv.getWidth() + "Height : " + learn_full_iv.getHeight(), Toast.LENGTH_SHORT).show();
                playSound(5, 6);
                break;
            case R.id.d:
                //learn_full_iv.setImageResource(R.drawable.drum);
                learn_full_iv.setImageBitmap(decodeSampledBitmapFromResource(
                        getResources(), R.drawable.drum, learn_full_iv.getWidth(), learn_full_iv.getWidth()));
                Toast.makeText(getApplicationContext(), "Width : " + learn_full_iv.getWidth() + "Height : " + learn_full_iv.getHeight(), Toast.LENGTH_SHORT).show();
                playSound(7, 8);
                break;
                    default:
                    break;     
}

そして今、 onCreate() をロードするたびに問題が発生し、最初にクリックされた画像は、以下に示すように、画像全体ではなく画像の単色のみを表示します。

幅と高さのない画像ビュー

次のクリックから、同じ画像でも次の画像でも、コードは次のように正常に動作します。希望の幅と高さの ImageView!

アクティビティが再起動されるまで、他のすべては正常に機能します。このアクティビティを再開または再開すると、同じことが起こっています。

それで、何がうまくいかないのですか?

4

2 に答える 2

2

私はちょうどそれを解決し、それを自分で理解しました。実際には、Androidの画像の読み込みを少し見ただけです:

  @Override
    public void run() {
        // TODO Auto-generated method stub
        learn_full_iv.setVisibility(View.VISIBLE);
        learn_full_iv.startAnimation(anim);

        switch (id) {
        case R.id.a:
            //learn_full_iv.setImageResource(R.drawable.aeroplane);
            learn_full_iv.setImageBitmap(decodeSampledBitmapFromResource(
                    getResources(), R.drawable.aeroplane, learn_full_iv.getWidth(), learn_full_iv.getWidth()));
            Toast.makeText(getApplicationContext(), "Width : " + learn_full_iv.getWidth() + "Height : " + learn_full_iv.getHeight(), Toast.LENGTH_SHORT).show();
            playSound(1, 2);
            break;
    ...........

上記のコードの問題は、イメージがまだ ImageView に設定されていないため、デフォルトで幅と高さが「0」になる run メソッドの開始時に ImageView の「learn_full_iv」を有効にしていることです。

したがって、呼び出している場合:

 learn_full_iv.setImageBitmap(decodeSampledBitmapFromResource(
                getResources(), R.drawable.aeroplane, learn_full_iv.getWidth(), learn_full_iv.getWidth()));

この場合、幅と高さはゼロになるため、その画像の単色のみが表示されます。したがって、画面の幅と高さを整数値として取得し、それらを対応する幅と高さのセクションに渡すメソッドを使用しました上記の BITMAP DECODE メソッドで。

これで、値として幅と高さが得られたので、ビットマップが表示されます。

単純!!!

于 2013-08-27T07:43:12.480 に答える
0

どうか明らかにしてください :

  1. runnable で switch cod を呼び出すのはなぜですか??

  2. そして、実行するハンドラーをどこで呼び出していますか??

onresume/onstart メソッドでハンドラの start メソッドを呼び出さないようにしてください。また、向きが変わった場合に oncreate が再度呼び出されないようにしてください。

于 2013-08-16T06:30:34.863 に答える