2010-09-10 129 views
5

我的佈局可能有點不尋常。結構如下:具有動態位置的佈局

<?xml version="1.0" encoding="utf-8"?> 
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" > 

    <org.CustomSurfaceView 
     android:id="@+id/page_flip" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_marginBottom="20dip" /> 

    <org.customRelativeLayout 
     android:id="@+id/note" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:visibility="gone" /> 
</FrameLayout> 

我在XML底部customRelativeLayout有一個XML佈局太:

<?xml version="1.0" encoding="UTF-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="200dp" 
    android:layout_height="200dp" 
    android:background="@drawable/my_background" 
    android:id="@+id/note_layout"> 
    <TextView android:id="@+id/note" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:singleLine="false" 
     android:layout_margin="5dp" 
     android:textColor="#000000" /> 
</LinearLayout> 

我CustomRelativeLayout:

public class CustomRelativeLayout extends RelativeLayout implements OverlayView { 

    private TextView mNoteText; 
    private LinearLayout mLinearLayout; 
    public int mX = 0; 
    public int mY = 0; 
    public boolean mShow; 

    public CustomRelativeLayout(Context context, AttributeSet attrs) { 
     super(context, attrs); 

     LayoutInflater li = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     li.inflate(R.layout.note, this); 
     mNoteText = (TextView) findViewById(R.id.note); 
     mLinearLayout = (LinearLayout) findViewById(R.id.note_layout); 

     mLinearLayout.setOnTouchListener(new OnTouchListener() { 
      @Override 
      public boolean onTouch(View v, MotionEvent event) { 
       Log.e("touched"); 
       mX = (int) event.getX() - 100; 
       mY = (int) event.getY() - 100; 
       invalidate(); 
       return false; 
      } 
     }); 
    } 

    @Override 
    public void hide() { 
     mShow = false; 
     setVisibility(View.GONE); 
    } 

    @Override 
    public boolean isVisible() { 
     return isVisible(); 
    } 

    @Override 
    protected void onDraw(Canvas canvas) { 
     Log.e("Drawing the postit"); 
     Matrix matrix = new Matrix(); 
     matrix.postTranslate(mX, mY); 
     canvas.setMatrix(matrix); 
     super.onDraw(canvas); 
    } 

    @Override 
    public void setContent(Object content) { 
     if (content != null && content instanceof Content) { 
      Content noteContent = (Content) content; 
      if (noteContent.getText() != null) { 
       mNoteText.setText(noteContent.getText()); 
      } else { 
       mNoteText.setText(""); 
      } 
     } 
     mNoteText.invalidate(); 
     mLinearLayout.invalidate(); 
    } 

    @Override 
    public void show() { 
     mShow = true; 
     setVisibility(View.VISIBLE); 
    } 
} 

我想要做什麼:我希望能夠改變我的CustomRelativeLayout的位置。它應該跟隨我的觸摸。它比SurfaceView小,首先應按照我觸摸「幻燈片」 ......

有兩個問題:

  1. 的onTouchListener結合mLinearLayout(在CustomRelativeLayout)只觸發一次,只爲ACTION_DOWN。我只看到日誌輸出一次
  2. 註釋從不改變位置,它永遠不會被改變的矩陣重繪...我看到在onDraw中所述的輸出永遠不會發生觸摸事件。

首先可能是因爲SurfaceView還處理觸摸事件。但我想如果CustomRelativeLayout首先處理它,它應該可以工作。

有些想法?

編輯爲解決

感謝Xil3我能刪除我碰到...這裏是我的解決方案牆:

我記的xml:

<?xml version="1.0" encoding="UTF-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="@android:color/transparent"> 
    <LinearLayout android:layout_width="200dp" 
     android:layout_height="200dp" 
     android:background="@drawable/postit" 
     android:id="@+id/textnote_layout"> 
     <TextView android:id="@+id/textnote_content" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:singleLine="false" 
      android:layout_margin="5dp" 
      android:textColor="#000000" /> 
    </LinearLayout> 
</LinearLayout> 

這是我的構造函數:

public TextNoteOverlay(Context context, AttributeSet attrs) { 
    super(context, attrs); 

    LayoutInflater li = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    li.inflate(R.layout.textnote, this); 
    mNoteText = (TextView) findViewById(R.id.textnote_content); 
    mLinearLayout = (LinearLayout) findViewById(R.id.textnote_layout); 

    setOnTouchListener(new OnTouchListener() { 
     @Override 
     public boolean onTouch(View v, MotionEvent event) { 
      mLinearLayout.getHitRect(mNoteRect); 
      if (mNoteRect.contains((int) event.getX(), (int) event.getY())) { 
       if (event.getAction() == MotionEvent.ACTION_DOWN) { 
        mStartX = (int) event.getX() - mLinearLayout.getLeft(); 
        mStartY = (int) event.getY() - mLinearLayout.getTop(); 
        return true; 
       } 
       mX = (int) event.getX() - mStartX; 
       mY = (int) event.getY() - mStartY; 
       mLinearLayout.layout(mX, mY, mX + mLinearLayout.getWidth(), mY + mLinearLayout.getHeight()); 
       return true; 
      } 
      return false; 
     } 
    }); 
} 

我還發現了一個bug /功能/問題,這對我來說是全新的:當我在筆記根元素(當前是透明的)中移除背景時,該筆記僅在內部設置的200dp寬度/高度內可見LinearLayout中。所以它不是fill_parent,它的wrap_content即使我把它設置爲fill_parent。所以layout_widthlayout_height只用給定fill_parent當背景設置... weired ...

回答

2

的問題是,你正在返回的onTouch事件假 - 所以,你基本上是告訴它你」對任何後續事件不感興趣。

將其更改爲true。

+0

right ... * smackmyhead *但第二個問題仍然存在......它從不改變位置,因爲'onDraw'永遠不會被調用。我現在通過在SurfaceView運行的同一個線程中調用'onDraw'來解決這個問題。有沒有更好的方法使用'invalidate()'? – WarrenFaith 2010-09-10 11:14:27

+0

嗯,是一個UI線程?如果沒有,然後嘗試postInvalidate() – xil3 2010-09-10 11:27:07

+0

我添加了我的解決方案根據您對我的問題的答案。謝謝! – WarrenFaith 2010-09-10 12:10:48