2010-04-08 98 views
6

我正在研究需要自定義拖放功能的東西,因此我一直在對子視圖進行分類,在響應觸摸事件時做了一堆數學運算,然後通過手動方式呈現所有內容onDraw在畫布上的代碼。現在,我添加的功能越多,代碼失控越多,我發現自己編寫的代碼比我期望在Android等高級環境中編寫的代碼多得多。在Android中拖放+自定義繪圖

這是怎麼做的,或者我錯過了什麼?如果我沒有在UI中做任何事情,框架就可以處理我的大部分交互。內置的控件處理觸摸和拖動,而我的代碼幾乎侷限於業務邏輯和數據。有沒有辦法利用一些UI控件和動畫等東西的強大功能,同時還可以在onDraw畫布中手動執行一些操作?有什麼時候使用這兩種方法的公認標準(如果兩種方法的確可以混合使用)?

+0

在我的遊戲中,當處理幾乎任何類型的動畫和自定義觸摸界面時,我肯定必須編寫大量的代碼,數學,檢查等。也許有更簡單的方法,但做這種事情似乎並不罕見。 – 2010-04-08 21:22:25

+0

很酷...欣賞反饋。我認爲我可以通過使用ViewGroup並將我自定義繪製的視圖與其他標準元素混合在一起來做出時髦的混合東西,但我真的不想花時間玩這些東西,因爲我已經有了一點動力走的很遠。 – Rich 2010-04-08 23:46:50

回答

7

我使用拖放在我的音樂播放器應用程序!我向用戶提供將歌曲從播放列表移動到其他播放列表的功能。這對用戶來說非常好,很簡單。當用戶長時間輕敲一首歌曲或選擇菜單中的選項時,我開始爲我的視圖拖動事件! 這是我的課:

package com.liviu.app.smpp.gui; 

import android.content.Context; 
import android.util.Log; 
import android.view.LayoutInflater; 
import android.view.MotionEvent; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.RelativeLayout; 
import android.widget.TextView; 

import com.liviu.app.smpp.R; 
import com.liviu.app.smpp.listeners.CollisionListener; 

public class SongItemView extends RelativeLayout implements OnClickListener { 
// data 
private String TAG = "SongItemView"; 
private Context context; 
private LayoutInflater lInflater; 
private String title; 
private int id; 
private int maxHeight = 410; 
private int mCurX; 
private int mCurY; 

//listeners 
private CollisionListener onCollisionListener = null; 

// views 
private View v; 

public SongItemView(Context ctx, String title_, int id_) { 
    super(ctx); 

    context = ctx; 
    lInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    v   = lInflater.inflate(R.layout.song_item_view, null); 
    title  = title_; 
    id  = id_; 

    ((TextView)v.findViewById(R.id.siv_title)).setText(title);  

    addView(v, new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); 
} 

@Override 
public void onClick(View v) { 
    Log.e(TAG, "clicked! " + ((TextView)v.findViewById(R.id.piv_title)).getText().toString()); 
} 

public View getView(){ 
    return v; 
} 

public String getPlsName() { 
    return title; 
} 

public int getID() { 
    return id; 
} 

public void setTitle(String title_){ 
    ((TextView)v.findViewById(R.id.siv_title)).setText(title_); 
    title = title_; 
} 

public void setID(int id_) { 
    id = id_; 
} 

@Override 
public boolean dispatchTouchEvent(MotionEvent event) { 
    mCurX = (int) event.getRawX(); 
    mCurY = (int) event.getRawY(); 

    int action = event.getAction();   

    if (action == MotionEvent.ACTION_MOVE) 
    {  
     RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);      
     params.leftMargin = mCurX; 
     params.topMargin = mCurY; 
     this.setLayoutParams(params); 

     if(this.getTop() >= maxHeight) 
     { 
      Log.e(TAG, "Collision!!!!"); 
      if(onCollisionListener != null){ 
        onCollisionListener.onCollision(this);     
      } 
     }     
    } 

    return true; 
} 

public void setOnCollisionListener(CollisionListener listener){ 
    onCollisionListener = listener; 
} 

public void setMaxHeight(int height){ 
    maxHeight = height; 
} 

public int getmCurX() { 
    return mCurX; 
} 

public int getmCurY() { 
    return mCurY; 
} 

public int getMaxHeight() { 
    return maxHeight; 
} 

}

我希望這將有助於一點。

謝謝!

+0

我喜歡這個想法。我將嘗試通過在視圖頂部創建RelativeLayout來實現拖放,然後攔截longtouch並移動。我想這也是你在這裏做的。靈感的好主意! – Peterdk 2010-12-14 17:47:46