Skip to content

Commit 5e662a9

Browse files
committed
Implementar função zoom através do gesto de pinça
1 parent c675b25 commit 5e662a9

File tree

4 files changed

+103
-6
lines changed

4 files changed

+103
-6
lines changed

app/src/main/java/br/tiagohm/codeview/app/MainActivity.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import android.app.ProgressDialog;
44
import android.os.Bundle;
55
import android.support.v7.app.AppCompatActivity;
6+
import android.util.Log;
67
import android.view.Menu;
78
import android.view.MenuItem;
89
import android.widget.Toast;
@@ -125,6 +126,7 @@ protected void onCreate(Bundle savedInstanceState) {
125126
.setLanguage(Language.AUTO)
126127
.setWrapLine(true)
127128
.setFontSize(14)
129+
.setZoomEnabled(true)
128130
.apply();
129131
}
130132

@@ -161,6 +163,11 @@ public void onLanguageDetected(Language language, int relevance) {
161163
Toast.makeText(this, "language: " + language + " relevance: " + relevance, Toast.LENGTH_SHORT).show();
162164
}
163165

166+
@Override
167+
public void onFontSizeChanged(int sizeInPx) {
168+
Log.d("TAG", "font-size: " + sizeInPx + "px");
169+
}
170+
164171
private void setHighlightTheme(int pos) {
165172
mCodeView.setTheme(Theme.ALL.get(pos)).apply();
166173
Toast.makeText(this, Theme.ALL.get(pos).getName(), Toast.LENGTH_SHORT).show();

app/src/main/res/layout/activity_main.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
android:layout_height="0dp"
1616
android:layout_weight="1"
1717
app:cv_font_size="14"
18-
app:cv_wrap_line="true">
18+
app:cv_wrap_line="true"
19+
app:cv_zoom_enable="true">
1920
</br.tiagohm.codeview.CodeView>
2021

2122
</LinearLayout>

library/src/main/java/br/tiagohm/codeview/CodeView.java

Lines changed: 93 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44
import android.content.res.TypedArray;
55
import android.text.Html;
66
import android.util.AttributeSet;
7-
import android.util.Log;
7+
import android.view.MotionEvent;
8+
import android.view.ScaleGestureDetector;
89
import android.webkit.JavascriptInterface;
10+
import android.webkit.WebChromeClient;
911
import android.webkit.WebSettings;
1012
import android.webkit.WebView;
1113

@@ -17,15 +19,19 @@ public interface OnHighlightListener {
1719
void onFinishCodeHighlight();
1820

1921
void onLanguageDetected(Language language, int relevance);
22+
23+
void onFontSizeChanged(int sizeInPx);
2024
}
2125

2226
private String code = "";
2327
private String escapeCode;
2428
private Theme theme;
2529
private Language language;
26-
private int fontSize = 16;
30+
private float fontSize = 16;
2731
private boolean wrapLine = false;
2832
private OnHighlightListener onHighlightListener;
33+
private ScaleGestureDetector pinchDetector;
34+
private boolean zoomEnabled = false;
2935

3036
public CodeView(Context context) {
3137
this(context, null);
@@ -41,14 +47,26 @@ public CodeView(Context context, AttributeSet attrs, int defStyleAttr) {
4147
init(context, attrs);
4248
}
4349

50+
@Override
51+
public boolean onTouchEvent(MotionEvent event) {
52+
if (isZoomEnabled()) {
53+
pinchDetector.onTouchEvent(event);
54+
}
55+
return super.onTouchEvent(event);
56+
}
57+
4458
private void init(Context context, AttributeSet attrs) {
4559
TypedArray attributes = context.getTheme().obtainStyledAttributes(attrs,
4660
R.styleable.CodeView, 0, 0);
4761
//Define os atributos
4862
setWrapLine(attributes.getBoolean(R.styleable.CodeView_cv_wrap_line, false));
4963
setFontSize(attributes.getInt(R.styleable.CodeView_cv_font_size, 14));
64+
setZoomEnabled(attributes.getBoolean(R.styleable.CodeView_cv_zoom_enable, false));
5065
attributes.recycle();
5166

67+
pinchDetector = new ScaleGestureDetector(context, new PinchListener());
68+
69+
setWebChromeClient(new WebChromeClient());
5270
getSettings().setJavaScriptEnabled(true);
5371
getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
5472
getSettings().setLoadWithOverviewMode(true);
@@ -95,12 +113,22 @@ public void onLanguageDetected(String name, int relevance) {
95113
return this;
96114
}
97115

98-
public int getFontSize() {
116+
/**
117+
* Obtém o tamanho da fonte do texto em pixels.
118+
*/
119+
public float getFontSize() {
99120
return fontSize;
100121
}
101122

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;
103128
this.fontSize = fontSize;
129+
if (onHighlightListener != null) {
130+
onHighlightListener.onFontSizeChanged((int) fontSize);
131+
}
104132
return this;
105133
}
106134

@@ -166,6 +194,21 @@ public CodeView setWrapLine(boolean wrapLine) {
166194
return this;
167195
}
168196

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+
169212
/**
170213
* Aplica os atributos e exibe o código.
171214
*/
@@ -188,7 +231,7 @@ private String toHtml() {
188231
sb.append("<style>\n");
189232
//body
190233
sb.append("body {");
191-
sb.append("font-size:").append(String.format("%dpx;", getFontSize()));
234+
sb.append("font-size:").append(String.format("%dpx;", (int) getFontSize()));
192235
sb.append("margin: 0px; line-height: 1.2;");
193236
sb.append("}\n");
194237
//.hljs
@@ -212,4 +255,49 @@ private String toHtml() {
212255
.append("</code></pre>\n");
213256
return sb.toString();
214257
}
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+
}
215303
}

library/src/main/res/values/attrs.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@
33
<declare-styleable name="CodeView">
44
<attr name="cv_wrap_line" format="boolean"/>
55
<attr name="cv_font_size" format="integer"/>
6+
<attr name="cv_zoom_enable" format="boolean"/>
67
</declare-styleable>
78
</resources>

0 commit comments

Comments
 (0)