ListViewAnimation.cpp revision 06a62f6f5379aa5f11f6da3e7ee066e9f640b91a
1/*
2 * Copyright (C) 2015 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#include "TestSceneBase.h"
18#include "tests/common/TestListViewSceneBase.h"
19
20#include <cstdio>
21
22class ListViewAnimation;
23
24static TestScene::Registrar _ListView(TestScene::Info{
25    "listview",
26    "A mock ListView of scrolling content. Doesn't re-bind/re-record views as they are recycled, so"
27    "won't upload much content (either glyphs, or bitmaps).",
28    TestScene::simpleCreateScene<ListViewAnimation>
29});
30
31class ListViewAnimation : public TestListViewSceneBase {
32    SkBitmap createRandomCharIcon(int cardHeight) {
33        int size = cardHeight - (dp(10) * 2);
34        SkBitmap bitmap = TestUtils::createSkBitmap(size, size);
35        SkCanvas canvas(bitmap);
36        canvas.clear(0);
37
38        SkPaint paint;
39        paint.setAntiAlias(true);
40        SkColor randomColor = BrightColors[rand() % BrightColorsCount];
41        paint.setColor(randomColor);
42        canvas.drawCircle(size / 2, size / 2, size / 2, paint);
43
44        bool bgDark = SkColorGetR(randomColor) + SkColorGetG(randomColor) + SkColorGetB(randomColor)
45                < 128 * 3;
46        paint.setColor(bgDark ? Color::White : Color::Grey_700);
47        paint.setTextAlign(SkPaint::kCenter_Align);
48        paint.setTextSize(size / 2);
49        char charToShow = 'A' + (rand() % 26);
50        const SkPoint pos[] = {{
51                SkIntToScalar(size / 2),
52                /*approximate centering*/ SkFloatToScalar(size * 0.7f)}};
53        canvas.drawPosText(&charToShow, 1, pos, paint);
54        return bitmap;
55    }
56
57    static SkBitmap createBoxBitmap(bool filled) {
58        int size = dp(20);
59        int stroke = dp(2);
60        SkBitmap bitmap = TestUtils::createSkBitmap(size, size);
61        SkCanvas canvas(bitmap);
62        canvas.clear(Color::Transparent);
63
64        SkPaint paint;
65        paint.setAntiAlias(true);
66        paint.setColor(filled ? Color::Yellow_500 : Color::Grey_700);
67        paint.setStyle(filled ? SkPaint::kStrokeAndFill_Style : SkPaint::kStroke_Style);
68        paint.setStrokeWidth(stroke);
69        canvas.drawRect(SkRect::MakeLTRB(stroke, stroke, size - stroke, size - stroke), paint);
70        return bitmap;
71    }
72
73    void createListItem(RenderProperties& props, TestCanvas& canvas, int cardId,
74            int itemWidth, int itemHeight) override {
75        static SkBitmap filledBox = createBoxBitmap(true);
76        static SkBitmap strokedBox = createBoxBitmap(false);
77        // TODO: switch to using round rect clipping, once merging correctly handles that
78        SkPaint roundRectPaint;
79        roundRectPaint.setAntiAlias(true);
80        roundRectPaint.setColor(Color::White);
81        canvas.drawRoundRect(0, 0, itemWidth, itemHeight, dp(6), dp(6), roundRectPaint);
82
83        SkPaint textPaint;
84        textPaint.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
85        textPaint.setColor(rand() % 2 ? Color::Black : Color::Grey_500);
86        textPaint.setTextSize(dp(20));
87        textPaint.setAntiAlias(true);
88        char buf[256];
89        snprintf(buf, sizeof(buf), "This card is #%d", cardId);
90        TestUtils::drawUtf8ToCanvas(&canvas, buf, textPaint, itemHeight, dp(25));
91        textPaint.setTextSize(dp(15));
92        TestUtils::drawUtf8ToCanvas(&canvas, "This is some more text on the card", textPaint,
93                itemHeight, dp(45));
94
95        canvas.drawBitmap(createRandomCharIcon(itemHeight), dp(10), dp(10), nullptr);
96
97        const SkBitmap& boxBitmap = rand() % 2 ? filledBox : strokedBox;
98        canvas.drawBitmap(boxBitmap, itemWidth - dp(10) - boxBitmap.width(), dp(10), nullptr);
99    }
100};
101