2016-08-16 28 views
0

我在項目中遇到問題。我有一個活動,我使用Framelayout創建了一個圓形佈局。我的問題是我不知道如何在圓形佈局中實現項目。並使用ontouch事件移動項目,並在項目出現在特定位置時顯示一個吐司顯示或警報對話框。我附上演示圖片。如何在android中使用onTouch在圈中移動視圖

enter image description here

+0

任何新聞,Pardeep? –

+0

抱歉,這段代碼無法正常工作。我想當用戶觸摸圓圈時,圓圈也會移動前字或後字。並且當用戶停止滾動時,該數字接近該活動將打開的紅色框。但是,謝謝你給我這個建議 –

回答

0

你所尋找的是一個查看實現用拖動功能的onTouch事件。

1)爲了讓您的視圖呈現圓形,您需要創建一個文件,其中包含那些規範在可繪製文件夾中。

創建繪製/ your_circle.xml

<?xml version="1.0" encoding="utf-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval" > 
    <gradient android:startColor="#FFFF0000" android:endColor="#80FF00FF" 
     android:angle="270"/> 
</shape> 

2)你認爲你想圓,背景設置爲相同的繪製像

<YourView 
    android:id="@+id/your_id" 
    android:layout_width="50dp" 
    android:layout_height="50dp" 
    android:background="@drawable/your_circle"/> 

然後,將以下程序應用於活動

public class DraggableActivity extends Activity { 

    float dX; 
    float dY; 
    int lastAction; 
    View.OnTouchListener touchListener; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.drag_view_layout); 

     // 1 - Create the touch listener 
     touchListener = new View.OnTouchListener() { 
      @Override 
      public boolean onTouch(View view, MotionEvent event) { 
       switch (event.getActionMasked()) { 
        case MotionEvent.ACTION_DOWN: 
         dX = view.getX() - event.getRawX(); 
         dY = view.getY() - event.getRawY(); 
         lastAction = MotionEvent.ACTION_DOWN; 
         break; 
        case MotionEvent.ACTION_MOVE: 
         view.setY(event.getRawY() + dY); 
         view.setX(event.getRawX() + dX); 
         lastAction = MotionEvent.ACTION_MOVE; 
         break; 
        case MotionEvent.ACTION_UP: 
         if (lastAction == MotionEvent.ACTION_DOWN) { 
          Toast.makeText(DraggableView.this, "Clicked!", Toast.LENGTH_SHORT).show(); 
         } 
         break; 

        default: 
         return false; 
       } 
       return true; 
      } 
     }; 

     // 2 - Add a reference to your view that already is stated on the layout 
     final View dragView = findViewById(R.id.your_id); 

     // 3 - Attac the the TouchListener to your view 
     dragView.setOnTouchListener(this); 
    } 
} 

讓我知道它是否有效。

Regards,

相關問題