android视图正确绘制

时间:2018-01-24 04:34:59

标签: android xml view draw

的xml:

    <?xml version="1.0" encoding="utf-8"?>
    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/frame_living_root_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
       >

        <SurfaceView
            android:id="@+id/player_surface"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_centerInParent="true" />


        <android.opengl.GLSurfaceView
            android:id="@+id/camera_preview"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_alignParentBottom="true"
            android:layout_alignParentTop="true"
            android:visibility="gone" />


        <com.netease.nimlib.sdk.avchat.model.AVChatSurfaceViewRenderer
            android:id="@+id/video_render"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:visibility="gone" />


        <com.netease.nimlib.sdk.avchat.model.AVChatSurfaceViewRenderer
            android:id="@+id/bypass_video_render"
            android:layout_width="110dip"
            android:layout_height="150dip"
            android:layout_gravity="right|end|bottom"
            android:layout_marginBottom="70dip"
            android:layout_marginEnd="10dip"
            android:layout_marginRight="10dip"
            android:visibility="gone" />


     <!--root view is FrameLayout with 40dip layout_margin and is a little complex-->
        <include layout="@layout/layout_live_top" />


    <!-- the content of this view will be scroll by touch event-->
        <com.example.SimpleSlideView
            android:id="@+id/slide_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
<!-- yellow background-->
            <ImageView
                android:id="@+id/yellow_child_view"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="#ff0" />
        </com.example.SimpleSlideView>
    </FrameLayout>

SimpleSlideView:

public class SimpleSlideView extends FrameLayout {

    public int screenWidth;
    public int screenHeight;
    private float lastY;

    public SimpleSlideView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initScreenSize(context);
        scrollTo(0, screenHeight);
    }

    private void initScreenSize(@NonNull Context context) {
        DisplayMetrics dm = context.getApplicationContext().getResources().getDisplayMetrics();
        screenWidth = dm.widthPixels;
        screenHeight = dm.heightPixels;
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {

        float rawY = ev.getRawY();

        switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN:
                lastY = rawY;
                break;
            case MotionEvent.ACTION_MOVE:
                final int delta = (int) (rawY - lastY);
                scrollTo(0, getScrollY() - (delta));
                lastY = rawY;
                break;
            case MotionEvent.ACTION_UP:
                scrollTo(0, screenHeight);
                break;
        }

        return true;
    }
}

我想编写一个SimpleSlideView,可以通过下拉滚动内容来简化内容SimpleSlideView在轻量级XML中按预期工作,但在复杂的XML中有点奇怪。

@ layout / layout_live_top包含一个带有40dip layout_margin的FrameLayout根视图,而layout_live_top有点复杂。

SimpleSlideView是一个没有填充和边距的MatchParent视图,与具有黄色背景的子项@ + id / yellow_child_view相同。

快速下拉时会发生奇怪的事情:
enter image description here

黄色区域在拉动开始时左右两边有40dip边距,然后它变成匹配父.SimpleSlideView似乎受到其他视图的边距(40dip)的影响,它应该在拉下时匹配父级.I不知道为什么会这样。这是因为我复杂的XML?

0 个答案:

没有答案
相关问题