标签:
新建"MyView"类
package onTouchEvent; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.view.MotionEvent; import android.view.View; public class MyView extends View { private int textX=20,textY=20; public MyView(Context context) { super(context); setFocusable(true); // TODO Auto-generated constructor stub } //画笔onDraw方法 @Override protected void onDraw(Canvas canvas) { // TODO Auto-generated method stub super.onDraw(canvas); //初始化画笔 Paint paint=new Paint(); //设置颜色 paint.setColor(Color.GREEN); //设置字体大小 paint.setTextSize(50); //在画布上写文字 canvas.drawText("Android", textX, textY, paint); } @Override public boolean onTouchEvent(MotionEvent event) { // TODO Auto-generated method stub //获取用户手指触屏的X坐标赋值与文本的X坐标 textX=(int)event.getX(); //获取用户手指触屏的Y坐标赋值与文本的Y坐标 textY=(int)event.getY(); //重绘画布 invalidate(); return true; } }
MainActivity中将setContentView设置为当前MyView,即
setContentView(new MyView(this));
执行效果图如下:
标签:
原文地址:http://www.cnblogs.com/caidupingblogs/p/4972057.html