如何能摸拟安卓 ontouchend 事件事件

2897人阅读
android(37)
在项目中用listtview 上边加了一个gallery & listview 外边还加了viewpage &, 总的来说能让ontouch 事件搞死。。。
listview gallery 跟viewpage的冲突解决方法见
http://blog.csdn.net/codywangziham01/article/details/7732909
现在就直插主题,怎样模拟onclick 事件
public boolean onTouchEvent(MotionEvent event) {
super.onTouchEvent(event);
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
downTime = System.currentTimeMillis();
float x = event.getX();
Rect rect = new Rect();
this.getHitRect(rect);
if(x&(rect.right-rect.left)/2){
case MotionEvent.ACTION_MOVE:
curX = event.getX();
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL:
if(event.getX()!=0){
curX = event.getX();
if(downTime!=0&&Math.abs(curX-downX)&10){
long del = System.currentTimeMillis()-downT
if(del&300){
LinearLayout item = (LinearLayout) this.getSelectedView();
RelativeLayout relativeLayout = (RelativeLayout) item.getChildAt(0);
view = relativeLayout.getChildAt(0);
RelativeLayout relativeLayout = (RelativeLayout) item.getChildAt(1);
view = relativeLayout.getChildAt(0);
if(view!=null){
view.performClick(); & /// 这句话是整个的核心啊,,这是拿到view 之后把onclcik 事件传递下去&
downTime = 0;
downX = 0;
这是gallery中的的ontouch 事件,大体是根据时间来算的, &300就是点击事件
注意:。。。。gallery中的items的onclick事件一定要写。。。只是把onclick事件传递下去。
设置完之后再设置clickable为false
viewHolder.lImageView.setClickable(false);
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:431580次
积分:2890
积分:2890
排名:第9717名
原创:57篇
评论:44条
(1)(1)(2)(1)(2)(1)(3)(2)(7)(1)(11)(4)(1)(1)(2)(1)(1)(2)(2)(4)(5)(5)(1)(2)(2)8687人阅读
Android(194)
导论在Android中模拟一个点击事件有三种方式是通过模拟MotionEvent来实现;一种是通过ADB来实现;一种是通过Instrumentation测试框架来实现第一种:模拟MotionEvent通用方法如下:private void setSimulateClick(View view, float x, float y) {
long downTime = SystemClock.uptimeMillis();
final MotionEvent downEvent = MotionEvent.obtain(downTime, downTime,
MotionEvent.ACTION_DOWN, x, y, 0);
downTime += 1000;
final MotionEvent upEvent = MotionEvent.obtain(downTime, downTime,
MotionEvent.ACTION_UP, x, y, 0);
view.onTouchEvent(downEvent);
view.onTouchEvent(upEvent);
downEvent.recycle();
upEvent.recycle();
}Demo:package com.xys.
import android.app.A
import android.os.B
import android.os.SystemC
import android.view.MotionE
import android.view.V
import android.widget.B
import android.widget.T
public class MainActivity extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button1);
public void clickeMe(View view) {
Toast.makeText(this, &clicked&, Toast.LENGTH_LONG).show();
public void simulate(View view) {
setSimulateClick(button, 160, 100);
private void setSimulateClick(View view, float x, float y) {
long downTime = SystemClock.uptimeMillis();
final MotionEvent downEvent = MotionEvent.obtain(downTime, downTime,
MotionEvent.ACTION_DOWN, x, y, 0);
downTime += 1000;
final MotionEvent upEvent = MotionEvent.obtain(downTime, downTime,
MotionEvent.ACTION_UP, x, y, 0);
view.onTouchEvent(downEvent);
view.onTouchEvent(upEvent);
downEvent.recycle();
upEvent.recycle();
第二种:ADB控制ADB中通过input来实现,用于输入touch,key等事件:The sources are:
touchnavigation
touchscreen
The commands and default sources are:
text &string& (Default: touchscreen)
keyevent [--longpress] &key code number or name& ... (Default: keyboard)
tap &x& &y& (Default: touchscreen)
swipe &x1& &y1& &x2& &y2& [duration(ms)] (Default: touchscreen)
press (Default: trackball)
roll &dx& &dy& (Default: trackball)Demo 输入按键enter key:adb shell input keyevent 66Demo 输入输入滑动操作:adb shell input touchscreen swipe 18 665 18 350第三种:InstrumentationInstrumentation是Android的测试框架,通过他,可以模拟很多Activity操作
//KeyEvent.KEYCODE_MENU
//KeyEvent.KEYCODE_BACK
public static void sendKeyEvent(final int KeyCode) {
new Thread() {
//不可在主线程中调用
public void run() {
Instrumentation inst = new Instrumentation();
inst.sendKeyDownUpSync(KeyCode);
} catch (Exception e) {
e.printStackTrace();
}.start();
}第四种:通过使用sendeventadb提供了控制手机的另一种实现:&sendevent [device] [type] [code] [value]eg:229就是menu的type值sendevent /dev/input/event0&1 229 1sendevent /dev/input/event0 1 229 0这两句需要一起执行,因为一个点击事件包含down和up,不然不能执行操作
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
Android群英传,包含了Android开发技巧与提高
网上购买:
本书源码:
访问:1169288次
积分:16273
积分:16273
排名:第482名
原创:244篇
转载:18篇
评论:2460条
为你推荐最新的博文~更有惊喜等着你
阅读:26060
阅读:62857
阅读:47807
(1)(3)(4)(3)(2)(3)(1)(1)(2)(1)(1)(1)(4)(4)(1)(1)(10)(4)(12)(10)(1)(10)(18)(11)(11)(1)(3)(3)(10)(6)(8)(3)(1)(2)(1)(2)(23)(2)(8)(33)(35)(9)Android提高之模拟信号示波器的实现
投稿:shichen2014
字体:[ ] 类型:转载 时间:
这篇文章主要介绍了Android模拟信号示波器的实现方法,在Android项目开发中有一定的实用价值,需要的朋友可以参考下
前面简单地介绍了Android程序开发中AudioRecord和AudioTrack的使用,这次再结合SurfaceView实现一个Android版的手机模拟信号示波器。最近物联网炒得很火,作为手机软件开发者,如何在不修改手机硬件电路的前提下实现与第三方传感器结合呢?麦克风就是一个很好的ADC接口,通过麦克风与第三方传感器结合,再在软件里对模拟信号做相应的处理,就可以提供更丰富的传感化应用。
先来看看本文程序运行的效果图(屏幕录像截图的速度较慢,真机实际运行起来会很流畅):
本文程序使用8000hz的采样率,对X轴方向绘图的实时性要求较高,如果不降低X轴的分辨率,程序的实时性较差,因此程序对X轴数据缩小区间为8倍~16倍。由于采用16位采样,因此Y轴数据的高度相对于手机屏幕来说也偏大,程序也对Y轴数据做缩小,区间为1倍~10倍。在SurfaceView的OnTouchListener方法里加入了波形基线的位置调节,直接在SurfaceView控件上触摸即可控制整体波形偏上或偏下显示。
main.xml源码如下:
&?xml version="1.0" encoding="utf-8"?&
&LinearLayout xmlns:android="/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent"&
&LinearLayout android:id="@+id/LinearLayout01"
android:layout_height="wrap_content" android:layout_width="fill_parent"
android:orientation="horizontal"&
&Button android:layout_height="wrap_content" android:id="@+id/btnStart"
android:text="开始" android:layout_width="80dip"&&/Button&
&Button android:layout_height="wrap_content" android:text="停止"
android:id="@+id/btnExit" android:layout_width="80dip"&&/Button&
&ZoomControls android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="@+id/zctlX"&&/ZoomControls&
&ZoomControls android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="@+id/zctlY"&&/ZoomControls&
&/LinearLayout&
&SurfaceView android:id="@+id/SurfaceView01"
android:layout_height="fill_parent" android:layout_width="fill_parent"&&/SurfaceView&
&/LinearLayout&
ClsOscilloscope.java是实现示波器的类库,包含AudioRecord操作线程和SurfaceView绘图线程的实现,两个线程同步操作,代码如下:
package com.testO
import java.util.ArrayL
import android.graphics.C
import android.graphics.C
import android.graphics.P
import android.graphics.R
import android.media.AudioR
import android.view.SurfaceV
public class ClsOscilloscope {
private ArrayList&short[]& inBuf = new ArrayList&short[]&();
private boolean isRecording =// 线程控制标记
* X轴缩小的比例
public int rateX = 4;
* Y轴缩小的比例
public int rateY = 4;
public int baseLine = 0;
public void initOscilloscope(int rateX, int rateY, int baseLine) {
this.rateX = rateX;
this.rateY = rateY;
this.baseLine = baseL
* @param recBufSize
AudioRecord的MinBufferSize
public void Start(AudioRecord audioRecord, int recBufSize, SurfaceView sfv,
Paint mPaint) {
isRecording =
new RecordThread(audioRecord, recBufSize).start();// 开始录制线程
new DrawThread(sfv, mPaint).start();// 开始绘制线程
public void Stop() {
isRecording =
inBuf.clear();// 清除
* 负责从MIC保存数据到inBuf
* @author GV
class RecordThread extends Thread {
private int recBufS
private AudioRecord audioR
public RecordThread(AudioRecord audioRecord, int recBufSize) {
this.audioRecord = audioR
this.recBufSize = recBufS
public void run() {
short[] buffer = new short[recBufSize];
audioRecord.startRecording();// 开始录制
while (isRecording) {
// 从MIC保存数据到缓冲区
int bufferReadResult = audioRecord.read(buffer, 0,
recBufSize);
short[] tmpBuf = new short[bufferReadResult / rateX];
for (int i = 0, ii = 0; i & tmpBuf. i++, ii = i
* rateX) {
tmpBuf[i] = buffer[ii];
synchronized (inBuf) {//
inBuf.add(tmpBuf);// 添加数据
audioRecord.stop();
} catch (Throwable t) {
* 负责绘制inBuf中的数据
* @author GV
class DrawThread extends Thread {
private int oldX = 0;// 上次绘制的X坐标
private int oldY = 0;// 上次绘制的Y坐标
private SurfaceV// 画板
private int X_index = 0;// 当前画图所在屏幕X轴的坐标
private Paint mP// 画笔
public DrawThread(SurfaceView sfv, Paint mPaint) {
this.sfv =
this.mPaint = mP
public void run() {
while (isRecording) {
ArrayList&short[]& buf = new ArrayList&short[]&();
synchronized (inBuf) {
if (inBuf.size() == 0)
buf = (ArrayList&short[]&) inBuf.clone();// 保存
inBuf.clear();// 清除
for (int i = 0; i & buf.size(); i++) {
short[] tmpBuf = buf.get(i);
SimpleDraw(X_index, tmpBuf, rateY, baseLine);// 把缓冲区数据画出来
X_index = X_index + tmpBuf.
if (X_index & sfv.getWidth()) {
X_index = 0;
* 绘制指定区域
* @param start
X轴开始的位置(全屏)
* @param buffer
* @param rate
Y轴数据缩小的比例
* @param baseLine
void SimpleDraw(int start, short[] buffer, int rate, int baseLine) {
if (start == 0)
Canvas canvas = sfv.getHolder().lockCanvas(
new Rect(start, 0, start + buffer.length, sfv.getHeight()));// 关键:获取画布
canvas.drawColor(Color.BLACK);// 清除背景
for (int i = 0; i & buffer. i++) {// 有多少画多少
int x = i +
y = buffer[i] / rate + baseL// 调节缩小比例,调节基准线
canvas.drawLine(oldX, oldY, x, y, mPaint);
sfv.getHolder().unlockCanvasAndPost(canvas);// 解锁画布,提交画好的图像
testOscilloscope.java是主程序,控制UI和ClsOscilloscope,代码如下:
package com.testO
import android.app.A
import android.graphics.C
import android.graphics.P
import android.media.AudioF
import android.media.AudioR
import android.media.MediaR
import android.os.B
import android.view.MotionE
import android.view.SurfaceV
import android.view.V
import android.view.View.OnTouchL
import android.widget.B
import android.widget.ZoomC
public class testOscilloscope extends Activity {
/** Called when the activity is first created. */
Button btnStart,btnE
ZoomControls zctlX,zctlY;
ClsOscilloscope clsOscilloscope=new ClsOscilloscope();
static final int frequency = 8000;//分辨率
static final int channelConfiguration = AudioFormat.CHANNEL_CONFIGURATION_MONO;
static final int audioEncoding = AudioFormat.ENCODING_PCM_16BIT;
static final int xMax = 16;//X轴缩小比例最大值,X轴数据量巨大,容易产生刷新延时
static final int xMin = 8;//X轴缩小比例最小值
static final int yMax = 10;//Y轴缩小比例最大值
static final int yMin = 1;//Y轴缩小比例最小值
int recBufS//录音最小buffer大小
AudioRecord audioR
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//录音组件
recBufSize = AudioRecord.getMinBufferSize(frequency,
channelConfiguration, audioEncoding);
audioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC, frequency,
channelConfiguration, audioEncoding, recBufSize);
btnStart = (Button) this.findViewById(R.id.btnStart);
btnStart.setOnClickListener(new ClickEvent());
btnExit = (Button) this.findViewById(R.id.btnExit);
btnExit.setOnClickListener(new ClickEvent());
//画板和画笔
sfv = (SurfaceView) this.findViewById(R.id.SurfaceView01);
sfv.setOnTouchListener(new TouchEvent());
mPaint = new Paint();
mPaint.setColor(Color.GREEN);// 画笔为绿色
mPaint.setStrokeWidth(1);// 设置画笔粗细
//示波器类库
clsOscilloscope.initOscilloscope(xMax/2, yMax/2, sfv.getHeight()/2);
//缩放控件,X轴的数据缩小的比率高些
zctlX = (ZoomControls)this.findViewById(R.id.zctlX);
zctlX.setOnZoomInClickListener(new View.OnClickListener() {
public void onClick(View v) {
if(clsOscilloscope.rateX&xMin)
clsOscilloscope.rateX--;
setTitle("X轴缩小"+String.valueOf(clsOscilloscope.rateX)+"倍"
+","+"Y轴缩小"+String.valueOf(clsOscilloscope.rateY)+"倍");
zctlX.setOnZoomOutClickListener(new View.OnClickListener() {
public void onClick(View v) {
if(clsOscilloscope.rateX&xMax)
clsOscilloscope.rateX++;
setTitle("X轴缩小"+String.valueOf(clsOscilloscope.rateX)+"倍"
+","+"Y轴缩小"+String.valueOf(clsOscilloscope.rateY)+"倍");
zctlY = (ZoomControls)this.findViewById(R.id.zctlY);
zctlY.setOnZoomInClickListener(new View.OnClickListener() {
public void onClick(View v) {
if(clsOscilloscope.rateY&yMin)
clsOscilloscope.rateY--;
setTitle("X轴缩小"+String.valueOf(clsOscilloscope.rateX)+"倍"
+","+"Y轴缩小"+String.valueOf(clsOscilloscope.rateY)+"倍");
zctlY.setOnZoomOutClickListener(new View.OnClickListener() {
public void onClick(View v) {
if(clsOscilloscope.rateY&yMax)
clsOscilloscope.rateY++;
setTitle("X轴缩小"+String.valueOf(clsOscilloscope.rateX)+"倍"
+","+"Y轴缩小"+String.valueOf(clsOscilloscope.rateY)+"倍");
protected void onDestroy() {
super.onDestroy();
android.os.Process.killProcess(android.os.Process.myPid());
* 按键事件处理
* @author GV
class ClickEvent implements View.OnClickListener {
public void onClick(View v) {
if (v == btnStart) {
clsOscilloscope.baseLine=sfv.getHeight()/2;
clsOscilloscope.Start(audioRecord,recBufSize,sfv,mPaint);
} else if (v == btnExit) {
clsOscilloscope.Stop();
* 触摸屏动态设置波形图基线
* @author GV
class TouchEvent implements OnTouchListener{
public boolean onTouch(View v, MotionEvent event) {
clsOscilloscope.baseLine=(int)event.getY();
希望本文实例对于读者进行Android项目开发能起到一定的借鉴与帮助作用。
您可能感兴趣的文章:
大家感兴趣的内容
12345678910
最近更新的内容
常用在线小工具君,已阅读到文档的结尾了呢~~
扫扫二维码,随身浏览文档
手机或平板扫扫即可继续访问
Android下触摸屏模拟鼠标的实现分析
举报该文档为侵权文档。
举报该文档含有违规或不良信息。
反馈该文档无法正常浏览。
举报该文档为重复文档。
推荐理由:
将文档分享至:
分享完整地址
文档地址:
粘贴到BBS或博客
flash地址:
支持嵌入FLASH地址的网站使用
html代码:
&embed src='/DocinViewer--144.swf' width='100%' height='600' type=application/x-shockwave-flash ALLOWFULLSCREEN='true' ALLOWSCRIPTACCESS='always'&&/embed&
450px*300px480px*400px650px*490px
支持嵌入HTML代码的网站使用
您的内容已经提交成功
您所提交的内容需要审核后才能发布,请您等待!
3秒自动关闭窗口

我要回帖

更多关于 ontouchmove 事件 的文章

 

随机推荐