Android Рисование растрового изображения и кнопки на холсте по X Y Positions

Я новичок в Android, и у меня есть некоторые проблемы с моим приложением. Любая помощь будет оценена :)

У меня есть рисунок на холсте с помощью xpositon и ypostion с помощью датчиков (когда яя двигаю свой телефон, растровые изображения движутся)

Мне нужно создать движущуюся нажимаемую кнопку в центре моих растровых изображений, и когда я нажму на них, я перейду к следующему действию.

Растровые изображения прекрасно рисуются, но я не знаю, как рисовать на них кнопки, кнопки, которые будут перемещаться с моим растровым изображением при тех же значениях x и y. Как я могу это сделать?

Это мой кусок кода:

    private void updateBall() {

        // Calculate new speed
        // xVelocity += (xAcceleration * frameTime);
        // yVelocity += (yAcceleration * frameTime);

        // Calc distance travelled in that time
        // float xS = (xVelocity/2)*frameTime;
        // float yS = (yVelocity/2)*frameTime;

        // Add to position negative due to sensor
        // readings being opposite to what we want!
        // xPosition -= xS;

        // ------------------------------- MOVING Y --------------------------------------------------

        by = (-IMG_H + ddy) / 2f;
        ay = by / 90f;

        float dy = (ay * zAcceleration + by);

        if (Math.abs(zAcceleration) < 90 && Math.abs(yAcceleration) > 100)
            yPosition -= (yPosition - dy) / 20.;
        else if (zAcceleration > 0) {
            if (yPosition > -IMG_H) {
                if (Math.abs(yAcceleration) > 100) {
                    dy = (ay * 90 + by);
                    yPosition -= (yPosition - dy) / 20.;
                } else {
                    yPosition = -IMG_H;
                }
            }

        } else {
            if (yPosition < IMG_H) {
                if (Math.abs(yAcceleration) > 100) {
                    dy = (-ay * 90 + by);
                    yPosition -= (yPosition - dy) / 20.;
                } else {
                    yPosition = ddy;
                }
            }
        }

        // block y
        yPosition = 0;

        // ------------------------------- MOVING X --------------------------------------------------

        // For understanding :)
        // ddx= 1280-(float)display.getWidth();

        ax = -IMG_W / (180f);// -2*xStart);
        bx = -ax * xStart;

        // xStart pozycja początkowa

        if (xAcceleration > xStart && xAcceleration  xmax) { xPosition = xmax; } else if (xPosition < 0) {
         * xPosition = 0; }
         */
        /*
         * if (yPosition > ymax) { yPosition = ymax; } else if (yPosition < 0) {
         * yPosition = 0; }
         */

        // Print values
        Log.i("QLA", " x: " + xAcceleration + "   dx: " + dx + "    dx1: "+ dx1 + "  xpos: " + xPosition + " " + " xpos1: " + xPosition1);

    }

    // I've chosen to not implement this method
    public void onAccuracyChanged(Sensor arg0, int arg1) {
        // TODO Auto-generated method stub
    }

    @Override
    protected void onResume() {
        super.onResume();
        sensorManager.registerListener(this,
                sensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION),
                SensorManager.SENSOR_DELAY_GAME);
    }

    @Override
    protected void onStop() {
        // Unregister the listener
        sensorManager.unregisterListener(this);
        super.onStop();
    }

    public class CustomDrawableView extends View {
        public CustomDrawableView(Context context) {
            super(context);
            Bitmap ball = BitmapFactory.decodeResource(getResources(),
                    R.drawable.test);
            final int dstWidth = 1280;
            final int dstHeight = 960;
            mBitmap = Bitmap.createScaledBitmap(ball, dstWidth, dstHeight, true);

            Bitmap ball1 = BitmapFactory.decodeResource(getResources(),
                    R.drawable.tt);
            final int dstWidth1 = 1280;
            final int dstHeight1 = 960;
            mBitmap1 = Bitmap.createScaledBitmap(ball1, dstWidth1, dstHeight1, true);
            // mWood = BitmapFactory.decodeResource(getResources(),
            // R.drawable.wood);

            /*
             * ImageButton star = (ImageButton) findViewById(R.id.imageButton1);
             * star.setOnClickListener(new View.OnClickListener() { public void
             * onClick(View view) { Intent myIntent = new
             * Intent(view.getContext(), Main.class);
             * startActivityForResult(myIntent, 0);
             * overridePendingTransition(R.anim.fade_in, R.anim.fade_out); }
             * 
             * });
             */
            // ImageView ddss=star.getDrawable();

        }

        protected void onDraw(Canvas canvas) {
            final Bitmap bitmap = mBitmap;
            // canvas.drawBitmap(mWood, 0, 0, null);
            canvas.drawBitmap(bitmap, xPosition, yPosition, null);

            // ss.getChildAt(0).setX();

            final Bitmap bitmap1 = mBitmap1;
            // canvas.drawBitmap(mWood, 0, 0, null);
            canvas.drawBitmap(bitmap1, xPosition1, yPosition, null);

            invalidate();

        }
    }

// END of class
};

И XML:





  

   

  


 


Ответы на вопрос(2)

Ваш ответ на вопрос