1 /*
2  * Copyright (C) 2013 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.keyframeanimation;
18 
19 import android.app.Activity;
20 import android.graphics.Bitmap;
21 import android.graphics.Canvas;
22 import android.graphics.Color;
23 import android.graphics.Paint;
24 import android.graphics.drawable.AnimationDrawable;
25 import android.graphics.drawable.BitmapDrawable;
26 import android.os.Bundle;
27 import android.view.View;
28 import android.widget.ImageView;
29 
30 /**
31  * This example shows how to use AnimationDrawable to construct a keyframe animation where each
32  * frame is shown for a specified duration.
33  *
34  * Watch the associated video for this demo on the DevBytes channel of developer.android.com
35  * or on YouTube at https://www.youtube.com/watch?v=V3ksidLf7vA.
36  */
37 public class KeyframeAnimation extends Activity {
38 
39     @Override
onCreate(Bundle savedInstanceState)40     public void onCreate(Bundle savedInstanceState) {
41         super.onCreate(savedInstanceState);
42         setContentView(R.layout.activity_keyframe_animation);
43 
44         ImageView imageview = (ImageView) findViewById(R.id.imageview);
45 
46         // Create the AnimationDrawable in which we will store all frames of the animation
47         final AnimationDrawable animationDrawable = new AnimationDrawable();
48         for (int i = 0; i < 10; ++i) {
49             animationDrawable.addFrame(getDrawableForFrameNumber(i), 300);
50         }
51         // Run until we say stop
52         animationDrawable.setOneShot(false);
53 
54         imageview.setImageDrawable(animationDrawable);
55 
56         // When the user clicks on the image, toggle the animation on/off
57         imageview.setOnClickListener(new View.OnClickListener() {
58 
59             @Override
60             public void onClick(View v) {
61                 if (animationDrawable.isRunning()) {
62                     animationDrawable.stop();
63                 } else {
64                     animationDrawable.start();
65                 }
66             }
67         });
68     }
69 
70     /**
71      * The 'frames' in this app are nothing more than a gray background with text indicating
72      * the number of the frame.
73      */
getDrawableForFrameNumber(int frameNumber)74     private BitmapDrawable getDrawableForFrameNumber(int frameNumber) {
75         Bitmap bitmap = Bitmap.createBitmap(400, 400, Bitmap.Config.ARGB_8888);
76         Canvas canvas = new Canvas(bitmap);
77         canvas.drawColor(Color.GRAY);
78         Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
79         paint.setTextSize(80);
80         paint.setColor(Color.BLACK);
81         canvas.drawText("Frame " + frameNumber, 40, 220, paint);
82         return new BitmapDrawable(getResources(), bitmap);
83     }
84 
85 }
86