我想创建一个TextView
,可以根据缩放缩放更改文本的大小。 TextView
位于ScrollView
内。只有当你的手指几乎完全水平移动时,我才能在捏住时变焦TextView
。如果捏缩放有垂直分量,则默认为滚动而不是缩放
当指针上有多个指针时,有没有办法禁用ScrollView
的滚动,然后当指针计数返回到1或0时重新启用滚动?是创建CustomScrollView
小部件的最佳方式还是更简单的方法?
答案 0 :(得分:4)
受到这个答案的启发https://stackoverflow.com/a/14577399/2014236
scaleGD = new ScaleGestureDetector(this, new simpleOnScaleGestureListener());
mTextView.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getPointerCount() == 1){
//stuff for 1 pointer
}else{ //when 2 pointers are present
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
// Disallow ScrollView to intercept touch events.
v.getParent().requestDisallowInterceptTouchEvent(true);
scaleGD.onTouchEvent(event);
break;
case MotionEvent.ACTION_MOVE:
// Disallow ScrollView to intercept touch events.
v.getParent().requestDisallowInterceptTouchEvent(true);
scaleGD.onTouchEvent(event);
break;
case MotionEvent.ACTION_UP:
// Allow ScrollView to intercept touch events.
v.getParent().requestDisallowInterceptTouchEvent(false);
break;
}
}
return true;
}
});
编辑:
//later in the code
public class simpleOnScaleGestureListener extends SimpleOnScaleGestureListener {
@Override
public boolean onScale(ScaleGestureDetector detector) {
float size = codedText.getTextSize();
float factor = detector.getScaleFactor();
int increase = 0;
if(factor > 1.0f)
increase = 2;
else if(factor < 1.0f)
increase = -2;
size += increase;
codedText.setTextSize(TypedValue.COMPLEX_UNIT_PX, size);
plainText.setTextSize(TypedValue.COMPLEX_UNIT_PX, size);
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
//remembering the favourable text size
Editor edit = sp.edit();
edit.putFloat("TXTSIZE", size);
edit.commit();
return true;
}
}
绝不是完美的,但对我来说做得足够好 - 随意做出改进。
答案 1 :(得分:2)
我认为使用起来更简单:
textView.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
if(event.getPointerCount() == 1) {
Log.d("Scroll","1-pointer touch");
v.getParent().requestDisallowInterceptTouchEvent(false);
}
if(event.getPointerCount() == 2){
Log.d("Zoom","2-pointer touch");
v.getParent().requestDisallowInterceptTouchEvent(true);
}
return false;
}
});
答案 2 :(得分:0)
这是一个很好的android-dev博客示例代码,展示了如何实现手指捏。
除非您不重写onTouchEvent方法以在您的条件下返回false,否则无法禁用滚动条的滚动功能。以下是一些可以帮助您在滚动视图中禁用滚动的线程。
How to disable scrolling in ScrollView when the viewSwitcher is enabled?
How to disable ScrollView scrolling?
Disable ScrollView Programmatically?
谢谢:)
答案 3 :(得分:0)
我正在使用此解决方案。 vídeo
中的缩放算法凭据使用不带ScrollView的TextView,只需使用 android:scrollbars =“ vertical”
<TextView
android:id="@+id/activity_content_text_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_weight="1"
android:gravity="fill"
android:textSize="8pt"
android:scrollbars="vertical"
/>
Java:
public class MainActivity extends Activity implements View.OnTouchListener {
private TextView textContent = null;
private final static float move = 200;
private float ratio = 1.0f;
private int baseDist;
private float baseRatio;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.teste_layout);
textContent = findViewById(R.id.activity_content_text_content);
textContent.setText("Lorem ipsum dolor sit amet......");
textContent.setMovementMethod(new ScrollingMovementMethod());
textContent.setOnTouchListener(this);
}
@Override
public boolean onTouch(View v, MotionEvent event) {
return onTouchEvent(event);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if(event.getPointerCount() == 2){
int action = event.getAction();
int mainAction = action&MotionEvent.ACTION_MASK;
if(mainAction == MotionEvent .ACTION_POINTER_DOWN){
baseDist = getDisTance(event);
baseRatio = ratio;
} else {
float scale = (getDisTance(event)-baseDist)/move;
float factor = (float)Math.pow(2, scale);
ratio = Math.min(1024.0f, Math.max(0.1f, baseRatio*factor));
textContent.setTextSize(ratio+15);
}
} else {
return false;
}
return true;
}
private int getDisTance(MotionEvent event) {
int dx = (int) (event.getX(0)-event.getX(1));
int dy = (int) (event.getY(0)-event.getY(1));
return (int) (Math.sqrt(dx*dx+dy*dy));
}
}
Frevo Studios的开发人员