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