如何在Android中的图像前显示图像按钮?

时间:2015-12-01 19:41:42

标签: java android

我对编程很陌生,我遇到了一个问题,我一直在制作一个游戏,所有的边框和背景都是图像,我一直试图找到一个显示一个图像按钮,所以我可以做一个设置菜单,但我不知道接下来该做什么请帮忙。这就是我按钮的方式

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

        ImageButton myButton = new ImageButton(this);
        myButton.setImageResource(R.drawable.settings);
        myButton.setId(1);
        RelativeLayout myLayout = new RelativeLayout(this);
        RelativeLayout.LayoutParams buttonParams =
                new RelativeLayout.LayoutParams(
                        RelativeLayout.LayoutParams.WRAP_CONTENT,
                        RelativeLayout.LayoutParams.WRAP_CONTENT);
        myLayout.addView(myButton, buttonParams);
        setContentView(myLayout);

        myButton.setOnClickListener(
                new View.OnClickListener() {
                    public void onClick(View v) {
                        onButtonClick(v);
                    }
                });


    }

    public void onButtonClick(View v) {
        //do something when button is clicked.

        PopupMenu popup = new PopupMenu(this, v);
        MenuInflater inflater = popup.getMenuInflater();
        inflater.inflate(R.menu.contextitems, popup.getMenu());
        popup.show();

    }

这就是我显示图片的方式

 @Override
    public void draw(Canvas canvas) {


        final float scaleFactorX = getWidth() / (WIDTH * 1.f);
        final float scaleFactorY = getHeight() / (HEIGHT * 1.f);

        if (canvas != null) {


            final int savedState = canvas.save();
            canvas.scale(scaleFactorX, scaleFactorY);

            bg.draw(canvas);
            if (!dissapear) {
                player.draw(canvas);
            }
            //draw smokepuffs
            for (Smokepuff sp : smoke) {
                sp.draw(canvas);
            }
            //draw missiles
            for (Missile m : missiles) {
                m.draw(canvas);
            }


            //draw topborder
            for (TopBorder tb : topborder) {
                tb.draw(canvas);
            }

            //draw botborder
            for (BotBorder bb : botborder) {
                bb.draw(canvas);
            }
            //draw explosion
            if (started) {
                explosion.draw(canvas);
            }


            drawText(canvas);
            canvas.restoreToCount(savedState);

        }


    }

1 个答案:

答案 0 :(得分:0)

您必须使用图层列表(可绘制)。

<bitmap 
      android:src="@drawable/your_photo"
      android:gravity="center" />