Android 仿360桌面小人

首先自定义FloatsWindowView,用于显示动画小人。

 

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.AnimationDrawable;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewTreeObserver.OnPreDrawListener;
import android.view.WindowManager;
import android.widget.Toast;

@SuppressLint("ClickableViewAccessibility")
public class FloatsWindowView extends View {

	private Context mContext = null;
	private WindowManager mWindowMgr = null;
	private WindowManager.LayoutParams mWindowMgrParams = null;
	private AnimationDrawable mAnimationDrawable = null;

	private int iPosX = 0;
	private int iPosY = 0;
	private int iLastPosX = 0;
	private int iLastPosY = 0;
	private boolean bMoved = false;

	public FloatsWindowView(Context context) {
		this(context, null, 0);
	}

	public FloatsWindowView(Context context, AttributeSet attrs) {
		this(context, attrs, 0);
	}

	@SuppressWarnings("deprecation")
	public FloatsWindowView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);

		mContext = context;
		mWindowMgr = (WindowManager) getContext().getApplicationContext()
				.getSystemService("window");
		mWindowMgrParams = new WindowManager.LayoutParams();
		initParams();

		mAnimationDrawable = new AnimationDrawable();
		for (int i = 0; i < 4; i++) {
			int id = getResources().getIdentifier("a" + i, "drawable",
					mContext.getPackageName());
			mAnimationDrawable.addFrame(getResources().getDrawable(id), 100);
		}
		mAnimationDrawable.setOneShot(false);
		this.setBackgroundDrawable(mAnimationDrawable);

		OnPreDrawListener listener = new OnPreDrawListener() {
			@Override
			public boolean onPreDraw() {
				mAnimationDrawable.start();
				return true;
			}
		};
		this.getViewTreeObserver().addOnPreDrawListener(listener);
	}

	private void initParams() {
		DisplayMetrics dm = getResources().getDisplayMetrics();
		mWindowMgrParams.x = dm.widthPixels - 136;
		mWindowMgrParams.y = 300;
		mWindowMgrParams.width = 136;
		mWindowMgrParams.height = 136;
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {

		switch (event.getAction()) {
		case MotionEvent.ACTION_DOWN:
			iPosX = (int) event.getX();
			iPosY = (int) event.getY();
			bMoved = false;
			break;

		case MotionEvent.ACTION_MOVE:
			bMoved = true;
			iLastPosX = (int) event.getX();
			iLastPosY = (int) event.getY();
			updatePostion(iLastPosX - iPosX, iLastPosY - iPosY);
			break;

		case MotionEvent.ACTION_UP:
			if (!bMoved) {
				Intent it = new Intent(mContext, MainActivity.class);
				mContext.startActivity(it);
			}
			break;

		default:
			break;
		}

		if (event.getAction() == MotionEvent.ACTION_MOVE) {
			getParent().requestDisallowInterceptTouchEvent(true);
		}

		if (event.getAction() == MotionEvent.ACTION_UP) {
			if ((iPosX == iLastPosX) && (iPosY == iLastPosY)) {
				singleClick();
				return true;
			}
		}
		return true;
	}

	private void updatePostion(int x, int y) {
		mWindowMgrParams.type = 2003;
		mWindowMgrParams.format = 1;
		mWindowMgrParams.flags = 40;
		mWindowMgrParams.gravity = Gravity.LEFT | Gravity.TOP;
		mWindowMgrParams.x += x;
		mWindowMgrParams.y += y;
		mWindowMgr.updateViewLayout(this, mWindowMgrParams);
	}

	public void singleClick() {
		Toast.makeText(mContext, "点击了阿狸,哇咔咔咔", Toast.LENGTH_SHORT).show();
	}
}

  

 

然后在Activity中添加自定义小人,即可,可以处理小人的点击逻辑,滑动逻辑。

 

import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.WindowManager;
import android.app.Activity;
import android.content.Context;

public class MainActivity extends Activity {

	private static WindowManager mWindowMgr = null;
	private WindowManager.LayoutParams mWindowMgrParams = null;
	private static FloatsWindowView mFloatsWindowView = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
	}

	@Override
	protected void onStop() {
		super.onStop();
	}

	@Override
	public void onWindowFocusChanged(boolean hasFocus) {
		getWindowLayout();
	}

	private void initParams() {
		DisplayMetrics dm = getResources().getDisplayMetrics();
		mWindowMgrParams.x = dm.widthPixels - 136;
		mWindowMgrParams.y = 300;
		mWindowMgrParams.width = 136;
		mWindowMgrParams.height = 136;
	}

	private void getWindowLayout() {
		if (mFloatsWindowView == null) {
			mWindowMgr = (WindowManager) getBaseContext().getSystemService(
					Context.WINDOW_SERVICE);
			mWindowMgrParams = new WindowManager.LayoutParams();

			/*
			 * 2003 在指悬浮在所有界面之上 (4.0+系统中,在下拉菜单下面,而在2.3中,在上拉菜单之上)
			 */
			mWindowMgrParams.type = 2003;
			mWindowMgrParams.format = 1;

			/*
			 * 代码实际是wmParams.flags |= FLAG_NOT_FOCUSABLE;
			 * 40的由来是wmParams的默认属性(32)+ FLAG_NOT_FOCUSABLE(8)
			 */
			mWindowMgrParams.flags = 40;
			mWindowMgrParams.gravity = Gravity.LEFT | Gravity.TOP;
			initParams();

			mFloatsWindowView = new FloatsWindowView(this);
			mWindowMgr.addView(mFloatsWindowView, mWindowMgrParams);
		}
	}

}

  

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。