拖动一个控件在另一个控件(layout)上,并固定位置在几个位置显示



实现效果: 鼠标拖动btn SSS,SSS在水平的layout上移动。 当鼠标抬起 响应UP事件。SSS会自动移动到距离其最近的Btn上,与其重合。即SSS如图只存在五个固定的显示位置。

SSS响应setOnTouchListener事件。

在MotionEvent.ACTION_UP事件中,调用TranslateAnimation动画效果,将其从UP事件位置移动到最近的btn所在位置。

即在UP事件中,响应函数:

private void setPosition() {
            int positionPixel = (touchBtn.getLeft()+touchBtn.getRight())/2;
            int positionIndex = (positionPixel)/btn[1].getWidth();
            int toPosition = positionIndex*btn[1].getWidth()+touchBtn.getWidth()/2;        
            touchBtn.layout(positionIndex*btn[1].getWidth(), touchBtn.getTop(),positionIndex*btn[1].getWidth()+touchBtn.getWidth(), 
                            touchBtn.getBottom());            
            MoveAction = new TranslateAnimation(positionPixel - toPosition,0,0,0);
            MoveAction.setDuration(500);
            touchBtn.startAnimation(MoveAction);
//            touchBtn.invalidate();            
        }


动画效果,将其移动到最近位置上

或者也可以这样计算:

/**
*获得最佳停留位置
*/
private void setBestPosition(View v) {
		int width=v.getWidth();
        int left = v.getLeft();
        int selectedPosition = 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值