Search code examples
javaandroidcanvasbitmapgame-physics

Draw bitmap inside of growing Arc


I have 1 question and 1 problem.

I want an expanding arc drawn in my game and so far Ive managed to make it gradually expand, but after a certain point the arc stops expanding and just dissapears, why is that?

Here is my code for the arc:

public void render(Canvas canvas) {
    if(gameState == TESTMODE){
        drawMainBg(canvas);
        hModeRadius += 50*thread.getDelta();
        Paint p = new Paint();
        RectF rectF = new RectF(hModeY - hModeRadius, hModeX + hModeRadius, hModeX + hModeRadius,  hModeY-hModeRadius);
        p.setColor(Color.WHITE);
        canvas.drawArc (rectF, 0, 360, true, p);
    }

Now my second, and main, question is, is there any way to fill this arc with a bitmap? I want to use this arc to change my background in the game and I want to background to expand as an arc until it fills the screen.


Solution

  • You can use a BitmapShader for this:

    // Define a method to generate a bitmap shader from a drawable resource
    private static Shader getDrawableShader(Context ctx, int resId) {
        Bitmap bmp = BitmapFactory.decodeResource(ctx.getResources(), resId);
        return new BitmapShader(bmp, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
    }
    
    // Outside of onDraw(), preferably on a background thread if possible
    Shader shader = getDrawableShader(getContext(), R.drawable.my_image);
    
    // In onDraw()
    paint.setShader(shader);
    canvas.drawArc(rectF, 0, 360, true, paint);