zgz345

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

根据Android源码修改,具有微信彩蛋效果

主要代码

    public static class Board extends FrameLayout {
        public static final boolean FIXED_STARS = true;

        // 控制数量
        public static final int NUM_CATS = 30;

        static Random sRNG = new Random();

        static float lerp(float a, float b, float f) {
            return (b - a) * f + a;
        }

        static float randfrange(float a, float b) {
            return lerp(a, b, sRNG.nextFloat());
        }

        static int randsign() {
            return sRNG.nextBoolean() ? 1 : -1;
        }

        static <E> E pick(E[] array) {
            if (array.length == 0)
                return null;
            return array[sRNG.nextInt(array.length)];
        }

        public class FlyingCat extends ImageView {
            public static final float VMAX = 300.0f;

            public static final float VMIN = 100.0f;

            public float v, vr;

            public float dist;

            public float z;

            public ComponentName component;

            public FlyingCat(Context context, AttributeSet as) {
                super(context, as);
                setImageResource(R.drawable.star_anim);
            }

            public void reset() {
                final float scale = lerp(0.5f, 1.5f, z);
                setScaleX(scale);
                setScaleY(scale);
                // setX(-scale * getWidth() + 1);
                setX(randfrange(0, Board.this.getHeight() - scale * getHeight()));
                setY(randfrange(0, Board.this.getHeight() - scale * getHeight()));
                v = lerp(VMIN, VMAX, z);
                dist = 0;
            }

            public void update(float dt) {
                dist += v * dt;
                // setX(getX() + v * dt);
                // 根据Y轴漂移
                setY(getY() + v * dt);
            }
        }

        TimeAnimator mAnim;

        Context mContext;

        public Board(Context context, AttributeSet as) {
            super(context, as);
            this.mContext = context;
        }

        private void reset() {
            removeAllViews();
            final ViewGroup.LayoutParams wrap = new ViewGroup.LayoutParams(
                    ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            for (int i = 0; i < NUM_CATS; i++) {
                FlyingCat nv = new FlyingCat(getContext(), null);
                addView(nv, wrap);
                nv.z = ((float) i / NUM_CATS);
                nv.z *= nv.z;
                nv.reset();
                nv.setX(randfrange(0, Board.this.getWidth()));
                final AnimationDrawable anim = (AnimationDrawable) nv.getDrawable();
                postDelayed(new Runnable() {
                    public void run() {
                        anim.start();
                    }
                }, (int) randfrange(0, 1000));
            }
            if (mAnim != null) {
                mAnim.cancel();
            }
            mAnim = new TimeAnimator();
            mAnim.setTimeListener(new TimeAnimator.TimeListener() {
                public void onTimeUpdate(TimeAnimator animation, long totalTime, long deltaTime) {
                    for (int i = 0; i < getChildCount(); i++) {
                        View v = getChildAt(i);
                        if (!(v instanceof FlyingCat))
                            continue;
                        FlyingCat nv = (FlyingCat) v;
                        nv.update(deltaTime / 200f);
                    }
                }
            });
        }

        @Override
        protected void onSizeChanged(int w, int h, int oldw, int oldh) {
            super.onSizeChanged(w, h, oldw, oldh);
            post(new Runnable() {
                public void run() {
                    reset();
                    mAnim.start();
                }
            });
        }

        @Override
        protected void onDetachedFromWindow() {
            super.onDetachedFromWindow();
            mAnim.cancel();
        }

        @Override
        public boolean isOpaque() {
            return true;
        }
    }

 少写了一行,源码下载地址:http://download.csdn.net/detail/zgz345/8247563

posted on 2014-12-15 10:12  zonyZhang  阅读(1780)  评论(1编辑  收藏  举报