ListOfFadedTextAnimation.cpp revision 06152cdd06da50762716cd455dcf7ab0117f25b0
194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood/*
294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood * Copyright (C) 2016 The Android Open Source Project
394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood *
419803807cd7ae01868fcfa50305f4a7dd13765e2Jeff Sharkey * Licensed under the Apache License, Version 2.0 (the "License");
519803807cd7ae01868fcfa50305f4a7dd13765e2Jeff Sharkey * you may not use this file except in compliance with the License.
619803807cd7ae01868fcfa50305f4a7dd13765e2Jeff Sharkey * You may obtain a copy of the License at
794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood *
819803807cd7ae01868fcfa50305f4a7dd13765e2Jeff Sharkey *      http://www.apache.org/licenses/LICENSE-2.0
994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood *
1019803807cd7ae01868fcfa50305f4a7dd13765e2Jeff Sharkey * Unless required by applicable law or agreed to in writing, software
1119803807cd7ae01868fcfa50305f4a7dd13765e2Jeff Sharkey * distributed under the License is distributed on an "AS IS" BASIS,
1219803807cd7ae01868fcfa50305f4a7dd13765e2Jeff Sharkey * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1319803807cd7ae01868fcfa50305f4a7dd13765e2Jeff Sharkey * See the License for the specific language governing permissions and
1494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood * limitations under the License.
1594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood */
1694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
1702d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe#include "TestSceneBase.h"
18bd558d61871218f5b13df2fe4b7cc3b530ee947cStephen Smalley#include "tests/common/TestListViewSceneBase.h"
19bd558d61871218f5b13df2fe4b7cc3b530ee947cStephen Smalley
2002d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe#include <SkGradientShader.h>
21c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey
2202d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampeclass ListOfFadedTextAnimation;
2302d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe
2402d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampestatic TestScene::Registrar _ListOfFadedTextAnimation(TestScene::Info{
2502d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe    "fadingedges",
2602d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe    "A mock ListView of scrolling text with faded edge. Doesn't re-bind/re-record views"
2702d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe    "as they are recycled, so won't upload much content (either glyphs, or bitmaps).",
2802d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe    TestScene::simpleCreateScene<ListOfFadedTextAnimation>
2902d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe});
3002d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe
3102d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampeclass ListOfFadedTextAnimation : public TestListViewSceneBase {
3202d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe    void createListItem(RenderProperties& props, Canvas& canvas, int id,
3302d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe            int itemWidth, int itemHeight)  override {
3402d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe        canvas.drawColor(Color::White, SkXfermode::kSrcOver_Mode);
3502d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe        int length = dp(100);
3602d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe        canvas.saveLayer(0, 0, length, itemHeight, nullptr, SaveFlags::HasAlphaLayer);
3702d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe        SkPaint textPaint;
3802d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe        textPaint.setTextSize(dp(20));
3902d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe        textPaint.setAntiAlias(true);
4002d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe        TestUtils::drawUtf8ToCanvas(&canvas, "not that long long text", textPaint, dp(10), dp(30));
4102d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe
4202d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe        SkPoint pts[2];
4394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        pts[0].set(0, 0);
4494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        pts[1].set(0, 1);
4588ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov
4694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        SkColor colors[2] = {Color::Black, Color::Transparent};
4794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        SkAutoTUnref<SkShader> s(SkGradientShader::CreateLinear(pts, colors, NULL, 2,
4802d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe                SkShader::kClamp_TileMode));
4902d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe
5002d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe        SkMatrix matrix;
5102d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe        matrix.setScale(1, length);
5202d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe        matrix.postRotate(-90);
5302d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe        SkPaint fadingPaint;
5402d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe        fadingPaint.setShader(s->newWithLocalMatrix(matrix))->unref();
5502d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe        SkXfermode* mode = SkXfermode::Create(SkXfermode::kDstOut_Mode);
5602d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe        fadingPaint.setXfermode(mode);
5702d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe        canvas.drawRect(0, 0, length, itemHeight, fadingPaint);
5802d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe        canvas.restore();
5902d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe    }
6002d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe};
6102d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe