Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發 >> 關於android開發 >> 手勢縮放(一),手勢縮放(

手勢縮放(一),手勢縮放(

編輯:關於android開發

手勢縮放(一),手勢縮放(


 效果:字體縮放(無數倍),目前我自定義有個倍數,不過有點BUG;而且scroollveiw滑動沖突沒解決;

1.   activity_main.xml

<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);
}



}

 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved