ListOfFadedTextAnimation.cpp revision 06a62f6f5379aa5f11f6da3e7ee066e9f640b91a
1/*
2 * Copyright (C) 2016 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 <SkGradientShader.h>
21
22class ListOfFadedTextAnimation;
23
24static TestScene::Registrar _ListOfFadedTextAnimation(TestScene::Info{
25    "fadingedges",
26    "A mock ListView of scrolling text with faded edge. Doesn't re-bind/re-record views"
27    "as they are recycled, so won't upload much content (either glyphs, or bitmaps).",
28    TestScene::simpleCreateScene<ListOfFadedTextAnimation>
29});
30
31class ListOfFadedTextAnimation : public TestListViewSceneBase {
32    void createListItem(RenderProperties& props, TestCanvas& canvas, int id,
33            int itemWidth, int itemHeight)  override {
34        canvas.drawColor(Color::White, SkXfermode::kSrcOver_Mode);
35        int length = dp(100);
36        canvas.saveLayer(0, 0, length, itemHeight, nullptr, SaveFlags::HasAlphaLayer);
37        SkPaint textPaint;
38        textPaint.setTextSize(dp(20));
39        textPaint.setAntiAlias(true);
40        TestUtils::drawUtf8ToCanvas(&canvas, "not that long long text", textPaint, dp(10), dp(30));
41
42        SkPoint pts[2];
43        pts[0].set(0, 0);
44        pts[1].set(0, 1);
45
46        SkColor colors[2] = {Color::Black, Color::Transparent};
47        SkAutoTUnref<SkShader> s(SkGradientShader::CreateLinear(pts, colors, NULL, 2,
48                SkShader::kClamp_TileMode));
49
50        SkMatrix matrix;
51        matrix.setScale(1, length);
52        matrix.postRotate(-90);
53        SkPaint fadingPaint;
54        fadingPaint.setShader(s->newWithLocalMatrix(matrix))->unref();
55        SkXfermode* mode = SkXfermode::Create(SkXfermode::kDstOut_Mode);
56        fadingPaint.setXfermode(mode);
57        canvas.drawRect(0, 0, length, itemHeight, fadingPaint);
58        canvas.restore();
59    }
60};
61