/ /ビットマップを効率的にロードする-開始時に単色で表示する-android、image、bitmap、imageview

ビットマップを効率的にロード-開始時に単色を表示-Android、イメージ、ビットマップ、イメージビュー

私は ビットマップを効率的にロードする 開発者ガイドから、そのチュートリアルからポイントツーポイントの指示に従いました。

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

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!

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

では、何が問題になっていますか?

回答:

回答№1は2

私はちょうどそれを解決し、それを自分自身で理解しました、実際にはそれは画像のアンドロイドローディングのための小さな見方です:

  @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 "learn_full_iv"を有効にします。これは、ImageViewに画像がまだ設定されていないため、デフォルトで幅と高さが "0"になります。

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

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

この場合、幅と高さはゼロになるので、その画像の単色だけが表示されます。そこで、私は 方法 画面の幅と高さを整数値として取得し、上記のBITMAPDECODEメソッドの対応する幅と高さのセクションに渡します。

これで、幅と高さを値として使用できるようになり、ビットマップが表示されます。

シンプル!!!


回答№2の場合は0

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

  1. なぜランナブルでスイッチタラを呼んでいるのですか?

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

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