だから私は基本的にこのプロジェクトからコードをコピーして遊んでいます: 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 関数では、ドローアブルとアニメーションがコンストラクターの 1 つとして渡されます。ある種の構成に欠けているものはありますか?
これが私が使用しているすべてのコードです(私はスノーフレークの代わりに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();
}
}