public ScaleHandlerFroyo(Context c, ScaleListener listener) { mScaleListener = listener; mScaleDetector = new android.view.ScaleGestureDetector(c, new OnScaleGestureListener() { @Override public void onScaleEnd(ScaleGestureDetector detector) { mScaleListener.onScaleEnd(detector.getScaleFactor(), detector.getFocusX(), detector.getFocusY()); } @Override public boolean onScaleBegin(ScaleGestureDetector detector) { return mScaleListener.onScaleBegin(detector.getScaleFactor(), detector.getFocusX(), detector.getFocusY()); } @Override public boolean onScale(ScaleGestureDetector detector) { return mScaleListener.onScale(detector.getScaleFactor(), detector.getFocusX(), detector.getFocusY()); } }); }
protected OnScaleGestureListener getScaleListener() { return new ScaleListener(); }
private void init() { sharedPreferences = getSharedPreferences(getPackageName(), MODE_PRIVATE); mtv_reader = (MyTextView) findViewById(R.id.mtv_reader); scaled_density = getResources().getDisplayMetrics().scaledDensity; text_size_dp_default = mtv_reader.getTextSize() / scaled_density; mtv_reader.setTextSize(text_size_dp_default + sharedPreferences.getFloat("reader_text_size_dp_extra", 0.0f)); scaleGestureDetector = new ScaleGestureDetector(this, new OnScaleGestureListener() { @Override public boolean onScale(ScaleGestureDetector p1) { return false; } @Override public boolean onScaleBegin(ScaleGestureDetector p1) { return true; } @Override public void onScaleEnd(ScaleGestureDetector p1) { final float SCALE_FACTOR = p1.getScaleFactor(); if (SCALE_FACTOR >= 0.8f && SCALE_FACTOR <= 1.2f) { return; } final float OLD_TEXT_SIZE_DP = mtv_reader.getTextSize() / scaled_density; if (OLD_TEXT_SIZE_DP <= text_size_dp_default - 4.0f && SCALE_FACTOR <= 1.0f) { return; } if (OLD_TEXT_SIZE_DP >= text_size_dp_default + 4.0f && SCALE_FACTOR > 1.0f) { return; } final float NEW_TEXT_SIZE_DP = SCALE_FACTOR > 1.0f ? (OLD_TEXT_SIZE_DP + 4.0f) : (OLD_TEXT_SIZE_DP - 4.0f); //Unit: dp sharedPreferences.edit().putFloat("reader_text_size_dp_extra", NEW_TEXT_SIZE_DP - text_size_dp_default).commit(); mtv_reader.setTextSize(NEW_TEXT_SIZE_DP); } }); mtv_reader.setOnTouchListener(new OnTouchListener() { @Override public boolean onTouch(View p1, MotionEvent p2) { switch (p2.getPointerCount()) { case 2: { //Disallow HorizontalScrollView to intercept touch events. p1.getParent().requestDisallowInterceptTouchEvent(true); //Disallow ScrollView to intercept touch events. p1.getParent().getParent().requestDisallowInterceptTouchEvent(true); return scaleGestureDetector.onTouchEvent(p2); } default: return false; } } }); }