2016-02-22 72 views
1

我想在android中使用滑動圖片按鈕,就像android中的鬧鐘屏幕一樣,請幫助我完成此操作。 謝謝。 screenshot of what I want to createAndroid中的滑動圖像按鈕

+0

顯示你的努力 –

+0

[檢查](https://androidician.wordpress.com/2014/09/24/android-custom-toggle-button-example-ios-like-toggle-buttons/) – Skynet

+0

@Skynet the你給的參考只是一個togglebutton,他想要不同的東西! –

回答

0

您可以爲它定製一個ontouchlistener並相應地設置您的x和y位置。

下面是它的一個示例。

https://stackoverflow.com/a/19222290/5193608

主要代碼部分來自它(如果鏈接失效):

public class MultiTouchListener implements OnTouchListener 
{ 

private float mPrevX; 
private float mPrevY; 

public MainActivity mainActivity; 
public MultiTouchListener(MainActivity mainActivity1) { 
    mainActivity = mainActivity1; 
} 

@Override 
public boolean onTouch(View view, MotionEvent event) { 
    float currX,currY; 
    int action = event.getAction(); 
    switch (action) { 
     case MotionEvent.ACTION_DOWN: { 

      mPrevX = event.getX(); 
      mPrevY = event.getY(); 
      break; 
     } 

     case MotionEvent.ACTION_MOVE: 
     { 

       currX = event.getRawX(); 
       currY = event.getRawY(); 


       MarginLayoutParams marginParams = new MarginLayoutParams(view.getLayoutParams()); 
       marginParams.setMargins((int)(currX - mPrevX), (int)(currY - mPrevY),0, 0); 
       RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(marginParams); 
       view.setLayoutParams(layoutParams); 


      break; 
     } 



     case MotionEvent.ACTION_CANCEL: 
      break; 

     case MotionEvent.ACTION_UP: 

      break; 
    } 

    return true; 
} 

} 

現在您的主要活動,設置OnTouchListener對你的看法...這是你的ImageButton或ImageView的;

MultiTouchListener touchListener=new MultiTouchListener(this); 
onButton.setOnTouchListener(touchListener); 

希望它有幫助!