`

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

UP 
阅读更多


实现效果: 鼠标拖动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 = Math.round(1.0F*left/width);//四舍五入  
        int toPosition = selectedPosition*width;  
        v.layout(selectedPosition*width, v.getTop(),  
                selectedPosition*width+width, v.getBottom());  
        TranslateAnimation animation = new TranslateAnimation(left-toPosition,0,0,0);  
        animation.setInterpolator(new LinearInterpolator());    
        animation.setDuration(400);  
        animation.setFillAfter(true);    
        v.startAnimation(animation); 
//        v.invalidate();
    }


全代码:
public class App extends Activity{
	private static final String tag="App";
	private Context context;
	private FrameLayout container;
	private Button btn;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        context=this;
        
        container=(FrameLayout)findViewById(R.id.container);
        
        btn=(Button)findViewById(R.id.btn);
        btn.setBackgroundResource(R.drawable.tabswitcher_short);
        btn.setOnTouchListener(touchLisener);
        btn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Log.i(tag,"btn clicked");
				
			}
		});
    }
    
    @Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
	}

	OnTouchListener touchLisener=new OnTouchListener() {
    	int lastX, lastY;
		@Override
		public boolean onTouch(View v, MotionEvent event) {
			// TODO Auto-generated method stub
			switch (event.getAction()) {
			case MotionEvent.ACTION_DOWN:
				lastX = (int) event.getRawX();
                lastY = (int) event.getRawY();
				break;
			case MotionEvent.ACTION_MOVE:
				int dx = (int) event.getRawX() - lastX;  
//				int dy = (int) event.getRawY() - lastY;  
                int dy = 0;  

                int left = v.getLeft() + dx;  
                int top = v.getTop() + dy;  
                int right = v.getRight() + dx;  
                int bottom = v.getBottom() + dy;  

                if (left < 0) {  
                    left = 0;  
                    right = left + v.getWidth();  
                }  

                if (right > container.getMeasuredWidth()) {  
                    right = container.getMeasuredWidth();  
                    left = right - v.getWidth();  
                }  

                if (top < 0) {  
                    top = 0;  
                    bottom = top + v.getHeight();  
                }  

                if (bottom > container.getMeasuredHeight()) {  
                    bottom = container.getMeasuredHeight();  
                    top = bottom - v.getHeight();  
                }  

                v.layout(left, top, right, bottom);  

                lastX = (int) event.getRawX();  
                lastY = (int) event.getRawY();
				break;
			case MotionEvent.ACTION_UP:
				setBestPosition(v);
				break;

			default:
				break;
			}
			return false;
		}
	};
	
	private void setBestPosition(View v) {
		int width=v.getWidth();
        int left = v.getLeft();
        int selectedPosition = Math.round(1.0F*left/width);//四舍五入  
        int toPosition = selectedPosition*width;  
        v.layout(selectedPosition*width, v.getTop(),  
                selectedPosition*width+width, v.getBottom());  
        TranslateAnimation animation = new TranslateAnimation(left-toPosition,0,0,0);  
        animation.setInterpolator(new LinearInterpolator());    
        animation.setDuration(400);  
        animation.setFillAfter(true);    
        v.startAnimation(animation); 
//        v.invalidate();
    }
}

布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 	xmlns:app="http://schemas.android.com/apk/res/com.ql.app"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <TextView android:layout_width="fill_parent"
		android:layout_height="wrap_content"
		android:text="TEST DRAG"
		android:textSize="20sp"
		/>
		 <FrameLayout android:id="@+id/container"
		 android:layout_width="fill_parent"
	    android:layout_height="fill_parent"
	    android:layout_weight="1"
	    >
		    <Button android:id="@+id/btn"
		    android:layout_width="80dp"
			android:layout_height="wrap_content"
			android:text="drag me!"
		    />
	    </FrameLayout>
	    <EditText 
	    android:layout_width="fill_parent"
	    android:layout_height="wrap_content"
	    />
</LinearLayout>

但是这样有个问题:当点击EditText弹出输入法的时候,那个拖动条会回到初始的位置,这是何故?
  • 大小: 8.5 KB
分享到:
评论

相关推荐

    DevExpress 在layoutcontrol内置控件上无法触发滚动解决方式

    DevExpress 在layoutcontrol内置控件上无法触发滚动 只能拖动滚动条实现滚动 17版本一下都适用 内含样式代码和视频

    android拖动控件,解决回到原点

    拖动view 每次移动都要设置其layout,不然由于父布局可能嵌套listview,当父布局发生改变冲毁(如下拉刷新时)则移动的view会回到原来的位置

    Android实现控件的缩放移动功能

    外层一个LinearLayout,里面一个自定义的控件DragScaleView,为了能够更清楚的看到控件的变化过程,就给控件加了一个灰色带虚线的边框bg_dashgap。 layout文件 &lt;?xml version=1.0 encoding=utf-8?&gt; &lt;...

    Qt widgets-基本控件使用示例

    Qt widgets-基本控件使用示例,不使用控件拖动,采用代码layout完成控件排布。 BasicLayout Basic Layouts shows how to use the standard layout managers that are available in Qt: QBoxLayout, QGridLayout, and...

    向上拖动时,可以惯性滑动显示到下一页的控件DragLayout

    NULL 博文链接:https://gundumw100.iteye.com/blog/2249642

    5个托拽布局控件.rar

    5个JS可托拽布局控件整理 分别是jquery.dad.min.js、layoutitit、dnd-master、jQuery支持拖动的div布局、jquery.dad.js-master

    Android即时贴控件StickerView.zip

    StickerView是Android上的一个可以进行添加,缩放,拖动,删除操作的即时贴控件。如图所示:‍用法:‍StickerView 是基于 ImageView 布局的扩展。   android:id="@ id/sticker_view"  android:layout_width=...

    bootstarp拖拽布局例子

    bootstarp拖拽布局例子,框架官网http://layoutit.justjavac.com/#close,实现类似QQ空间小窝布局模式下的自由布局功能。

    Android代码-Android 层叠卡片控件,仿"探探app"

    效果图 功能 自定义卡片的堆叠效果 自定义卡片移除动画 ...父布局使用clipChildren="false", 使之能全屏拖动 ... android:clipChildren="false"&gt; &lt;com.fashare.stack_layout.StackLayout android:

    悬浮窗的使用

    悬浮窗可以显示在所有应用程序之上,不管在PC机还是Android设备上都有这个,最常见的是360的“加速球”

    Android-Android支持拖拽排序的流式标签布局

    Android 支持拖拽排序的流式标签布局 

    flex3的cookbook书籍完整版dpf(包含目录)

    载入并显示图像 8.2节. 创建视频显示 8.3节.Mp3文件的播放和暂停 8.4节. 为音频文件创建进度搜索条 8.5节. 融合两幅图像 8.6节. 将Convolution滤镜应用于图像 8.7节. 通过摄像头将视频发送到FMS实例 8.8节. 访问...

    ExtAspNet v2.2.1 (2009-4-1) 值得一看

    -Button控件将不再自动拥有display:inline属性,如果希望两个按钮在一行显示,请为第一个按钮设置CssStyle="float:left;"属性。 -修正了弹出菜单的位置在Firefox下不正确的BUG(feedback:eroach)。 -为TriggerBox...

    Android控件之SlidingDrawer(滑动式抽屉)详解与实例分享

    它可以垂直或水平滑动,它有俩个View组成,其一 是可以拖动的handle,其二是隐藏内容的View.它里面的控件必须设置布局,在布局文件中必须指定handle和content.例如下面 代码如下:&lt;SlidingDrawer android:layout_...

    ExtAspNet_v2.3.2_dll

    -Button控件将不再自动拥有display:inline属性,如果希望两个按钮在一行显示,请为第一个按钮设置CssStyle="float:left;"属性。 -修正了弹出菜单的位置在Firefox下不正确的BUG(feedback:eroach)。 -为TriggerBox...

    android开发实例大全_王东华

    实例053: 在屏幕中拖动一个按钮 157 第4章 数据存储实例集锦 163 实例054: 在屏幕中显示SharedPreferences 中存储的信息 163 实例055: 演示数据添加、删除等操作 165 实例056: 编写一个手机日记本程序 170 ...

    Android中SeekBar拖动条控件使用方法详解

    SeekBar拖动条控件使用方法,具体内容如下 一、简介 1、  二、SeekBar拖动条控件使用方法 1、创建SeekBar控件 android:layout_width=match_parent android:layout_height=wrap_content android:progress=30 &gt;...

    Android 将 android view 的位置设为右下角的解决方法

    默认情况下,我们在eclipse中拖动控件到editor中,控件的位置将位于整个屏幕的左上角。可以使用 android:layout_gravity、android:gravity和android:width三个属性值,实现控件的九宫格定位。  Xml代码 代码如下:...

Global site tag (gtag.js) - Google Analytics