你的位置:首页 > 操作系统

[操作系统]手势缩放(一)


 效果:字体缩放(无数倍),目前我自定义有个倍数,不过有点BUG;而且scroollveiw滑动冲突没解决;

1.   activity_main.

<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="11sp"
android:scrollbars="vertical"
/>

</ScrollView>

2.   MainActivity.java

TextView textView = (TextView) findViewById(R.id.textView);
textView.setMovementMethod(ScrollingMovementMethod.getInstance());
textView.setText("道德三皇五帝,\n" + "功名夏后商周.\n" + "英雄五霸闹春秋,\n" + "顷刻兴亡过手.\n"
+ "青史几行名姓,\n" + "北邙无数荒丘.\n" + "前人田地后人收,\n" + "说甚龙争虎斗." + "道德三皇五帝,\n" + "功名夏后商周.\n" + "英雄五霸闹春秋,\n" + "顷刻兴亡过手.\n"
+ "青史几行名姓,\n" + "北邙无数荒丘.\n" + "前人田地后人收,\n" + "说甚龙争虎斗." + "道德三皇五帝,\n" + "功名夏后商周.\n" + "英雄五霸闹春秋,\n" + "顷刻兴亡过手.\n"
+ "青史几行名姓,\n" + "北邙无数荒丘.\n" + "前人田地后人收,\n" + "说甚龙争虎斗." + "道德三皇五帝,\n" + "功名夏后商周.\n" + "英雄五霸闹春秋,\n" + "顷刻兴亡过手.\n"
+ "青史几行名姓,\n" + "北邙无数荒丘.\n" + "前人田地后人收,\n" + "说甚龙争虎斗." + "道德三皇五帝,\n" + "功名夏后商周.\n" + "英雄五霸闹春秋,\n" + "顷刻兴亡过手.\n"
+ "青史几行名姓,\n" + "北邙无数荒丘.\n" + "前人田地后人收,\n" + "说甚龙争虎斗." + "道德三皇五帝,\n" + "功名夏后商周.\n" + "英雄五霸闹春秋,\n" + "顷刻兴亡过手.\n"
+ "青史几行名姓,\n" + "北邙无数荒丘.\n" + "前人田地后人收,\n" + "说甚龙争虎斗." + "道德三皇五帝,\n" + "功名夏后商周.\n" + "英雄五霸闹春秋,\n" + "顷刻兴亡过手.\n"
+ "青史几行名姓,\n" + "北邙无数荒丘.\n" + "前人田地后人收,\n" + "说甚龙争虎斗." + "道德三皇五帝,\n" + "功名夏后商周.\n" + "英雄五霸闹春秋,\n" + "顷刻兴亡过手.\n"
+ "青史几行名姓,\n" + "北邙无数荒丘.\n" + "前人田地后人收,\n" + "说甚龙争虎斗.");
textView.getParent().requestDisallowInterceptTouchEvent(true);
textView.setOnTouchListener(new ZoomListenter());

 

3.   ZoomListenter.java

import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.TextView;

public class ZoomListenter implements OnTouchListener {

private int mode = 0;
float oldDist;
float textSize = 0;
TextView textView;
final static float defaultTextSize = 11.0f;
public boolean onTouch(View v, MotionEvent event) {
textView = (TextView) v;
if (textSize == 0) {
textSize = textView.getTextSize();
}
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
mode = 1;
break;
case MotionEvent.ACTION_UP:
mode = 0;
break;
case MotionEvent.ACTION_POINTER_UP:
mode -= 1;
break;
case MotionEvent.ACTION_POINTER_DOWN:
oldDist = spacing(event);
mode += 1;
break;

case MotionEvent.ACTION_MOVE:
if (mode >= 2) {
float newDist = spacing(event);
if (newDist > oldDist + 1) {
if (textSize < defaultTextSize*3){
zoom(newDist / oldDist);
oldDist = newDist;
}

}
if (newDist < oldDist - 1) {
if (textSize > defaultTextSize){
zoom(newDist / oldDist);
oldDist = newDist;
}
}
}
break;
}
return true;
}
private void zoom(float f) {
textView.setTextSize(textSize *= f);
}
private float spacing(MotionEvent event) {
float x = event.getX(0) - event.getX(1);
float y = event.getY(0) - event.getY(1);
//平方根
return (float) Math.sqrt(x * x + y * y);
}



}