我使用了开发人员指南中的Loading Bitmaps Efficiently,并按照教程中的指示点进行操作。
您可能知道,这是我使用的代码:
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()时,首先单击的图像将仅显示图像的纯色而不显示整个图像,如下所示:
从下次点击开始,无论是在同一图片还是下一张图片上,代码都可以正常工作:
其他一切正常,直到Activity重新启动。如果我们重新启动或恢复此活动,则会发生同样的事情。
那么,出了什么问题?
答案 0 :(得分:2)
我刚解决了它&amp;想出了我的自己,实际上它是一个小的看看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;
...........
上面代码中的问题是我只是在run方法的开头启用ImageView“learn_full_iv”,它将具有宽度&amp;高度为“0”默认情况下,因为图像仍未在ImageView上设置
所以,如果我们打电话:
learn_full_iv.setImageBitmap(decodeSampledBitmapFromResource(
getResources(), R.drawable.aeroplane, learn_full_iv.getWidth(), learn_full_iv.getWidth()));
宽度和宽度在这种情况下,高度将为零,因此,只显示该图像的固体颜色。所以,我使用方法来获得宽度和宽度。屏幕高度为整数值&amp;将它们传递到相应的宽度和宽度。上面的BITMAP DECODE方法中的高度部分。
多数民众赞成,因为现在我们有宽度和宽度。高度为值,现在将显示位图。
简单!!!
答案 1 :(得分:0)
请澄清:
你为什么要在runnable中调用switch cod?
你在哪里调用处理程序来运行??
确保不在onresume / onstart方法中调用处理程序启动方法也确保在方向更改时不再调用oncreate