public MouseView(Context context) {
super(context);
}
public MouseView(Context context, MouseManager mMouseMrg) {
super(context);
init( mMouseMrg);
}
public OnMouseListener getOnMouseListener() {
return mOnMouseListener;
}
public void setOnMouseListener(OnMouseListener mOnMouseListener) {
this.mOnMouseListener = mOnMouseListener;
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
if (mMouseView != null && mMouseBitmap != null) {
mMouseView.measure(MeasureSpec.makeMeasureSpec(mMouseBitmap.getWidth(), MeasureSpec.EXACTLY),
MeasureSpec.makeMeasureSpec(mMouseBitmap.getHeight(), MeasureSpec.EXACTLY));
}
}
private void init(MouseManager manager) {
mMouseManager = manager;
Drawable drawable = getResources().getDrawable(
R.drawable.shubiao);
mMouseBitmap = drawableToBitamp(drawable);
mMouseView = new ImageView(getContext());
mMouseView.setImageBitmap(mMouseBitmap);
addView(mMouseView, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
mOffsetX = (int)((mMouseBitmap.getWidth()));
mOffsetY = (int)((mMouseBitmap.getHeight()));
}
@Override
protected void onLayout(boolean changed, int left, int top, int right,
int bottom) {
if(mMouseView != null) {
mMouseView.layout(mMouseX, mMouseY, mMouseX + mMouseView.getMeasuredWidth(), mMouseY + mMouseView.getMeasuredHeight());
}
}
/**
* @param parent
* @param type
*/
public void init(ViewGroup parentView, int type) {
mParentView = parentView;
mContext = parentView.getContext();
mMouseView = new MouseView(mContext, this);
mMouseView.setOnMouseListener(this);
mCurrentType = type;
}
/**
* @return
*/
public boolean getMouseType() {
return isMouseType;
}
/**
* @return
*/
public int getCurrentActivityType() {
return mCurrentType;
}
/**
* showmouse
*/
public void showMouseView() {
ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT);
if(mMouseView != null) {
mParentView.addView(mMouseView, lp);
}
}
public boolean onDpadClicked(KeyEvent event) {
if(!isMouseType) {
return false;
}
if(event.getKeyCode() == KEYCODE_CENTER) {
dispatchKeyEventToMouse(event);
} else {
if(event.getAction() == KeyEvent.ACTION_DOWN) {
if(!isKeyEventCousumed) {
if(event.getDownTime() - mLastEventTime < defTimes) {
if(mSpeed < defMaxSpeed) {
mSpeed ++;
}
} else {
mSpeed = 1;
}
}
mLastEventTime = event.getDownTime();
dispatchKeyEventToMouse(event);
isKeyEventCousumed = true;
} else if(event.getAction() == KeyEvent.ACTION_UP) {
if(!isKeyEventCousumed){
dispatchKeyEventToMouse(event);
}
isKeyEventCousumed = false;
}
}
return true;
}
public void sendCenterClickEvent(int x, int y, int action) {
sendMotionEvent(x, y, action);
}
@SuppressLint("InlinedApi")
public void sendMouseHoverEvent(int downx, int downy) {
sendMotionEvent(downx, downy, MotionEvent.ACTION_HOVER_MOVE);
}
@TargetApi(Build.VERSION_CODES.HONEYCOMB_MR1)
@SuppressLint("NewApi")
private void sendMotionEvent(int x, int y, int action) {
MotionEvent motionEvent = getMotionEvent( x, y ,action) ;
if(action == MotionEvent.ACTION_HOVER_MOVE) {
motionEvent.setSource(InputDevice.SOURCE_CLASS_POINTER);
mMouseView.dispatchGenericMotionEvent(motionEvent);
//mParentView.dispatchGenericMotionEvent(motionEvent);
} else {
//mParentView.dispatchTouchEvent(motionEvent);
mMouseView.dispatchTouchEvent(motionEvent);
}
}
private MotionEvent getMotionEvent(int downx, int downy, int action) {
// TODO Auto-generated method stub
long downTime = SystemClock.uptimeMillis();
long eventTime = SystemClock.uptimeMillis();
int metaState = 0;
return MotionEvent.obtain(
downTime,
eventTime,
action,
downx,
downy,
metaState
);
}
@Override
public boolean onclick(View v, KeyEvent et) {
if (getMouseType()) {
return onDpadClicked(et);
}
return mParentView.dispatchKeyEvent(et);
}
public class MainActivity extends Activity {
WindowManager wm;
WindowManager.LayoutParams params;
private MouseManager mMouseManager;
public static ViewGroup contentView;
private WebView webView;
private View mLoginStatusView;
private TextView mLoaddingMessageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LayoutInflater inflater = getLayoutInflater();
contentView = (ViewGroup) inflater.inflate(R.layout.test, null);
setContentView(contentView);
init();
initMouse();
showMouse();
}
private void init() {
webView = (WebView) contentView.findViewById(R.id.web);
mLoginStatusView = this.findViewById(R.id.login_status);
mLoaddingMessageView = (TextView) this
.findViewById(R.id.login_status_message);
Button button = (Button) contentView.findViewById(R.id.btn_onclick);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "onclicked ", 1).show();
showProgress(true);
webView.setVisibility(View.VISIBLE);
webView.loadUrl("https://www.baidu.com/");
WebSettings settings = webView.getSettings();
settings.setJavaScriptEnabled(true);
webView.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view,
String url) {
view.loadUrl(url);
return true;
}
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
}
@Override
public void onReceivedError(WebView view, int errorCode,
String description, String failingUrl) {
Toast.makeText(MainActivity.this, "加载失败 ",
Toast.LENGTH_LONG).show();
super.onReceivedError(view, errorCode, description,
failingUrl);
}
});
webView.setWebChromeClient(new WebChromeClient() {
@Override
public void onProgressChanged(WebView view, int newProgress) {
// TODO Auto-generated method stub
if (newProgress == 100) {
showProgress(false);
} else {
}
}
});
}
});
}
@SuppressLint("NewApi")
private void showProgress(final boolean show) {
// On Honeycomb MR2 we have the ViewPropertyAnimator APIs, which allow
// for very easy animations. If available, use these APIs to fade-in
// the progress spinner.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {
int shortAnimTime = getResources().getInteger(
android.R.integer.config_shortAnimTime);
mLoginStatusView.setVisibility(View.VISIBLE);
mLoginStatusView.animate().setDuration(shortAnimTime)
.alpha(show ? 1 : 0)
.setListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
webView.setVisibility(show ? View.VISIBLE
: View.GONE);
}
});
webView.setVisibility(View.VISIBLE);
webView.animate().setDuration(shortAnimTime).alpha(show ? 0 : 1)
.setListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
webView.setVisibility(show ? View.GONE
: View.VISIBLE);
}
});
} else {
// The ViewPropertyAnimator APIs are not available, so simply show
// and hide the relevant UI components.
mLoginStatusView.setVisibility(show ? View.VISIBLE : View.GONE);
webView.setVisibility(show ? View.GONE : View.VISIBLE);
}
}
private void showMouse() {
mMouseManager.showMouseView();
}
public void initMouse() {
initMouseMrg();
}
public void initMouseMrg() {
mMouseManager = new MouseManager();
mMouseManager.init(contentView, MouseManager.MOUSE_TYPE);
mMouseManager.setshowMouse(true);
}
}
instrumentation发送键盘鼠标事件:Instrumentation提供了丰富的以send开头的函数接口来实现模拟键盘和鼠标,如下所述:
sendCharacterSync(int keyCode) //用于发送指定KeyCode的按键
sendKeyDownUpSync(int key) //用于发送指定KeyCode的按键
sendPointerSync(MotionEvent event) //用于模拟Touch
sendStringSync(String text) //用于发送字符串
发发送一条模拟点击事件