我的布局可能有点不寻常。结构如下:具有动态位置的布局
<?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小,首先应按照我触摸“幻灯片” ......
有两个问题:
- 的onTouchListener结合mLinearLayout(在CustomRelativeLayout)只触发一次,只为ACTION_DOWN。我只看到日志输出一次
- 注释从不改变位置,它永远不会被改变的矩阵重绘...我看到在
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_width
和layout_height
只用给定fill_parent
当背景设置... weired ...
right ... * smackmyhead *但第二个问题仍然存在......它从不改变位置,因为'onDraw'永远不会被调用。我现在通过在SurfaceView运行的同一个线程中调用'onDraw'来解决这个问题。有没有更好的方法使用'invalidate()'? – WarrenFaith 2010-09-10 11:14:27
嗯,是一个UI线程?如果没有,然后尝试postInvalidate() – xil3 2010-09-10 11:27:07
我添加了我的解决方案根据您对我的问题的答案。谢谢! – WarrenFaith 2010-09-10 12:10:48