How can I animate the opacity of a bitmap using Canvas in Android?

I am new to Android programming. I am trying to create a bitmap animation using Canvasin Android. I use a setAlpha()bitmap to control the opacity. My method drawFrame()includes the following bit:

c = holder.lockCanvas();  
drawScene(c, paint);  
holder.unlockCanvasAndPost(c); 

My drawScene()includes this bit:

Paint transparencyValue = new Paint();  
transparencyValue.setAlpha(paint);
canvas.drawBitmap(boom.getImage(), logoToBoom.getX(), logoToBoom.getY(),
    transparencyValue);

I assume I need to insert a loop to change paintfrom 0 to 255 and back off. So far this has not worked, but I'm probably doing something wrong. Can anyone recommend something?

EDIT . Here is my code for Runnable. paint- this private double, set to 255. boom_activated- this boolean, which becomes true, if permitted onTouchEvent. It should remain trueuntil Runnableit disconnects it ( setBoomState(false);). For some reason, it still does not draw a bitmap while reducing opacity. Is the code below valid or am I missing something?

    private final Runnable DrawSceneThread = new Runnable() {
        public void run() {
            if (boom_activated && paint <= 0) {
                paint = 0;
                drawFrame();
                setBoomState(false);
                paint = 255;
            } else if (boom_activated && paint >= 0) {
                drawFrame();
                paint -= 0.7;
            } else {
                drawFrame();
            }`

In mine drawScene()I have this line:
              scene_handler.postDelayed(DrawSceneThread, 25);

+3
source share
2 answers

. ?

, Paint paint drawScene , . .

0

All Articles