Android简单滑动例子

因为觉得Android中用到滑动的地方太多,所以再写一篇简单的滑动例子方便以后查看。

效果图:

Android滑动效果

activity类代码:

package com.TouchView;

import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;

public class TouchView extends Activity {

	private TextView eventlable;
	private TextView histroy;
	private TextView TouchView;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		TouchView = (TextView) findViewById(R.id.touch_area);
		histroy = (TextView) findViewById(R.id.history_label);
		eventlable = (TextView) findViewById(R.id.event_label);

		TouchView.setOnTouchListener(new View.OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				int action = event.getAction();
				switch (action) {
				// 当按下的时候
				case (MotionEvent.ACTION_DOWN):
					Display("ACTION_DOWN", event);
					break;
				// 当按上的时候
				case (MotionEvent.ACTION_UP):
					int historysize = ProcessHistory(event);
					histroy.setText("历史数据" + historysize);
					Display("ACTION_UP", event);
					break;
				// 当触摸的时候
				case (MotionEvent.ACTION_MOVE):
					Display("ACTION_MOVE", event);
				}
				return true;
			}
		});
	}

	public void Display(String eventType, MotionEvent event) {
		// 触点相对坐标的信息
		int x = (int) event.getX();
		int y = (int) event.getY();
		// 表示触屏压力大小
		float pressure = event.getPressure();
		// 表示触点尺寸
		float size = event.getSize();
		// 获取绝对坐标信息
		int RawX = (int) event.getRawX();
		int RawY = (int) event.getRawY();

		String msg = "";

		msg += "事件类型" + eventType + "
";
		msg += "相对坐标" + String.valueOf(x) + "," + String.valueOf(y) + "
";
		msg += "绝对坐标" + String.valueOf(RawX) + "," + String.valueOf(RawY)
				+ "
";
		msg += "触点压力" + String.valueOf(pressure) + ",";
		msg += "触点尺寸" + String.valueOf(size) + "
";
		eventlable.setText(msg);
	}

	public int ProcessHistory(MotionEvent event) {
		int history = event.getHistorySize();
		for (int i = 0; i < history;="" i++)="" {="" long="" time="event.getHistoricalEventTime(i);" float="" pressure="event.getHistoricalPressure(i);" float="" x="event.getHistoricalX(i);" float="" y="event.getHistoricalY(i);" float="" size="event.getHistoricalSize(i);" }="" return="" history;="" }="" }="">

main.xml代码部分:

暂无评论
  • 1:请一针见血的评论。
  • 2:评论需要审核通过后才能显示。
  • 3:评论字数限制在1000字以内。
  • 当前字数:0
热门文章
推荐文章
随机文章
关于本站 - 广告服务 - 版权声明 - 联系我们 - 友情链接 - 网站地图 - 帮助中心