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 #include "hwui/Paint.h"
20 #include <SkBlendMode.h>
21 #include <SkGradientShader.h>
22 
23 class ListOfFadedTextAnimation;
24 
25 static TestScene::Registrar _ListOfFadedTextAnimation(TestScene::Info{
26         "fadingedges",
27         "A mock ListView of scrolling text with faded edge. Doesn't re-bind/re-record views"
28         "as they are recycled, so won't upload much content (either glyphs, or bitmaps).",
29         TestScene::simpleCreateScene<ListOfFadedTextAnimation>});
30 
31 class ListOfFadedTextAnimation : public TestListViewSceneBase {
createListItem(RenderProperties & props,Canvas & canvas,int id,int itemWidth,int itemHeight)32     void createListItem(RenderProperties& props, Canvas& canvas, int id, int itemWidth,
33                         int itemHeight) override {
34         canvas.drawColor(Color::White, SkBlendMode::kSrcOver);
35         int length = dp(100);
36         canvas.saveLayer(0, 0, length, itemHeight, nullptr);
37         Paint textPaint;
38         textPaint.getSkFont().setSize(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         sk_sp<SkShader> s(
48                 SkGradientShader::MakeLinear(pts, colors, NULL, 2, SkTileMode::kClamp));
49 
50         SkMatrix matrix;
51         matrix.setScale(1, length);
52         matrix.postRotate(-90);
53         Paint fadingPaint;
54         fadingPaint.setShader(s->makeWithLocalMatrix(matrix));
55         fadingPaint.setBlendMode(SkBlendMode::kDstOut);
56         canvas.drawRect(0, 0, length, itemHeight, fadingPaint);
57         canvas.restore();
58     }
59 };
60