1 /* 2 * Copyright (C) 2008 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.example.android.apis.graphics; 18 19 import com.example.android.apis.R; 20 21 import android.content.Context; 22 import android.graphics.Canvas; 23 import android.graphics.Color; 24 import android.graphics.drawable.Drawable; 25 import android.os.Bundle; 26 import android.view.View; 27 import android.view.animation.Animation; 28 import android.view.animation.TranslateAnimation; 29 30 public class AnimateDrawables extends GraphicsActivity { 31 32 @Override 33 protected void onCreate(Bundle savedInstanceState) { 34 super.onCreate(savedInstanceState); 35 setContentView(new SampleView(this)); 36 } 37 38 private static class SampleView extends View { 39 private AnimateDrawable mDrawable; 40 41 public SampleView(Context context) { 42 super(context); 43 setFocusable(true); 44 setFocusableInTouchMode(true); 45 46 Drawable dr = context.getResources().getDrawable(R.drawable.beach); 47 dr.setBounds(0, 0, dr.getIntrinsicWidth(), dr.getIntrinsicHeight()); 48 49 Animation an = new TranslateAnimation(0, 100, 0, 200); 50 an.setDuration(2000); 51 an.setRepeatCount(-1); 52 an.initialize(10, 10, 10, 10); 53 54 mDrawable = new AnimateDrawable(dr, an); 55 an.startNow(); 56 } 57 58 @Override 59 protected void onDraw(Canvas canvas) { 60 canvas.drawColor(Color.WHITE); 61 62 mDrawable.draw(canvas); 63 invalidate(); 64 } 65 } 66 } 67 68