如何在android中为位图制作雪落运动

时间:2014-03-13 09:50:58

标签: java android bitmap

我正在制作一个应用程序,其中一个雪的位图会掉下来。它会在屏幕上随机移动一个位置并下降到一定距离。我可以让雪落到一定距离并消失在那里。但我需要雪在开始时显得模糊,逐渐显得好看,最终在屏幕上运动时显得模糊和消失。(意味着我们知道雪是如何下降的。用同样的方式,用户应该很容易理解雪的起点和终点秋季)。请帮我做雪地运动。

import java.util.Random;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.view.animation.TranslateAnimation;

public class Leaf { 
    private float x;
    private float rawX;
    private float y;
    private float speedY;
    private int heightBound;
    private Bitmap bitmap;  
    private float bounceOffset;
    private boolean touched;
    private float xOffset,yOffset;
    private int width;
    private int height;
    float angle;
    float randomx,randomy,randomy1;

public Leaf(Bitmap source, int canvasHeight, int canvasWidth){      
    Random rand = new Random();     
    randomx=rand.nextFloat();
    randomy1=rand.nextFloat();

    speedY = 1 + rand.nextFloat() * 3;      
    //float scaleRate = rand.nextFloat() * 0.5f;
    //if(scaleRate <= 0.01){
        //scaleRate = 0.1f;
    //}
    width = (int)(source.getWidth()); //* scaleRate);
    height = (int)(source.getHeight());// * scaleRate);
    bitmap = Bitmap.createScaledBitmap(source, width, height, true);        
    rawX = rand.nextFloat() * canvasWidth;  
    x = rawX;
    randomy=1+rand.nextFloat()*canvasHeight;
    y = -bitmap.getHeight();        
    heightBound = canvasHeight + bitmap.getHeight();    
    bounceOffset = 8.0f;
    touched = false;
    xOffset = rand.nextFloat();
    if(xOffset >= 1.5){
        xOffset = xOffset - 3;}

    }       


public float getX() {
    return x;
}

public void setX(float x) {
    this.x = x;
}

public float getY() {
    return y;
}

public void setY(float y) {
    this.y = y;
}

public float getSpeedY() {
    return speedY;
}

public void setSpeedY(float speedY) {
    this.speedY = speedY;
}   

public boolean isTouched(){
    return touched;
}

public void setTouched(boolean flag){
    this.touched = flag;
}

public Bitmap getBitmap() {
    return bitmap;
}

public void setBitmap(Bitmap flag) {
    this.bitmap = flag;
}

public void drawLeaf(Canvas canvas, Paint paint){
    Matrix matrix = new Matrix();
    matrix.reset();
    matrix.postTranslate(x+2,randomy+2); // Centers image
    matrix.postRotate(angle);
    float px=x+bitmap.getHeight()/2;
    float py=y+bitmap.getWidth()/2;
    matrix.postTranslate(px, py);
    canvas.drawBitmap(bitmap, matrix, paint);
    //canvas.drawBitmap(bitmap,x,randomy, paint);


}   

public void handleFalling(boolean fallingDown){
    if(fallingDown){
        this.y += this.speedY;  
        this.x += this.xOffset;

        angle=angle+0.5f;
        if(this.y >= (5*randomy1)+(5*bitmap.getHeight())){
            this.y = -this.bitmap.getHeight();
            this.x = rawX;

        }
    }else{

        this.y -= this.speedY;
        this.x += this.xOffset;
        angle=angle+0.4f;
        if(this.y <= -this.bitmap.getHeight()){
            this.y += 6*this.bitmap.getHeight();
            this.x = rawX;

        }
    }

}

public void handleTouched(float touchX, float touchY){          
    float centerX = this.x + this.bitmap.getWidth() / 2.0f;
    float centerY = this.y + this.bitmap.getHeight() / 2.0f;

    if(centerX <= touchX){
        this.x -= this.bounceOffset;
        if(centerY <= touchY){
            this.y -= this.bounceOffset;
        }else{
            this.y += this.bounceOffset;
        }

    }else{
        this.x += this.bounceOffset;
        if(centerY <= touchY){
            this.y -= this.bounceOffset;
        }else{
            this.y += this.bounceOffset;
        }
    }

    this.bounceOffset *= 0.9;
    if(this.bounceOffset < 1.0){
        this.bounceOffset = 8.0f;
        touched = false;

        }   

    }

}

这里的叶子意味着雪。这个类用于在handlefalling方法()中使叶子失效。而drawleaf()用于使用矩阵使叶子平移。

0 个答案:

没有答案