0

我有一个LayeredImageView扩展类ImageView,我用它来创建由相互绘制的位图层组成的位图。我正在绘制的位图是游戏的军事单位,我想在单位位图上叠加的位图之一是他们的健康。我在方法中打开健康变量onDraw()并绘制适当的位图。

我的问题是该onDraw()方法永远不会被调用(使用调试消息验证)。

我将 LayeredImageViews 的适配器保存在ImageViewAdapter扩展类BaseAdapter中,并使用适当的 Bitmap 参数保存在getView(...)方法调用中。imageView.setImageBitmap(mThumbIds[position])据我了解,这应该调用 的invalidate()方法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);
}

}

如您所见,我尝试在此处实施建议,但没有成功。

编辑

这里是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;
}

任何帮助是极大的赞赏。谢谢!

4

0 回答 0