4
4
import android .content .res .TypedArray ;
5
5
import android .text .Html ;
6
6
import android .util .AttributeSet ;
7
- import android .util .Log ;
7
+ import android .view .MotionEvent ;
8
+ import android .view .ScaleGestureDetector ;
8
9
import android .webkit .JavascriptInterface ;
10
+ import android .webkit .WebChromeClient ;
9
11
import android .webkit .WebSettings ;
10
12
import android .webkit .WebView ;
11
13
@@ -17,15 +19,19 @@ public interface OnHighlightListener {
17
19
void onFinishCodeHighlight ();
18
20
19
21
void onLanguageDetected (Language language , int relevance );
22
+
23
+ void onFontSizeChanged (int sizeInPx );
20
24
}
21
25
22
26
private String code = "" ;
23
27
private String escapeCode ;
24
28
private Theme theme ;
25
29
private Language language ;
26
- private int fontSize = 16 ;
30
+ private float fontSize = 16 ;
27
31
private boolean wrapLine = false ;
28
32
private OnHighlightListener onHighlightListener ;
33
+ private ScaleGestureDetector pinchDetector ;
34
+ private boolean zoomEnabled = false ;
29
35
30
36
public CodeView (Context context ) {
31
37
this (context , null );
@@ -41,14 +47,26 @@ public CodeView(Context context, AttributeSet attrs, int defStyleAttr) {
41
47
init (context , attrs );
42
48
}
43
49
50
+ @ Override
51
+ public boolean onTouchEvent (MotionEvent event ) {
52
+ if (isZoomEnabled ()) {
53
+ pinchDetector .onTouchEvent (event );
54
+ }
55
+ return super .onTouchEvent (event );
56
+ }
57
+
44
58
private void init (Context context , AttributeSet attrs ) {
45
59
TypedArray attributes = context .getTheme ().obtainStyledAttributes (attrs ,
46
60
R .styleable .CodeView , 0 , 0 );
47
61
//Define os atributos
48
62
setWrapLine (attributes .getBoolean (R .styleable .CodeView_cv_wrap_line , false ));
49
63
setFontSize (attributes .getInt (R .styleable .CodeView_cv_font_size , 14 ));
64
+ setZoomEnabled (attributes .getBoolean (R .styleable .CodeView_cv_zoom_enable , false ));
50
65
attributes .recycle ();
51
66
67
+ pinchDetector = new ScaleGestureDetector (context , new PinchListener ());
68
+
69
+ setWebChromeClient (new WebChromeClient ());
52
70
getSettings ().setJavaScriptEnabled (true );
53
71
getSettings ().setCacheMode (WebSettings .LOAD_NO_CACHE );
54
72
getSettings ().setLoadWithOverviewMode (true );
@@ -95,12 +113,22 @@ public void onLanguageDetected(String name, int relevance) {
95
113
return this ;
96
114
}
97
115
98
- public int getFontSize () {
116
+ /**
117
+ * Obtém o tamanho da fonte do texto em pixels.
118
+ */
119
+ public float getFontSize () {
99
120
return fontSize ;
100
121
}
101
122
102
- public CodeView setFontSize (int fontSize ) {
123
+ /**
124
+ * Define o tamanho da fonte do texto em pixels.
125
+ */
126
+ public CodeView setFontSize (float fontSize ) {
127
+ if (fontSize < 8 ) fontSize = 8 ;
103
128
this .fontSize = fontSize ;
129
+ if (onHighlightListener != null ) {
130
+ onHighlightListener .onFontSizeChanged ((int ) fontSize );
131
+ }
104
132
return this ;
105
133
}
106
134
@@ -166,6 +194,21 @@ public CodeView setWrapLine(boolean wrapLine) {
166
194
return this ;
167
195
}
168
196
197
+ /**
198
+ * Verifica se o zoom está habilitado.
199
+ */
200
+ public boolean isZoomEnabled () {
201
+ return zoomEnabled ;
202
+ }
203
+
204
+ /**
205
+ * Define que o zoom estará habilitado ou não.
206
+ */
207
+ public CodeView setZoomEnabled (boolean zoomEnabled ) {
208
+ this .zoomEnabled = zoomEnabled ;
209
+ return this ;
210
+ }
211
+
169
212
/**
170
213
* Aplica os atributos e exibe o código.
171
214
*/
@@ -188,7 +231,7 @@ private String toHtml() {
188
231
sb .append ("<style>\n " );
189
232
//body
190
233
sb .append ("body {" );
191
- sb .append ("font-size:" ).append (String .format ("%dpx;" , getFontSize ()));
234
+ sb .append ("font-size:" ).append (String .format ("%dpx;" , ( int ) getFontSize ()));
192
235
sb .append ("margin: 0px; line-height: 1.2;" );
193
236
sb .append ("}\n " );
194
237
//.hljs
@@ -212,4 +255,49 @@ private String toHtml() {
212
255
.append ("</code></pre>\n " );
213
256
return sb .toString ();
214
257
}
258
+
259
+ private void changeFontSize (int sizeInPx ) {
260
+ if (android .os .Build .VERSION .SDK_INT >= android .os .Build .VERSION_CODES .KITKAT ) {
261
+ evaluateJavascript ("javascript:document.body.style.fontSize = '" + sizeInPx + "px'" , null );
262
+ } else {
263
+ loadUrl ("javascript:document.body.style.fontSize = '" + sizeInPx + "px'" );
264
+ }
265
+ }
266
+
267
+ /**
268
+ * Eventos de pinça.
269
+ */
270
+ private class PinchListener extends ScaleGestureDetector .SimpleOnScaleGestureListener {
271
+
272
+ private float fontSize ;
273
+ private int oldFontSize ;
274
+
275
+ @ Override
276
+ public boolean onScaleBegin (ScaleGestureDetector detector ) {
277
+ fontSize = getFontSize ();
278
+ oldFontSize = (int ) fontSize ;
279
+ return super .onScaleBegin (detector );
280
+ }
281
+
282
+ @ Override
283
+ public void onScaleEnd (ScaleGestureDetector detector ) {
284
+ CodeView .this .fontSize = fontSize ;
285
+ super .onScaleEnd (detector );
286
+ }
287
+
288
+ @ Override
289
+ public boolean onScale (ScaleGestureDetector detector ) {
290
+ fontSize = getFontSize () * detector .getScaleFactor ();
291
+ if (fontSize >= 8 ) {
292
+ changeFontSize ((int ) fontSize );
293
+ if (onHighlightListener != null && oldFontSize != (int ) fontSize ) {
294
+ onHighlightListener .onFontSizeChanged ((int ) fontSize );
295
+ }
296
+ oldFontSize = (int ) fontSize ;
297
+ } else {
298
+ fontSize = 8 ;
299
+ }
300
+ return false ;
301
+ }
302
+ }
215
303
}
0 commit comments