1/*
2 * Copyright (C) 2014 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
17package com.android.test.hwui;
18
19import android.app.Activity;
20import android.content.Context;
21import android.graphics.Canvas;
22import android.graphics.CanvasProperty;
23import android.graphics.Paint;
24import android.graphics.Paint.Style;
25import android.os.Bundle;
26import android.os.Trace;
27import android.view.DisplayListCanvas;
28import android.view.RenderNodeAnimator;
29import android.view.View;
30import android.widget.LinearLayout;
31import android.widget.LinearLayout.LayoutParams;
32import android.widget.ProgressBar;
33
34import java.util.ArrayList;
35
36public class CirclePropActivity extends Activity {
37    @Override
38    protected void onCreate(Bundle savedInstanceState) {
39        super.onCreate(savedInstanceState);
40
41        final LinearLayout layout = new LinearLayout(this);
42        layout.setOrientation(LinearLayout.VERTICAL);
43
44        ProgressBar spinner = new ProgressBar(this, null, android.R.attr.progressBarStyleLarge);
45        layout.addView(spinner, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
46        // For testing with a functor in the tree
47//        WebView wv = new WebView(this);
48//        wv.setWebViewClient(new WebViewClient());
49//        wv.setWebChromeClient(new WebChromeClient());
50//        wv.loadUrl("http://theverge.com");
51//        layout.addView(wv, new LayoutParams(LayoutParams.MATCH_PARENT, 100));
52
53        layout.addView(new CircleView(this),
54                new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
55
56        setContentView(layout);
57    }
58
59    static class CircleView extends View {
60        static final int DURATION = 500;
61
62        private boolean mToggle = false;
63        ArrayList<RenderNodeAnimator> mRunningAnimations = new ArrayList<RenderNodeAnimator>();
64
65        CanvasProperty<Float> mX;
66        CanvasProperty<Float> mY;
67        CanvasProperty<Float> mRadius;
68        CanvasProperty<Paint> mPaint;
69
70        CircleView(Context c) {
71            super(c);
72            setClickable(true);
73
74            mX = CanvasProperty.createFloat(200.0f);
75            mY = CanvasProperty.createFloat(200.0f);
76            mRadius = CanvasProperty.createFloat(150.0f);
77
78            Paint p = new Paint();
79            p.setAntiAlias(true);
80            p.setColor(0xFFFF0000);
81            p.setStyle(Style.STROKE);
82            p.setStrokeWidth(60.0f);
83            mPaint = CanvasProperty.createPaint(p);
84        }
85
86        @Override
87        protected void onDraw(Canvas canvas) {
88            super.onDraw(canvas);
89
90            if (canvas.isHardwareAccelerated()) {
91                DisplayListCanvas displayListCanvas = (DisplayListCanvas) canvas;
92                displayListCanvas.drawCircle(mX, mY, mRadius, mPaint);
93            }
94        }
95
96        @Override
97        public boolean performClick() {
98            for (int i = 0; i < mRunningAnimations.size(); i++) {
99                mRunningAnimations.get(i).cancel();
100            }
101            mRunningAnimations.clear();
102
103            mToggle = !mToggle;
104
105            mRunningAnimations.add(new RenderNodeAnimator(
106                    mX, mToggle ? 400.0f : 200.0f));
107
108            mRunningAnimations.add(new RenderNodeAnimator(
109                    mY, mToggle ? 600.0f : 200.0f));
110
111            mRunningAnimations.add(new RenderNodeAnimator(
112                    mRadius, mToggle ? 250.0f : 150.0f));
113
114            mRunningAnimations.add(new RenderNodeAnimator(
115                    mPaint, RenderNodeAnimator.PAINT_STROKE_WIDTH,
116                    mToggle ? 5.0f : 60.0f));
117
118            mRunningAnimations.add(new RenderNodeAnimator(
119                    mPaint, RenderNodeAnimator.PAINT_ALPHA, 64.0f));
120
121            // Will be "chained" to run after the above
122            mRunningAnimations.add(new RenderNodeAnimator(
123                    mPaint, RenderNodeAnimator.PAINT_ALPHA, 255.0f));
124
125            for (int i = 0; i < mRunningAnimations.size(); i++) {
126                RenderNodeAnimator anim = mRunningAnimations.get(i);
127                anim.setDuration(1000);
128                anim.setTarget(this);
129                if (i == (mRunningAnimations.size() - 1)) {
130                    // "chain" test
131                    anim.setStartValue(64.0f);
132                    anim.setStartDelay(anim.getDuration());
133                }
134                anim.start();
135            }
136
137            if (mToggle) {
138                post(new Runnable() {
139                    @Override
140                    public void run() {
141                        Trace.traceBegin(Trace.TRACE_TAG_VIEW, "pretendBusy");
142                        try {
143                            Thread.sleep(DURATION);
144                        } catch (InterruptedException e) {
145                        }
146                        Trace.traceEnd(Trace.TRACE_TAG_VIEW);
147                    }
148                });
149            }
150            return true;
151        }
152    }
153}
154