如何将图像添加为另一个图像的特征?

时间:2012-04-05 14:07:37

标签: java android image graphics

在Java中我创建了以下类,它扩展了ImageView以创建一个字符...我想知道是否可以添加我的角色所持有的项目的另一个图像(参见方法“addWeapon()”)我的角色移动时会移动(参见方法“warriorMover(..)”)。基本上,我想知道是否有可能使“武器形象”成为主角的“特征/部分”?

public class WarriorEntity extends ImageView
{
private kingsColor kingColor;
private weapons weaponId;

private int kingSide;
private int warriorId;
private int nLeft;
private int nTop;

public WarriorEntity(Context context)
{
  super(context);
}

public void defineWarrior(kingsColor a1, weapons a2, int a3, int a4, int a5, int a6)
{
  kingColor = a1;
  weaponId = a2;
  kingSide = a3;
  warriorId = a4;
  nLeft = a5;
  nTop = a6;

  switch (kingColor)
  {
    case blue:
      this.setImageResource(R.drawable.bluewarrior);
      break;
    case green:
      this.setImageResource(R.drawable.greenwarrior);
      break;
    case purple:
      this.setImageResource(R.drawable.purplewarrior);
      break;
    case red:
      this.setImageResource(R.drawable.redwarrior);
      break;
    case white:
      this.setImageResource(R.drawable.whitewarrior);
      break;
    case yellow:
      this.setImageResource(R.drawable.yellowwarrior);
      break;
  }
  this.setId(warriorId);
  this.setOnClickListener(WarriorClick);
  setImagexy(this, nLeft, nTop, 20, 30);
}

public void addWeapon()
{
  switch (weaponId)
  {
     case bowandarrow :
        break;
     case mace :
        break;
     case spear :
        break;
     case sword :
         // - Here I would want some code to add the weapon image which as if it were part
              of the same image as the character... moves/reacts with the character.
        break;
  }
}

public void warriorMover(int leftX, int topY)
{
  setImagexy(this, leftX + 8, topY - 3, 20, 30);
}

“setImagexy”是一种自定义方法,可以将图像移动到屏幕上的给定位置。

1 个答案:

答案 0 :(得分:0)

可以使用不同的方法: 制作默认精灵的副本并在其上绘制剑(如果您不需要经常更新精灵,这是一个很好的方法

或者你可以在绘制角色精灵后将剑渲染到精灵顶部:

render_loop()
_>draw_characterSprite()
__>draw_equipment()