SampleTextAlpha.cpp revision 8a1c16ff38322f0210116fa7293eb8817c7e477e
1#include "SampleCode.h"
2#include "SkView.h"
3#include "SkBlurMaskFilter.h"
4#include "SkCanvas.h"
5#include "SkGradientShader.h"
6#include "SkGraphics.h"
7#include "SkImageDecoder.h"
8#include "SkPath.h"
9#include "SkPorterDuff.h"
10#include "SkRandom.h"
11#include "SkRegion.h"
12#include "SkShader.h"
13#include "SkUtils.h"
14#include "SkXfermode.h"
15#include "SkShaderExtras.h"
16#include "SkColorPriv.h"
17#include "SkColorFilter.h"
18#include "SkTime.h"
19#include "SkTypeface.h"
20
21#include "SkImageRef.h"
22#include "SkOSFile.h"
23#include "SkStream.h"
24
25static void check_for_nonwhite(const SkBitmap& bm, int alpha) {
26    if (bm.config() != SkBitmap::kRGB_565_Config) {
27        return;
28    }
29
30    for (int y = 0; y < bm.height(); y++) {
31        for (int x = 0; x < bm.width(); x++) {
32            uint16_t c = *bm.getAddr16(x, y);
33            if (c != 0xFFFF) {
34                SkDebugf("------ nonwhite alpha=%x [%d %d] %x\n", alpha, x, y, c);
35                return;
36            }
37        }
38    }
39}
40
41class TextAlphaView : public SkView {
42public:
43	TextAlphaView() {
44        fByte = 0xFF;
45    }
46
47protected:
48    // overrides from SkEventSink
49    virtual bool onQuery(SkEvent* evt)  {
50        if (SampleCode::TitleQ(*evt)) {
51            SkString str("TextAlpha");
52            SampleCode::TitleR(evt, str.c_str());
53            return true;
54        }
55        return this->INHERITED::onQuery(evt);
56    }
57
58    void drawBG(SkCanvas* canvas) {
59        canvas->drawColor(SK_ColorWHITE);
60    }
61
62    virtual void onDraw(SkCanvas* canvas) {
63        this->drawBG(canvas);
64
65        const char* str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
66        SkPaint paint;
67        SkScalar    x = SkIntToScalar(10);
68        SkScalar    y = SkIntToScalar(20);
69
70        paint.setFlags(0x105);
71
72        paint.setARGB(fByte, 0xFF, 0xFF, 0xFF);
73
74        paint.setMaskFilter(SkBlurMaskFilter::Create(SkIntToScalar(3),
75                                        SkBlurMaskFilter::kNormal_BlurStyle));
76        paint.getMaskFilter()->unref();
77
78        SkRandom rand;
79
80        for (int ps = 6; ps <= 35; ps++) {
81            paint.setColor(rand.nextU() | (0xFF << 24));
82            paint.setTextSize(SkIntToScalar(ps));
83            paint.setTextSize(SkIntToScalar(24));
84            canvas->drawText(str, strlen(str), x, y, paint);
85            y += paint.getFontMetrics(NULL);
86        }
87        //check_for_nonwhite(canvas->getDevice()->accessBitmap(), fByte);
88        //SkDebugf("------ byte %x\n", fByte);
89
90        if (false) {
91            fByte += 1;
92            fByte &= 0xFF;
93            this->inval(NULL);
94        }
95    }
96
97    virtual SkView::Click* onFindClickHandler(SkScalar x, SkScalar y) {
98        return new Click(this);
99    }
100
101    virtual bool onClick(Click* click) {
102        int y = click->fICurr.fY;
103        if (y < 0) {
104            y = 0;
105        } else if (y > 255) {
106            y = 255;
107        }
108        fByte = y;
109        this->inval(NULL);
110        return true;
111    }
112
113private:
114    int fByte;
115
116    typedef SkView INHERITED;
117};
118
119//////////////////////////////////////////////////////////////////////////////
120
121static SkView* MyFactory() { return new TextAlphaView; }
122static SkViewRegister reg(MyFactory);
123
124