Android模拟器中视图无法显示的问题排查求助
问题分析与修复方案
嗨,别担心,这确实是新手容易踩的典型小坑,我帮你梳理下代码里的关键问题,以及对应的修复方案:
核心问题点
- 类成员
img1未被正确赋值:你在My_Bitmap2()方法里创建了局部变量Bitmap img1 = TrimBitmap(my_bitmap2);,这只会给局部变量赋值,而类级别的public Bitmap img1;始终是null,导致ImageView没有位图可以显示。 ImageView未添加到布局:你创建了LinearLayout和ImageView,但漏掉了ll.addView(IV1)这一步——就算有内容,没把视图加入布局树也不会被渲染出来。- Activity自定义构造方法无效:Android的Activity实例由系统创建,自定义带参数的
public Trimmer(final Resources res)永远不会被调用,甚至会导致Activity无法正常初始化(系统找不到无参构造)。你完全可以通过getResources()直接获取资源,不需要手动传参。 - 未触发位图加载逻辑:你定义了
My_Bitmap2()方法但没在onCreate里调用,等于完全没执行位图的加载和裁剪操作。
修正后的完整代码
package com.example.trimmer_test; import android.app.Activity; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Color; import android.os.Bundle; import android.widget.ImageView; import android.widget.LinearLayout; public class Trimmer extends Activity { public Bitmap img1; public static Bitmap TrimBitmap(Bitmap bmp) { int imgHeight = bmp.getHeight(); int imgWidth = bmp.getWidth(); //TRIM WIDTH - LEFT int startWidth = 0; for (int x = 0; x < imgWidth; x++) { if (startWidth == 0) { for (int y = 0; y < imgHeight; y++) { if (bmp.getPixel(x, y) != Color.TRANSPARENT) { startWidth = x; break; } } } else break; } //TRIM WIDTH - RIGHT int endWidth = imgWidth; // 修改默认值,避免全透明时出现负数 for (int x = imgWidth - 1; x >= 0; x--) { if (endWidth == imgWidth) { for (int y = 0; y < imgHeight; y++) { if (bmp.getPixel(x, y) != Color.TRANSPARENT) { endWidth = x; break; } } } else break; } //TRIM HEIGHT - TOP int startHeight = 0; for (int y = 0; y < imgHeight; y++) { if (startHeight == 0) { for (int x = 0; x < imgWidth; x++) { if (bmp.getPixel(x, y) != Color.TRANSPARENT) { startHeight = y; break; } } } else break; } //TRIM HEIGHT - BOTTOM int endHeight = imgHeight; // 修改默认值,避免全透明时出现负数 for (int y = imgHeight - 1; y >= 0; y--) { if (endHeight == imgHeight) { for (int x = 0; x < imgWidth; x++) { if (bmp.getPixel(x, y) != Color.TRANSPARENT) { endHeight = y; break; } } } else break; } // 安全判断,避免裁剪区域非法导致崩溃 int width = endWidth - startWidth + 1; int height = endHeight - startHeight + 1; if (width <=0 || height <=0) { return bmp; // 如果全透明,返回原位图 } return Bitmap.createBitmap( bmp, startWidth, startHeight, width, height ); } public void My_Bitmap2() { Bitmap my_bitmap2 = BitmapFactory.decodeResource(getResources(), R.drawable.my_bitmap2); img1 = TrimBitmap(my_bitmap2); // 赋值给类成员变量,让onCreate能拿到 } @Override public void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); My_Bitmap2(); // 调用方法加载并裁剪位图 LinearLayout ll = new LinearLayout(this); ll.setOrientation(LinearLayout.VERTICAL); ll.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT)); ImageView IV1 = new ImageView(this); IV1.setImageBitmap(img1); ll.addView(IV1); // 将ImageView添加到布局中 setContentView(ll); } }
额外优化说明
我还在TrimBitmap方法里加了两个安全优化:
- 修改
endWidth和endHeight的默认值,避免图片全透明时出现裁剪区域宽高为负数的情况,防止Bitmap.createBitmap抛出异常。 - 添加裁剪区域合法性判断,如果计算出的宽高非法,直接返回原位图,避免程序崩溃。
现在运行修改后的代码,应该就能看到裁剪后的图片正常显示了。
内容的提问来源于stack exchange,提问作者Statsanalyst
相关产品推荐
相关产品推荐

