这是抄的某位大神代码后自己改的 可惜忘记了原帖地址。。。。先贴下代码以备忘记
使用这个textview 控件就可以 手势缩放字体大小 不过好像在有scrollbar 的情况下 效果不是很好。。
package com.joosee.app.ui;
import android.content.Context;
import android.graphics.Matrix;
import android.graphics.PointF;
import android.util.AttributeSet;
import android.util.FloatMath;
import android.util.Log;
import android.view.MotionEvent;
import android.widget.TextView;
public class JooseeContentTextView extends TextView {
Matrix matrix = new Matrix();
Matrix savedMatrix = new Matrix();
// We can be in one of these 3 states
static final int NONE = 0;
static final int DRAG = 1;
static final int ZOOM = 2;
static final float zoomSpeed = 1.5f;
private static int minSize = 8;
private static int maxSize = 30;
int mode = NONE;
private int nowSize = 12;
// Remember some things for zooming
PointF start = new PointF();
PointF mid = new PointF();
float oldDist = 1f;
public JooseeContentTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
savedMatrix.set(matrix);
start.set(event.getX(), event.getY());
// Log.d(TAG, "mode=DRAG");
mode = DRAG;
// Log.d(TAG, "mode=NONE");
break;
case MotionEvent.ACTION_POINTER_DOWN:
oldDist = spacing(event);
// Log.d(TAG, "oldDist=" + oldDist);
if (oldDist > 10f) {
savedMatrix.set(matrix);
midPoint(mid, event);
mode = ZOOM;
Log.d("textview", "mode=ZOOM");
}
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_POINTER_UP:
mode = NONE;
// Log.e("view.getWidth", view.getWidth() + "");
// Log.e("view.getHeight", view.getHeight() + "");
break;
case MotionEvent.ACTION_MOVE:
if (mode == DRAG) {
// ...
matrix.set(savedMatrix);
matrix.postTranslate(event.getX() - start.x, event.getY()
- start.y);
} else if (mode == ZOOM) {
float newDist = spacing(event);
// Log.d(TAG, "newDist=" + newDist);
Log.d("textview", "old=" + oldDist + "new" + newDist);
// if (newDist > 10f) {
// matrix.set(savedMatrix);
// float scale = newDist / oldDist;
// matrix.postScale(scale, scale, mid.x, mid.y);
// }
zoom(newDist, oldDist);
}
break;
}
return true; // indicate event was handled
}
private float spacing(MotionEvent event) {
float x = event.getX(0) - event.getX(1);
float y = event.getY(0) - event.getY(1);
return FloatMath.sqrt(x * x + y * y);
}
private void midPoint(PointF point, MotionEvent event) {
float x = event.getX(0) + event.getX(1);
float y = event.getY(0) + event.getY(1);
point.set(x / 2, y / 2);
}
private void zoom(Float newDist, Float oldDist) {
if (newDist > 10f) {
if (oldDist > newDist) {
nowSize = (int) (nowSize - zoomSpeed);
if (nowSize > minSize) {
this.setTextSize(nowSize);
} else {
nowSize = minSize;
this.setTextSize(minSize);
}
Log.d("textview", "small " + nowSize + "");
} else {
nowSize = (int) (nowSize + zoomSpeed);
if (nowSize < maxSize) {
this.setTextSize(nowSize);
} else {
nowSize = maxSize;
this.setTextSize(nowSize);
}
Log.d("textview", "large " + nowSize + "");
}
}
}
}