1

所以我基本上是从这个项目中复制代码来玩它:http ://code.google.com/p/android-30days-apps/source/browse/trunk/08day/src/com/bakhtiyor/android /snowfall/SnowFall.java?r=27

但我遇到了一个问题。就行 drawables.add(new AnimateDrawable(snow_flake, animation)); 我收到 AnimateDrawable 无法解析为类型的错误消息。我环顾四周,在原始的 Android 函数中,drawable 和动画作为构造函数之一传递。对于某种配置,我是否缺少某些东西?

这是我正在使用的所有代码(我使用的是cherry_blossom而不是雪花,但这是唯一的主要区别)

private class CherryBlossomView extends View
{
    private int cherry_blossom_count = 15;
    private final List<Drawable> drawables = new ArrayList<Drawable>();
    private int [][] coords;
    private final Drawable cherry_blossom;

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

        cherry_blossom = context.getResources().getDrawable(R.drawable.blossom_petal);
        cherry_blossom.setBounds(0, 0, cherry_blossom.getIntrinsicWidth(), cherry_blossom.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();

        cherry_blossom_count = Math.max(width, height) / 20;
        coords = new int[cherry_blossom_count][];
        drawables.clear();
        for(int i = 0; i < cherry_blossom_count; i++)
        {
            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(cherry_blossom, animation));
            animation.setStartOffset(random.nextInt(20 * height));
            animation.startNow();
        }
    }

    @Override
    protected void onDraw(Canvas canvas)
    {
        for(int i = 0; i < cherry_blossom_count; i++)
        {
            Drawable drawable = drawables.get(i);
            canvas.save();
            canvas.translate(coords[i][0], coords[i][1]);
            drawable.draw(canvas);
            canvas.restore();
        }
        invalidate();
    }
}
4

1 回答 1

2

如果您单击链接到的代码上方的包, 在此处输入图像描述

您会看到它显示了更多文件。 在此处输入图像描述

动画绘制:

/*
 * Copyright (C) 2008 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.bakhtiyor.android.snowfall;

import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.Transformation;

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);
        }
    }
}
于 2013-03-03T17:11:18.267 回答