所以我基本上是从这个项目中复制代码来玩它: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和animation作为构造函数之一传递。对于某种配置,我有什么遗漏吗?
这是我正在使用的所有代码(我使用的是cherry_blossom而不是snow flake,但这是唯一的主要区别)
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();
}
}
答案 0 :(得分:2)
如果您点击package above that code you linked to,
你会看到它显示更多文件。
AnimateDrawable:
/*
* 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);
}
}
}