Vin*_*tti 43
查看以下可能对您有所帮助的链接
以下链接提供了最佳示例,您可以重构以满足您的要求.
Emm*_*ery 37
对于android 2.2+(api level8),您可以使用ScaleGestureDetector.
你需要一个成员:
private ScaleGestureDetector mScaleDetector;
Run Code Online (Sandbox Code Playgroud)
在你的构造函数(或onCreate())中添加:
mScaleDetector = new ScaleGestureDetector(context, new OnScaleGestureListener() {
@Override
public void onScaleEnd(ScaleGestureDetector detector) {
}
@Override
public boolean onScaleBegin(ScaleGestureDetector detector) {
return true;
}
@Override
public boolean onScale(ScaleGestureDetector detector) {
Log.d(LOG_KEY, "zoom ongoing, scale: " + detector.getScaleFactor());
return false;
}
});
Run Code Online (Sandbox Code Playgroud)
您重写onTouchEvent:
@Override
public boolean onTouchEvent(MotionEvent event) {
mScaleDetector.onTouchEvent(event);
return true;
}
Run Code Online (Sandbox Code Playgroud)
如果您手动绘制视图,则在onScale()中可能会将比例因子存储在成员中,然后调用invalidate()并在onDraw()中绘制时使用比例因子.否则,您可以直接修改字体大小或onScale()中的字体大小.
fol*_*one 15
我TextView
使用本教程为我实现了一个缩放缩放.结果代码是这样的:
private GestureDetector gestureDetector;
private View.OnTouchListener gestureListener;
Run Code Online (Sandbox Code Playgroud)
并在onCreate()中:
// Zoom handlers
gestureDetector = new GestureDetector(new MyGestureDetector());
gestureListener = new View.OnTouchListener() {
// We can be in one of these 2 states
static final int NONE = 0;
static final int ZOOM = 1;
int mode = NONE;
static final int MIN_FONT_SIZE = 10;
static final int MAX_FONT_SIZE = 50;
float oldDist = 1f;
@Override
public boolean onTouch(View v, MotionEvent event) {
TextView textView = (TextView) findViewById(R.id.text);
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_POINTER_DOWN:
oldDist = spacing(event);
Log.d(TAG, "oldDist=" + oldDist);
if (oldDist > 10f) {
mode = ZOOM;
Log.d(TAG, "mode=ZOOM" );
}
break;
case MotionEvent.ACTION_POINTER_UP:
mode = NONE;
break;
case MotionEvent.ACTION_MOVE:
if (mode == ZOOM) {
float newDist = spacing(event);
// If you want to tweak font scaling, this is the place to go.
if (newDist > 10f) {
float scale = newDist / oldDist;
if (scale > 1) {
scale = 1.1f;
} else if (scale < 1) {
scale = 0.95f;
}
float currentSize = textView.getTextSize() * scale;
if ((currentSize < MAX_FONT_SIZE && currentSize > MIN_FONT_SIZE)
||(currentSize >= MAX_FONT_SIZE && scale < 1)
|| (currentSize <= MIN_FONT_SIZE && scale > 1)) {
textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, currentSize);
}
}
}
break;
}
return false;
}
Run Code Online (Sandbox Code Playgroud)
魔术常数1.1和0.95是根据经验选择的(scale
为此目的使用变量使我的TextView
行为有点奇怪).
归档时间: |
|
查看次数: |
109813 次 |
最近记录: |