0

我已经使用了开发人员指南中的有效加载位图,并按照该教程中的点对点说明进行操作。

如您所知,这是我使用的代码:

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() 时,第一次点击的图片只会显示图片的纯色,而不是整个图片,如下图:

没有宽度和高度的图像视图

从下一次点击开始,无论是在同一张图片还是下一张图片上,代码都可以正常工作:具有所需宽度和高度的 ImageView!

在 Activity 重新启动之前,其他一切都可以正常工作。如果我们重新启动或恢复这个活动,同样的事情正在发生。

那么,出了什么问题?

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“learn_full_iv”,默认情况下,宽度和高度为“0”,因为图像仍未在 ImageView 上设置

所以如果我们打电话:

 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 方法中调用处理程序启动方法,还要确保在方向更改的情况下不会再次调用 oncreate

于 2013-08-16T06:30:34.863 回答