Android Studio screen limits

0

I have a Bitmap on the canvas that I move around the screen with an On Touch event. The question is, how can I make the image I move not to leave the screen? Type set some limits where you can move it and hit the edge of the screen and do not let me pass. Thanks

This is the code I have:

    public class Juego extends View implements View.OnTouchListener{

    Bitmap super_esfera;
    int esferaX = 200;
    int esferaY = 200;

    public Juego(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.setOnTouchListener(this);
        setFocusable(true);

        super_esfera = BitmapFactory.decodeResource(getResources(), R.drawable.super_esfera);

    }

    public void onDraw(Canvas canvas){
        Paint paint = new Paint();

        Bitmap indexcanvas = Bitmap.createScaledBitmap(super_esfera, 200, 200, true);

        canvas.drawBitmap(indexcanvas, esferaX, esferaY, paint);

    }

    public boolean onTouch(View view, MotionEvent event) {
        esferaX = (int)event.getX()-100;
        esferaY = (int)event.getY()-100;

        invalidate();
        return true;
    }
}
    
asked by Agustin Val 17.06.2017 в 18:18
source

0 answers