所以我基本上是从这个项目中复制代码来玩它: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();
}
}