仿iOS 的AssistiveTouch 按键 Floating Action Button

仿iOS 的AssistiveTouch 按键 Floating Action Button
FloatButton fb = new FloatButton(this);
		fb.show();
		fb.setClickActionListener(new ClickActionListener(){

			@Override
			public void onClickAction() {
				
			}});



import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewConfiguration;
import android.view.WindowManager;
import android.widget.ImageView;

import com.pingan.haoche.app.R;
/**
 *
 * float button in window
 * <p>
 * Need implement ClickActionListener for response click event
 *  <p>
 *  @author Kingz 2015-4-14
 *
 */
public class FloatButton implements OnTouchListener {
	
	private Context mContext;
	private ImageView mFloatContent;
	private boolean mIsShowing;
	private int mImageWidth;
	private int mImageHeight;
	private WindowManager mWindowManager;
	private WindowManager.LayoutParams mWinManagerParams;
	private int touchSlop;
	private ClickActionListener clickActionListener;

	public FloatButton(Context context) {
		mContext = context;
		mWindowManager = (WindowManager) context
				.getSystemService(Context.WINDOW_SERVICE);
		touchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
		creatFloatView();
		createParams();
	}


	private void creatFloatView() {
		mFloatContent = new ImageView(mContext);
		Drawable fudr = mContext.getResources().getDrawable(
				R.drawable.floatbutton);
		mImageWidth = (int) (fudr.getIntrinsicWidth());
		mImageHeight = (int) (fudr.getIntrinsicHeight());
		mFloatContent.setImageDrawable(fudr);
		mFloatContent.setVisibility(View.INVISIBLE);
		mFloatContent.setOnTouchListener(this);
		mFloatContent.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				clickActionListener.onClickAction();
			}

		});
	}
	private void createParams() {
		int dY = 10;
		mWinManagerParams = new WindowManager.LayoutParams();
		mWinManagerParams.type = WindowManager.LayoutParams.TYPE_APPLICATION;
		mWinManagerParams.format = android.graphics.PixelFormat.TRANSPARENT;
		mWinManagerParams.flags |= WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
		mWinManagerParams.flags |= WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH;
		mWinManagerParams.gravity = Gravity.LEFT | Gravity.BOTTOM;
		mWinManagerParams.x = mDisWid - dY - mImageWidth;
		mWinManagerParams.y = dY + mImageHeight;
		mWinManagerParams.width = 75;
		mWinManagerParams.height = 75;
	}

	private float mTouchStartX;
	private float mTouchStartY;
	private float x;
	private float y;
	private boolean hasMoved = false;
	private int lasteventX = 0;
	private int lasteventY = 0;
	private int mContainerHeight = 0;
	private int mDisWid;

	@Override
	public boolean onTouch(View v, MotionEvent event) {
		x = event.getRawX();
		y = event.getRawY();

		switch (event.getAction()) {
		case MotionEvent.ACTION_DOWN: {

			hasMoved = false;
			mTouchStartX = event.getX();
			mTouchStartY = event.getY();
			lasteventX = (int) (x - mTouchStartX);
			lasteventY = (int) (y - mTouchStartY);
			mContainerHeight = lasteventY + mWinManagerParams.y;

			break;
		}
		case MotionEvent.ACTION_MOVE: {
			updateViewPosition();
			break;
		}
		case MotionEvent.ACTION_UP: {
			mTouchStartX = mTouchStartY = 0;
			if (hasMoved) {
				return true;
			}
			break;
		}
		}
		return false;
	}

	private void updateViewPosition() {
		int touchX = (int) (x - mTouchStartX);
		int touchY = (int) (y - mTouchStartY);

		int step = touchSlop;
		if (hasMoved || Math.abs(lasteventX - touchX) > step
				|| Math.abs(lasteventY - touchY) > step) {
			mWinManagerParams.x = touchX;
			mWinManagerParams.y = mContainerHeight - touchY;
			mWindowManager.updateViewLayout(mFloatContent, mWinManagerParams);

			hasMoved = true;
			lasteventX = touchX;
			lasteventY = touchY;
		}
	}

	public void show() {
		if (mIsShowing) {
			return;
		}
		mFloatContent.setVisibility(View.VISIBLE);
		if (mWindowManager != null && !mIsShowing) {
			mWindowManager.addView(mFloatContent, mWinManagerParams);
			mIsShowing = true;
		}

	}
	
	public interface ClickActionListener{
		void onClickAction();
	}
	public void setClickActionListener(ClickActionListener listener){
		clickActionListener = listener;
	}

}



你可能感兴趣的:(action)