【问题标题】:Loading Bitmaps Efficiently - showing solid color on start有效加载位图 - 开始时显示纯色
【发布时间】:2013-08-18 11:41:58
【问题描述】:

我使用了开发人员指南中的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);
}

我在 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 重新启动之前,其他一切都正常工作。如果我们重新启动或恢复这个活动,同样的事情正在发生。

那么,出了什么问题?

【问题讨论】:

    标签: android image bitmap imageview


    【解决方案1】:

    我刚刚解决了它并弄清楚了我自己,实际上这是一个关于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 “learn_full_iv”,默认情况下宽度和高度为“0”,因为图像仍未设置在图像视图

    如果我们打电话:

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

    在这种情况下,宽度和高度将为零,因此,只会显示该图像中的纯色。所以,我使用 方法 将屏幕的宽度和高度作为整数值& 将它们传递给上述 BITMAP DECODE 方法中相应的宽度和高度部分。

    就是这样,因为现在我们将宽度和高度作为值,现在将显示位图。

    简单!!!

    【讨论】:

    • 我也掉进了那个坑。谢谢你帮我出坑:)
    【解决方案2】:

    请说明:

    1. 你为什么在 runnable 中调用 switch 代码?

    2. 你在哪里调用处理程序来运行??

    确保不要在 onresume/onstart 方法中调用处理程序启动方法,同时确保在方向改变的情况下不会再次调用 oncreate

    【讨论】:

      猜你喜欢
      • 2016-07-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-20
      • 1970-01-01
      相关资源
      最近更新 更多