我有一个扩展LayeredImageView
的类ImageView
,用于创建由相互绘制的位图层组成的位图。我正在绘制的位图是一个游戏的军事单位,我想在一个单位位图上叠加的一个位图是他们的健康状况。我在onDraw()
方法中打开运行状况变量并绘制相应的位图。
我的问题是永远不会调用onDraw()
方法(通过调试消息验证)。
我将LayeredImageViews的适配器保存在扩展ImageViewAdapter
的类BaseAdapter
中,并使用适当的Bitmap参数保存在getView(...)
方法调用imageView.setImageBitmap(mThumbIds[position])
中。根据我的理解,这应该是调用invalidate()
的{{1}}方法,而LayeredImageView
又应该调用onDraw()
,但就像我说的那样,这种情况并没有发生。
以下是LayeredImageView
类的核心:
public class LayeredImageView extends ImageView{
private static final String TAG = "LayeredImageView";
Context mContext;
int health;
public LayeredImageView(Context context) {
super(context);
mContext = context;
health = 0;
setWillNotDraw(false);
}
public void setHealth(int health){
this.health = health;
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Bitmap healthIcon = null;
int width;
int height;
int x;
int y;
switch(health){
case 1:
healthIcon = decodeSampledBitmapFromResource(mContext.getResources(),R.drawable.health1, 100, 100);
break;
case 2:
healthIcon = decodeSampledBitmapFromResource(mContext.getResources(),R.drawable.health2, 100, 100);
break;
}
if(healthIcon != null){
width = healthIcon.getWidth();
height = healthIcon.getHeight();
x = canvas.getWidth() - width;
y = canvas.getHeight() - height;
canvas.drawBitmap(healthIcon, x, y, new Paint());
}
}
public static int calculateInSampleSize(BitmapFactory.Options options,
int reqWidth, int reqHeight) {
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
// Calculate ratios of height and width to requested height and
// width
final int heightRatio = Math.round((float) height
/ (float) reqHeight);
final int widthRatio = Math.round((float) width / (float) reqWidth);
// Choose the smallest ratio as inSampleSize value, this will
// guarantee
// a final image with both dimensions larger than or equal to the
// requested height and width.
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 set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeResource(res, resId, options);
}
}
如您所见,我尝试实施建议here,但没有运气。
修改
这是实例化LayeredImageView
对象的地方:
public View getView(int position, View convertView, ViewGroup parent) {
LayeredImageView imageView;
if (convertView == null) { // if it's not recycled, initialize some attributes
imageView = new LayeredImageView(mContext);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(7, 5, 7, 5);
}
else{
imageView = (LayeredImageView) convertView;
}
Piece piece = game.getPiece(position % 10, 9-position/10);
if(piece != null){//here we overlay the health icon if the piece has non-zero health
imageView.setHealth(piece.getHealth());
}
imageView.setImageBitmap(mThumbIds[position]);
return imageView;
}
非常感谢任何帮助。谢谢!