我已经使用了开发人员指南中的有效加载位图,并按照该教程中的点对点说明进行操作。
如您所知,这是我使用的代码:
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);
}
我在 switch 案例中将此方法称为:
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() 时,第一次点击的图片只会显示图片的纯色,而不是整个图片,如下图:
从下一次点击开始,无论是在同一张图片还是下一张图片上,代码都可以正常工作:
在 Activity 重新启动之前,其他一切都可以正常工作。如果我们重新启动或恢复这个活动,同样的事情正在发生。
那么,出了什么问题?