android怎么实现多点触摸应用

android怎么实现多点触摸应用

本篇内容介绍了“android怎么实现多点触摸应用”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

JhkMultiTouchActivity.java

packagecom.android.forlinx;importandroid.app.Activity;importandroid.os.Bundle;importandroid.view.Window;importandroid.view.WindowManager;publicclassJhkMultiTouchActivityextendsActivity{/**Calledwhentheactivityisfirstcreated.*/@OverridepublicvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);//setContentView(R.layout.main);//隐藏标题栏requestWindowFeature(Window.FEATURE_NO_TITLE);//设置成全屏getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);//设置为上面的MTViewsetContentView(newMTView(this));}}

MTView.java

packagecom.android.forlinx;importandroid.content.Context;importandroid.graphics.Canvas;importandroid.graphics.Color;importandroid.graphics.Paint;importandroid.graphics.Typeface;importandroid.view.MotionEvent;importandroid.view.SurfaceHolder;importandroid.view.SurfaceView;publicclassMTViewextendsSurfaceViewimplementsSurfaceHolder.Callback{privatestaticfinalintMAX_TOUCHPOINTS=10;privatestaticfinalStringSTART_TEXT="请随便触摸屏幕进行测试";privatePainttextPaint=newPaint();privatePainttouchPaints[]=newPaint[MAX_TOUCHPOINTS];privateintcolors[]=newint[MAX_TOUCHPOINTS];privateintwidth,height;privatefloatscale=1.0f;publicMTView(Contextcontext){super(context);SurfaceHolderholder=getHolder();holder.addCallback(this);setFocusable(true);//确保我们的View能获得输入焦点setFocusableInTouchMode(true);//确保能接收到触屏事件init();}privatevoidinit(){//初始化10个不同颜色的画笔textPaint.setColor(Color.GREEN);textPaint.setTypeface(null);textPaint.setAlpha(200);colors[0]=Color.BLUE;colors[1]=Color.RED;colors[2]=Color.GREEN;colors[3]=Color.YELLOW;colors[4]=Color.CYAN;colors[5]=Color.MAGENTA;colors[6]=Color.DKGRAY;colors[7]=Color.WHITE;colors[8]=Color.LTGRAY;colors[9]=Color.GRAY;for(inti=0;i<MAX_TOUCHPOINTS;i++){touchPaints[i]=newPaint();touchPaints[i].setColor(colors[i]);touchPaints[i].setAlpha(50);}}/**处理触屏事件*/@OverridepublicbooleanonTouchEvent(MotionEventevent){//获得屏幕触点数量intpointerCount=event.getPointerCount();if(pointerCount>MAX_TOUCHPOINTS){pointerCount=MAX_TOUCHPOINTS;}//锁定Canvas,开始进行相应的界面处理Canvasc=getHolder().lockCanvas();if(c!=null){c.drawColor(Color.BLACK);if(event.getAction()==MotionEvent.ACTION_UP){//当手离开屏幕时,清屏}else{//先在屏幕上画一个十字,然后画一个圆for(inti=0;i<pointerCount;i++){//获取一个触点的坐标,然后开始绘制intid=event.getPointerId(i);intx=(int)event.getX(i);inty=(int)event.getY(i);drawCrosshairsAndText(x,y,touchPaints[id],i,id,c);}for(inti=0;i<pointerCount;i++){intid=event.getPointerId(i);intx=(int)event.getX(i);inty=(int)event.getY(i);drawCircle(x,y,touchPaints[id],c);}}//画完后,unlockgetHolder().unlockCanvasAndPost(c);}returntrue;}/***画十字及坐标信息**@paramx*@paramy*@parampaint*@paramptr*@paramid*@paramc*/privatevoiddrawCrosshairsAndText(intx,inty,Paintpaint,intptr,intid,Canvasc){c.drawLine(0,y,width,y,paint);c.drawLine(x,0,x,height,paint);inttextY=(int)((15+20*ptr)*scale);c.drawText("x"+ptr+"="+x,10*scale,textY,textPaint);c.drawText("y"+ptr+"="+y,70*scale,textY,textPaint);c.drawText("id"+ptr+"="+id,width-55*scale,textY,textPaint);}/***画圆**@paramx*@paramy*@parampaint*@paramc*/privatevoiddrawCircle(intx,inty,Paintpaint,Canvasc){c.drawCircle(x,y,40*scale,paint);}/**进入程序时背景画成黑色,然后把“START_TEXT”写到屏幕*/publicvoidsurfaceChanged(SurfaceHolderholder,intformat,intwidth,intheight){this.width=width;this.height=height;if(width>height){this.scale=width/480f;}else{this.scale=height/480f;}textPaint.setTextSize(14*scale);Canvasc=getHolder().lockCanvas();if(c!=null){//背景黑色c.drawColor(Color.BLACK);floattWidth=textPaint.measureText(START_TEXT);c.drawText(START_TEXT,width/2-tWidth/2,height/2,textPaint);getHolder().unlockCanvasAndPost(c);}}publicvoidsurfaceCreated(SurfaceHolderholder){}publicvoidsurfaceDestroyed(SurfaceHolderholder){}}

效果图

“android怎么实现多点触摸应用”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注恰卡编程网网站,小编将为大家输出更多高质量的实用文章!

发布于 2022-05-19 10:37:11
收藏
分享
海报
0 条评论
24
上一篇:python怎么获取当前系统的日期 下一篇:thinkphp如何用中间件记录行为日志
目录

    0 条评论

    本站已关闭游客评论,请登录或者注册后再评论吧~

    忘记密码?

    图形验证码