概述
不需要修改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_绘制鼠标的全部内容,希望文章能够帮你解决android_绘制鼠标所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复