用canvas显示onTouchEvent点击的坐标。

本文介绍了如何在Android应用中创建一个继承自SurfaceView的MainView,并利用onTouchEvent事件来捕获用户的触摸操作,通过canvas在屏幕上显示用户点击的精确坐标。

新建一个MainView继承与SurfaceView。代码如下:

package com.example.view;

import java.util.Random;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.view.SurfaceView;

public class MainView extends SurfaceView implements Callback,Runnable  {
    private SurfaceHolder sfh;
    private Canvas canvas;
    private Paint paint;
    private Thread thread;
    private float x;
    private float y;
    private boolean isFlag;


    public MainView(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
        sfh = this.getHolder();
        sfh.addCallback(this);
        thread = new Thread(this);
        paint = new Paint();
        paint.setAntiAlias(true);
        paint.setColor(Color.RED);


    }

    public void draw(String location){
        try{
            canvas =sfh.lockCanvas();
            canvas.clipRect(100, 100, 200, 200);
            canvas.drawColor(Color.WHITE);
            canvas.save();
            canvas.drawText(location, 100, 110, paint);
            canvas.restore();
            canvas.save();
            canvas.drawText("hello.boy", 100, 130, paint);
            canvas.restore();
//          for(int i =0;i<10;i++){
//              int x = new Random().nextInt(300);
//              int y = new Random().nextInt(300);
//              canvas.drawText("hhhh", x, y, paint);
//          }
        } catch(Exception e){
            e.printStackTrace();
        } finally {
            if(canvas != null)
                sfh.unlockCanvasAndPost(canvas);
        }
    }

    @Override
    public void run() {
        // TODO Auto-generated method stub
        while(isFlag){
            draw("test");
            try{
                Thread.sleep(100);
            } catch(InterruptedException e){
                e.printStackTrace();
            }
        }

    }

    public boolean onTouchEvent(MotionEvent event){
        if(event.getAction() == MotionEvent.ACTION_UP){
            x = event.getX();
            y = event.getY();
            isFlag = false;
            draw("x =" + x +",y = " +y);
        }
        if(event.getAction() == MotionEvent.ACTION_MOVE){
            x = event.getX();
            y = event.getY();
            isFlag = false;
            draw("x ="+x + ",y = " + y);
        }
        return true;
    }

    @Override
    public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
        // TODO Auto-generated method stub

    }

    @Override
    public void surfaceCreated(SurfaceHolder arg0) {
        // TODO Auto-generated method stub
        thread.start();
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder arg0) {
        // TODO Auto-generated method stub

    }

}

修改MainActivity。java里的代码:

package com.example.testtouchevent;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import com.example.view.MainView;;

public class MainActivity extends Activity {
    private MainView mainview;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mainview = new MainView(this);
        setContentView(mainview);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

这样点击屏幕的时候屏幕中间就会显示点击的坐标了。移动的时候坐标也在变化的。运行结果如图:
![这里写图片描述](http://img.blog.youkuaiyun.com/20150522001444394)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值