1
\$\begingroup\$

I already got snow fall effect from here

the main activity for the snowfall

public class SnowFallActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        SnowFallView snowFallView = new SnowFallView(this);
        setContentView(snowFallView);
        snowFallView.setBackgroundDrawable(getResources().getDrawable(R.drawable.snow_bg));
    }

    private class SnowFallView extends View {
        private int snow_flake_count = 10;
        private final List<Drawable> drawables = new ArrayList<Drawable>();
        private int[][] coords;
        private final Drawable snow_flake;

        public SnowFallView(Context context) {
            super(context);
            setFocusable(true);
            setFocusableInTouchMode(true);

            snow_flake = context.getResources().getDrawable(R.drawable.snow_sprite_1);
            snow_flake.setBounds(0, 0, snow_flake.getIntrinsicWidth(), snow_flake
                        .getIntrinsicHeight());


        }

        @Override
        protected void onSizeChanged(int width, int height, int oldw, int oldh) {
            super.onSizeChanged(width, height, oldw, oldh);
            Random random = new Random();
            Interpolator interpolator = new LinearInterpolator();

            snow_flake_count = Math.max(width, height) / 10;
            coords = new int[snow_flake_count][];
            drawables.clear();
            for (int i = 0; i < snow_flake_count; i++) {
//                Log.e("sizeChange", " ang width = " + width + " the height = " + height);
                Animation animation = new TranslateAnimation(0, height / 10
                        - random.nextInt(height / 5), 0, height + 30);
                animation.setDuration(10 * height + random.nextInt(5 * height));
                animation.setRepeatCount(-1);
                animation.initialize(10, 10, 10, 10);
                animation.setInterpolator(interpolator);

                coords[i] = new int[] { random.nextInt(width - 30), -30 };

                drawables.add(new AnimateDrawable(snow_flake, animation));
                animation.setStartOffset(random.nextInt(20 * height));
                animation.startNow();
            }
        }

        @Override
        protected void onDraw(Canvas canvas) {
            for (int i = 0; i < snow_flake_count; i++) {
                Drawable drawable = drawables.get(i);
                canvas.save();
                canvas.translate(coords[i][0], coords[i][1]);
                drawable.draw(canvas);
                canvas.restore();
                Log.e("sizeChange", " ang width = " + coords[i][0] + " the height = " + coords[i][0]);
            }
            invalidate();
        }

    }
}

The AnimateDrawable Class

public class AnimateDrawable extends ProxyDrawable {

    private Animation mAnimation;
    private Transformation mTransformation = new Transformation();

    public AnimateDrawable(Drawable target) {
        super(target);
    }

    public AnimateDrawable(Drawable target, Animation animation) {
        super(target);
        mAnimation = animation;
    }

    public Animation getAnimation() {
        return mAnimation;
    }

    public void setAnimation(Animation anim) {
        mAnimation = anim;
    }

    public boolean hasStarted() {
        return mAnimation != null && mAnimation.hasStarted();
    }

    public boolean hasEnded() {
        return mAnimation == null || mAnimation.hasEnded();
    }

    @Override
    public void draw(Canvas canvas) {
        Drawable dr = getProxy();
        if (dr != null) {
            int sc = canvas.save();
            Animation anim = mAnimation;
            if (anim != null) {
                anim.getTransformation(
                        AnimationUtils.currentAnimationTimeMillis(),
                        mTransformation);
                canvas.concat(mTransformation.getMatrix());
            }
            dr.draw(canvas);
            canvas.restoreToCount(sc);
        }
    }
}

ProxyDrawable Class

public class ProxyDrawable extends Drawable {

    private Drawable mProxy;

    public ProxyDrawable(Drawable target) {
        mProxy = target;
    }

    public Drawable getProxy() {
        return mProxy;
    }

    public void setProxy(Drawable proxy) {
        if (proxy != this) {
            mProxy = proxy;
        }
    }

    @Override
    public void draw(Canvas canvas) {
        if (mProxy != null) {
            mProxy.draw(canvas);
        }
    }

    @Override
    public int getIntrinsicWidth() {
        return mProxy != null ? mProxy.getIntrinsicWidth() : -1;
    }

    @Override
    public int getIntrinsicHeight() {
        return mProxy != null ? mProxy.getIntrinsicHeight() : -1;
    }

    @Override
    public int getOpacity() {
        return mProxy != null ? mProxy.getOpacity() : PixelFormat.TRANSPARENT;
    }

    @Override
    public void setFilterBitmap(boolean filter) {
        if (mProxy != null) {
            mProxy.setFilterBitmap(filter);
        }
    }

    @Override
    public void setDither(boolean dither) {
        if (mProxy != null) {
            mProxy.setDither(dither);
        }
    }

    @Override
    public void setColorFilter(ColorFilter colorFilter) {
        if (mProxy != null) {
            mProxy.setColorFilter(colorFilter);
        }
    }

    @Override
    public void setAlpha(int alpha) {
        if (mProxy != null) {
            mProxy.setAlpha(alpha);
        }
    }
}

what i'm trying to achieve is to create different size of the snowflakes that im using..So far this is what i have tried to change the size of the snowflakes by creating a loop then using setbounds to a random number. Can someone help me to achieve my goal or point me to something easier to do this?Thanks in advance

 public SnowFallView(Context context) {
            super(context);
            setFocusable(true);
            setFocusableInTouchMode(true);

            snow_flake = context.getResources().getDrawable(R.drawable.snow_sprite_1);

            for(int i = 0; i < 5; i++){

                int width = snow_flake.getIntrinsicWidth();
                int height = snow_flake.getIntrinsicHeight() ;
                Random rand = new Random();
                if(i % 2 == 0){
                    width = width -rand.nextInt(5 - 1 + 1) + 1 - rand.nextInt(10);
                    height = height - rand.nextInt(5 - 1 + 1) + 1 - rand.nextInt(10);
                }else if(i % 2 == 1){
                    width = width + rand.nextInt(5 - 1 + 1) + 1;
                    height = height + rand.nextInt(5 - 1 + 1) + 1;
                }
                snow_flake.setBounds(0, 0, width, height);
            }

        }
\$\endgroup\$
1
  • \$\begingroup\$ Could this be narrowed down to "how do I scale up a Drawable"? Have you considered using Canvas.scale (as mentioned in the docs)? \$\endgroup\$
    – Anko
    Oct 23, 2015 at 18:10

1 Answer 1

1
\$\begingroup\$

Try adding the following to the SnowFallView function:

snow_flake2 = context.getResources().getDrawable(R.raw.snow_glow);
snow_flake2.setBounds(0, 0, (int)(snow_flake.getIntrinsicWidth()/2), (int)(snow_flake.getIntrinsicHeight()/2));

This line practically make another snow flake context with 1/2 the size of the original snow flake. U can add a few more with different sizes if you want. Next, add the following line in the onSizeChanged function:

    int y=0;
    y=random.nextInt(2);
    if(y==0)
      drawables.add(new AnimateDrawable(snow_flake, animation));
    else
      drawables.add(new AnimateDrawable(snow_flake2, animation));

This will randomly select between the larger snowflake or smaller snowflake to draw in the canvas. Then the rest of the code stays the same.

Hope this helps. Thanks.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .