欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android轻松画出触摸轨迹

程序员文章站 2024-02-26 17:49:28
本文实例介绍了android如何画出触摸轨迹的方法,分享给大家供大家参考,具体内容如下 效果图: 实现代码: package com.android.ga...

本文实例介绍了android如何画出触摸轨迹的方法,分享给大家供大家参考,具体内容如下

效果图:

Android轻松画出触摸轨迹

实现代码:

package com.android.gameview5;

import android.app.activity;
import android.content.context;
import android.graphics.canvas;
import android.graphics.color;
import android.graphics.paint;
import android.graphics.path;
import android.os.bundle;
import android.view.motionevent;
import android.view.surfaceholder;
import android.view.surfaceholder.callback;
import android.view.surfaceview;
import android.view.window;
import android.view.windowmanager;

public class surfaceviewactivity3 extends activity {
  public void oncreate(bundle s){
  super.oncreate(s);
  //全屏显示
  requestwindowfeature(window.feature_no_title);
  getwindow().setflags(windowmanager.layoutparams.flag_fullscreen,
  windowmanager.layoutparams.flag_fullscreen);
  setcontentview(new myview(this));
  }
  public class myview extends surfaceview implements callback,runnable{

  
  public static final int time_in_frame =50;
  
  paint mpaint = null;
  paint mtextpaint = null;
  surfaceholder msurfaceholder = null;
  
  
  boolean mrunning = false;
  
  
  canvas mcanvas = null;
  
  
  private path mpath;
  
  private float mposx,mposy;
  
public myview(context context){
super(context);
this.setfocusable(true);
this.setfocusableintouchmode(true);
  msurfaceholder = this.getholder();
  msurfaceholder.addcallback(this);
  mcanvas = new canvas();
      
  mpaint = new paint();
  mpaint.setcolor(color.black);
  
  
  mpaint.setantialias(true);
  
  mpaint.setstyle(paint.style.stroke);
  
  
  mpaint.setstrokecap(paint.cap.round);
  
  
  mpaint.setstrokewidth(6);
  
  mpath = new path();
  
  
  mtextpaint = new paint();
  
  mtextpaint.setcolor(color.black);
  
  mtextpaint.settextsize(15);
  
}
public boolean ontouchevent(motionevent event){
  int action = event.getaction();
  float x = event.getx();
  float y = event.gety();
  switch(action){
  case motionevent.action_down:
  mpath.moveto(x, y);
  break;
  case motionevent.action_move:
  mpath.quadto(mposx, mposy, x, y);
  break;
  case motionevent.action_up:
  //mpath.reset();
  break;
  }
  //记录当前触摸点得当前得坐标
  mposx = x;
  mposy = y;
return true;
}
private void ondraw(){
mcanvas.drawcolor(color.white);
//绘制曲线
mcanvas.drawpath(mpath, mpaint);
mcanvas.drawtext("当前触笔x:"+mposx,0,20,mtextpaint);
mcanvas.drawtext("当前触笔y:"+mposy,0,40,mtextpaint);
}
public void run() {
// todo auto-generated method stub
while(mrunning){
long starttime = system.currenttimemillis();
synchronized(msurfaceholder){
mcanvas = msurfaceholder.lockcanvas();
ondraw();
msurfaceholder.unlockcanvasandpost(mcanvas);
}
long endtime = system.currenttimemillis();
int difftime = (int) (endtime - starttime);
while(difftime<=time_in_frame){
difftime =(int)(system.currenttimemillis()-starttime);
thread.yield();
}
}
}

@override
public void surfacechanged(surfaceholder holder, int format, int width,
int height) {
// todo auto-generated method stub
}

@override
public void surfacecreated(surfaceholder holder) {
mrunning = true;
new thread(this).start();
}

@override
public void surfacedestroyed(surfaceholder holder) {
// todo auto-generated method stub
mrunning = false;
}
  
  }
}

以上就是android轻松画出触摸轨迹的具体方法,希望对大家的学习有所帮助。