【问题标题】:Layout with dynamic position具有动态位置的布局
【发布时间】:2010-09-10 10:02:39
【问题描述】:

我的布局可能有点不寻常。结构如下:

<?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>

我的自定义相对布局:

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. 绑定到 mLinearLayout(在 CustomRelativeLayout 中)的 onTouchListener 仅触发一次,并且仅针对 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;
        }
    });
}

我还发现了一个错误/功能/问题,这对我来说绝对是新的:当我删除笔记根元素(当前是透明的)中的背景时,笔记仅在我设置的 200dp 宽度/高度内可见在内部的线性布局中。所以它不是fill_parent,而是wrap_content,即使我将它设置为fill_parent。所以layout_widthlayout_height 只在设置背景时使用给定的fill_parent...奇怪...

【问题讨论】:

    标签: android layout surfaceview ontouchlistener


    【解决方案1】:

    问题是您在 onTouch 事件中返回 false - 所以,您基本上是在告诉它您对任何后续事件都不感兴趣。

    将其更改为返回 true。

    【讨论】:

    • 对... smackmyhead 但是第二个问题仍然存在...它永远不会改变位置,因为永远不会调用onDraw。我暂时通过在 SurfaceView 正在运行的同一线程中调用 onDraw 来解决此问题。难道没有更好的方法使用invalidate()吗?
    • 我根据您对我的问题的回答添加了我的解决方案。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-04
    • 1970-01-01
    • 2015-12-07
    • 1970-01-01
    相关资源
    最近更新 更多