Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/227.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何添加图像作为另一图像的特征?_Java_Android_Image_Graphics - Fatal编程技术网

Java 如何添加图像作为另一图像的特征?

Java 如何添加图像作为另一图像的特征?,java,android,image,graphics,Java,Android,Image,Graphics,在Java中,我创建了以下类,它扩展了ImageView来创建一个字符。。。我想知道是否有可能添加我的角色持有的物品的另一个图像,当我的角色移动时,它将移动,请参见方法WarriorOver。。。基本上,我想知道是否有可能使武器图像成为主角的一种特征/部分 public class WarriorEntity extends ImageView { private kingsColor kingColor; private weapons weaponId; private int kingS

在Java中,我创建了以下类,它扩展了ImageView来创建一个字符。。。我想知道是否有可能添加我的角色持有的物品的另一个图像,当我的角色移动时,它将移动,请参见方法WarriorOver。。。基本上,我想知道是否有可能使武器图像成为主角的一种特征/部分

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是一个自定义方法,它完全可以。。。将图像移动到屏幕上的给定位置。

可以使用不同的方法: 制作一个默认精灵的副本并在上面画剑是一个很好的方法,以防你不需要经常更新精灵

或者,您可以在绘制角色精灵后,在精灵顶部渲染剑:

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

为什么不添加一个武器类而不是只使用一个枚举,并扩展ImageView?是的,这实际上是一个想法。枚举仅表示角色持有的武器类型。我曾想过使用不同的武器绘制不同的精灵,但最终每个角色会有5到6个额外的图像。您只有1个额外的图像可用作缓冲区,以保存预绘制的角色及其装备。为什么你需要5个或6个?