Subversion Repositories AndroidProjects

Rev

Rev 6 | Blame | Compare with Previous | Last modification | View Log | RSS feed

package com.gebauz.pingK;

import com.gebauz.pingK.entities.Paddle;

import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffColorFilter;

public class ScoreDisplay {
       
        Bitmap mScore;
        Bitmap mOwnGoal;
       
        boolean isOwnGoal = false;
        float animTime;
        float totalAnimTime;
        int scoringPlayer = 0;
       
        public ScoreDisplay(Resources resources) {
                mScore = BitmapFactory.decodeResource(resources, R.drawable.score);
                mOwnGoal = BitmapFactory.decodeResource(resources, R.drawable.owngoal);
        }
       
        public void startAnimation(boolean ownGoal, float animationTime, int player) {
                isOwnGoal = ownGoal;
                animTime = animationTime;
                totalAnimTime = animationTime;
                scoringPlayer = player;
        }
       
        public void update(float deltaTime) {
                animTime -= deltaTime;                         
        }
       
        public void render(Canvas canvas) {
                if (animTime < 0.0f)
                        return;
               
                Paint paint = new Paint();
               
                Bitmap which = mScore;
                if (isOwnGoal)
                        which = mOwnGoal;
               
                int alpha = 255;
                if (animTime < 1.0f)
                        alpha = (int)((float)animTime * 255.0f);
               
                int color = Color.argb(alpha, 255, 0, 153);
                paint.setColor(color);
                paint.setColorFilter(new PorterDuffColorFilter(color, PorterDuff.Mode.MULTIPLY));
               
                float t = (animTime/totalAnimTime);
                t = t*t;
                float s = 1.0f + t;            
               
                Matrix scale = new Matrix();
                scale.postScale(s, s, which.getWidth()/2, which.getHeight()/2);
               
                Matrix rotate = new Matrix();
                if (isOwnGoal) {
                        if (scoringPlayer == Paddle.PADDLE_TOP)
                                rotate.postRotate(-t * 15.0f, which.getWidth()/2, which.getHeight()/2);
                        else
                                rotate.postRotate(180.0f + (t * 15.0f), which.getWidth()/2, which.getHeight()/2);                      
                } else {
                        if (scoringPlayer == Paddle.PADDLE_TOP)
                                rotate.postRotate(180.0f + (t * 15.0f), which.getWidth()/2, which.getHeight()/2);
                        else
                                rotate.postRotate(-t * 15.0f, which.getWidth()/2, which.getHeight()/2);
                }
               
                Matrix translate = new Matrix();
                translate.setTranslate((canvas.getWidth() - which.getWidth())/2, (canvas.getHeight() - which.getHeight())/2);
               
                Matrix matrix = new Matrix();
                matrix.postConcat(scale);
                matrix.postConcat(rotate);
                matrix.postConcat(translate);
               
                canvas.drawBitmap(which, matrix, paint);
        }
}