android_绘制鼠标

189人阅读 评论(1) 收藏 举报
分类:

转自:http://blog.csdn.net/xhcbird/article/details/17269055

不需要修改framework直接java绘制。

package com.example.hellocursor;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.graphics.PixelFormat;
import android.util.Log;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.WindowManager;
import android.widget.ImageView;

@SuppressLint("HandlerLeak") public class MainActivity extends Activity {

	private WindowManager wM;
	private WindowManager.LayoutParams lP;
	private ImageView ivCursor;
	private static final int msgUpdateView =1;
	volatile boolean bThreadRun = true;
	static int gaga = 1;
    @Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		//full screen;
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);
		//WindowManager;
		wM = (WindowManager)getBaseContext().getSystemService(Context.WINDOW_SERVICE);
		//top view for cursor;
		//copy from frameworks/base/policy/src/com/android/internal/policy/impl/PhoneWindowManager.java
		lP = new WindowManager.LayoutParams();
		lP.height = WindowManager.LayoutParams.WRAP_CONTENT;
		lP.width = WindowManager.LayoutParams.WRAP_CONTENT;
		//need:<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
		lP.type = WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY;
		lP.flags = WindowManager.LayoutParams.FLAG_FULLSCREEN
				| WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE
				| WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
				| WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN;
		lP.format = PixelFormat.TRANSLUCENT;
		//notice ,x=y=0; means center of window;
		lP.x = 0;
		lP.y = 0;
		//cursor image;
		ivCursor = new ImageView(this.getBaseContext());
		ivCursor.setImageResource(R.drawable.cursor);
		wM.addView(ivCursor, lP);
	}
    
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		// TODO Auto-generated method stub
		Log.i("mouse->", "onTouchEvent");
		//pen down and pen up; so use gaga;
		if(1 == gaga)
		{
			bThreadRun = true;
			lP.x = 0;
			lP.y = 0;
			new PrintMouse().start();
			gaga++;
		}
		//return super.onTouchEvent(event);
		return true;
	}

	class PrintMouse extends Thread {
		public void run() {
			while (bThreadRun) {
				Log.i("mouse->", "Thread");			
				try {
					sleep(200);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				Message m = new Message(); 
				m.what = msgUpdateView;
				myMessageHandler.sendMessage(m); 
			}
		}
	}
	
	Handler myMessageHandler = new Handler() {
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case msgUpdateView:
				lP.x = relX();
				lP.y = relY();				
				if(bThreadRun)
				{
					wM.updateViewLayout(ivCursor, lP);	
				}
				break;
			}
			super.handleMessage(msg);
		}
	};

    @Override
	public void finish() {
		// TODO Auto-generated method stub
    	bThreadRun = false;
    	gaga = 1;
    	wM.removeView(ivCursor);
		super.finish();
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
	}

	@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;
    }
	
	public native int relX();
	public native int relY();
    static {
        System.loadLibrary("mouse");
    }
}


查看评论

一种通过鼠标操作实现多边形的绘制的实现方法

前面的《MFC基本图形的绘制(三)在SDI中实现绘图操作》对多边形的绘制做了一个简单的介绍。尽管也实现了多边形的绘制,但那种方法有很大的局限性,最主要的就是表现在多边形的顶点(个数和位置)必须固定。通...
  • shufac
  • shufac
  • 2014-04-02 00:01:27
  • 1923

canvas画图:用鼠标---画曲线,直线,矩形,圆

var canvas = document.getElementById('canvas'); // 得到画布 var ctx = canvas.getContext('2d'); // 得到画布的...
  • qq_31164127
  • qq_31164127
  • 2017-06-08 17:28:10
  • 2604

自定义绘制鼠标

  • 2012年03月04日 11:47
  • 2KB
  • 下载

鼠标画多边形C++实现

  • 2010年06月24日 21:04
  • 339KB
  • 下载

MFC 鼠标移动画矩形

MFC 鼠标移动画矩形 问题日记:MFC 鼠标移动画矩形 昨天的时候,模仿人家的软件已经七七八八啦,开始不断完善的工作。首先是完善用鼠标拖动矩形选择东西,开始当然进入死胡同,在OnMouseMov...
  • chenjie863
  • chenjie863
  • 2013-12-24 16:03:29
  • 3143

Qt之鼠标随手画及画多边形

主要是用Qt实现2D涂鸦及话多边形的功能,我的版本是Qt5.3。
  • u010906483
  • u010906483
  • 2016-03-12 14:17:52
  • 3434

Qt鼠标绘制平滑曲线解决方案(1)

Qt鼠标实时绘制平滑曲线 简单分析了鼠标绘制平滑曲线的一些问题
  • eiilpux17
  • eiilpux17
  • 2017-11-20 19:17:03
  • 304

用canvas实现鼠标拖动绘制矩形框

需要用到jCanvas插件和jQuery。 jCanvas下载:https://raw.githubusercontent.com/caleb531/jcanvas/master/jcanvas...
  • u013594477
  • u013594477
  • 2017-12-01 11:14:31
  • 513

关于opencv2.4.10鼠标操作画矩形

下面的实例是我参考opencv3书籍编写的代码,通过鼠标绘制不同颜色的矩形。#include "opencv2/opencv.hpp" #include using namespace cv; u...
  • u014455876
  • u014455876
  • 2016-08-30 20:14:08
  • 705

谷歌地图鼠标绘制多边形方法

谷歌地图鼠标点击绘制多边形
  • zxcvbnm145
  • zxcvbnm145
  • 2015-08-28 16:43:11
  • 759
    个人资料
    持之以恒
    等级:
    访问量: 58万+
    积分: 7588
    排名: 3574
    最新评论