android_绘制鼠标

转自: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");
    }
}


阅读更多
个人分类: android
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭