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