亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

拖動一個控件在另一個控件(layout)上,并固定

系統 2069 0
拖動一個控件在另一個控件(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 = 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彈出輸入法的時候,那個拖動條會回到初始的位置,這是何故?

拖動一個控件在另一個控件(layout)上,并固定位置在幾個位置顯示


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 中文字幕亚洲无线码在线一区 | 波霸欧美性猛交xxxxxx | 久久综合噜噜激激的五月天 | 看一级毛片| 亚洲成a人一区二区三区 | 国产亚洲精品日韩综合网 | 久久久久久久免费 | 久久免费香蕉视频 | 亚洲乱码在线播放 | 久久99精品久久久久久三级 | 尤物免费视频 | 日日噜噜夜夜狠狠tv视频免费 | 黄视频网站观看 | 妞干网这里只有精品 | 久久久综合九色合综国产 | 狠狠色噜噜噜噜狠狠狠狠狠狠奇米 | 青青草国产三级精品三级 | 午夜操 | 日韩毛片一级 | 成人97| 久久久噜噜噜www成人网 | 久99久精品免费视频热77 | 国产欧美成人不卡视频 | 黄色午夜影院 | 九月婷婷天天澡天天添天天爽 | 亚洲精品国产一区二区 | 中文字幕51精品乱码在线 | 日本色婷婷 | 免费在线精品视频 | 亚洲欧美不卡中文字幕 | 欧美日韩一区二区综合在线视频 | 亚洲婷婷综合中文字幕第一页 | 亚洲不卡视频在线 | 久久99国产视频 | 91中文字幕在线一区 | 97成人在线视频 | 夜夜爽天天狠狠九月婷婷 | 亚洲精品久久婷婷爱久久婷婷 | 美女撒尿毛片免费看 | 久久成人激情视频 | 成人欧美一区二区三区 |