在画布上绘制矩形

时间:2019-01-30 14:01:36

标签: java android android-canvas

我正在尝试制作游戏。我有一个mainActivity,叫做BridgeScene,还有一个敌人的职业。在mainActivity中,我有两个ImageView和一些按钮。

在我的课堂上,我有一个敌人(用矩形表示),并且使用Canvas绘制矩形。我需要在BridgeScene活动上绘制此矩形。我该怎么办?

我无法两次设置contentview,因为它已在活动中设置为

setContentView(R.layout.activity_bridge_scene);

那么如何在我的BridgeScene活动中从敌人的班级中绘制矩形?

我的活动

public class BridgeScene extends AppCompatActivity {

    private static final  int[] ALL_DRAWABLE_RES_EAGLE = new int[]{
            R.drawable.stickmanreturnkick0,
            R.drawable.stickmanreturnkick1,
    };

    private Ennemy1 ennemy1;

    private Handler handler = new Handler();
    private final static long Interval = 30;

    private Runnable updateImageTask;
    private int currentDrawableResIndex;

    private static long IMAGE_UPDATE_DELAY_MILLIS = 30;

    private ImageView imvStickman;
    private ImageView imvJump;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bridge_scene);

        ennemy1 = new Ennemy1(this);
        setContentView(ennemy1);

        updateImageTask = new UpdateImageTask();

        imvJump = (ImageView) findViewById(R.id.imvJump);

        imvStickman = (ImageView) findViewById(R.id.imvStickman);
        imvEnnemi = (ImageView) findViewById(R.id.imvEnnemi);

        imvJump.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
               //code//
            }
        });
    }
}

我的课:

public class Ennemy1 extends View{
    public  Bitmap rec;
    public  int recX;
    public  int recY;

    public int canvasWidth;
    public int canvasHeight;

    public Ennemy1(Context context){
        super(context);
        rec = BitmapFactory.decodeResource(getResources(), R.drawable.enem1);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawBitmap(rec, 100, 100, null);
        canvasWidth = canvas.getWidth();
        canvasHeight = canvas.getHeight();
    }
}

1 个答案:

答案 0 :(得分:0)

您应该添加与activity_bridge_scene.xml文件类似的内容

account.setDisabled(true);