`
AndroidLL
  • 浏览: 74867 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

自定义解锁滑动按钮

 
阅读更多

 

   实现滑动解锁按钮,附带按钮滑动音效。

public class SlidingButton extends Button {

	float offset;

	public SlidingButton(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
	}

	public SlidingButton(Context context) {
		super(context);
	}

	private boolean isMe = false;
	private int limit = 302;

	public int getLimit() {
		return limit;
	}

	public void setLimit(int limit) {
		this.limit = limit;
	}

	public boolean isMe() {
		return isMe;
	}

	public void setMe(boolean isMe) {
		this.isMe = isMe;
	}

	public SlidingButton(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		if (event.getAction() == MotionEvent.ACTION_DOWN) {
			offset = event.getX();
			isMe = true;
		} else if (event.getAction() == MotionEvent.ACTION_UP) {
			isMe = false;
		}
		return false;
	}

	public boolean handleActivityEvent(MotionEvent activityEvent) {
		boolean result = false;
		if (isMe()) {
			if (activityEvent.getAction() == MotionEvent.ACTION_UP) {

				int newX = (int) activityEvent.getX();

				if (newX < getLimit() && newX > 100) {
					LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) getLayoutParams();
					TranslateAnimation trans = new TranslateAnimation(
							Animation.RELATIVE_TO_SELF, 0, Animation.ABSOLUTE,
							-lp.leftMargin, Animation.RELATIVE_TO_SELF, 0,
							Animation.RELATIVE_TO_SELF, 0);
					trans.setStartOffset(0);
					trans.setDuration(500);
					trans.setFillBefore(true);
					trans.setAnimationListener(new SlidingAnimationListener(
							this));
					startAnimation(trans);
				} else if (newX > getLimit()) {
					//SoundPlayer.getInstance().play(SoundPlayer.SLIDE);
					LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) getLayoutParams();
					lp.leftMargin = 0;
					setLayoutParams(lp);
					isMe = false;
					//用户完成了选择动作
					result = true;
				}

			} else {
				// 还在拖动
				LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) getLayoutParams();
				lp.leftMargin = (int) (activityEvent.getX() - offset-5);
				System.out.println(lp.leftMargin);
				if (lp.leftMargin > 0 && lp.leftMargin < (367-149)) {
					setLayoutParams(lp);
				}
			}
		}
		return result;

	}

	private static class SlidingAnimationListener implements AnimationListener {

		private SlidingButton but;

		public SlidingAnimationListener(SlidingButton button) {
			this.but = button;
		}

		@Override
		public void onAnimationEnd(Animation animation) {
			rePosition();
			but.setMe(false);
		}

		private void rePosition() {
			LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) but
					.getLayoutParams();
			lp.leftMargin = 0;
			but.setLayoutParams(lp);
		}

		@Override
		public void onAnimationRepeat(Animation animation) {
			// TODO Auto-generated method stub

		}

		@Override
		public void onAnimationStart(Animation animation) {
			// TODO Auto-generated method stub

		}

	}

}
public class SoundPlayer {
	public static int BTN;
	public static int LOOP;

	private SoundPool soundPool;
	private int loopStreamId;

	private static SoundPlayer instance;

	public static SoundPlayer getInstance() {
		synchronized (SoundPlayer.class) {
			if (instance == null) {
				instance = new SoundPlayer();
			}
		}
		return instance;
	}

	// 单例
	private SoundPlayer() {
		soundPool = new SoundPool(10, AudioManager.STREAM_MUSIC, 0);
	}

	public void loadSound(Context context) {
		BTN = soundPool.load(context, R.raw.button, 1);
		LOOP = soundPool.load(context, R.raw.loop, 1);
	}

	public void play(int soundId) {
		if (soundId == LOOP) {
			// 循环播放
			soundPool.stop(loopStreamId);
			loopStreamId = soundPool.play(soundId, 1f, 1f, 1, -1, 1f);
		} else {
			// 只播放一次
			soundPool.play(soundId, 1f, 1f, 1, 0, 1f);
		}
	}
	
	public void stop() {
		soundPool.stop(loopStreamId);
	}

}
 

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics