如何使画布绘制覆盖布局层次结构?

问题描述 投票:-2回答:1

我有一些按钮的主要活动和一个带有画布的子视图。当我在按钮后面绘制某些内容时,如果我想在这些按钮之上绘制该怎么办?

acitvity_main.xml

<android.support.design.widget.CoordinatorLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  android:layout_width="match_parent"
  android:layout_height="match_parent">

<com.name.www.app.MyImageView
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/image_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

<ImageButton
    android:id="@+id/test_button"
    android:layout_width="56dp"
    android:layout_height="56dp"
    android:background="@drawable/background"
    android:src="@drawable/edit_reference"/>
</android.support.design.widget.CoordinatorLayout>

MyImageView.java

public class MyImageView extends android.support.v7.widget.AppCompatImageView {


    public MyImageView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        int action = event.getAction();
        Pos.x = event.getX();
        Pos.y = event.getY();
        switch (action) {
            case MotionEvent.ACTION_DOWN:
                this.invalidate();
                break;
            case MotionEvent.ACTION_MOVE:
                this.invalidate();
                break;
            default:
                break;
        }

        return true;
    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawCircle(Pos.x,Pos.y,100,mPaint);
    }
 }

图像按钮通常位于ImageView的顶部,但是如何使绘制圆环回到按钮的顶部?

android android-canvas
1个回答
0
投票

这是因为自定义视图已添加到xml中的按钮之前:

尝试一下:

<android.support.design.widget.CoordinatorLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  android:layout_width="match_parent"
  android:layout_height="match_parent">

<ImageButton
    android:id="@+id/test_button"
    android:layout_width="56dp"
    android:layout_height="56dp"
    android:background="@drawable/background"
    android:src="@drawable/edit_reference"/>

<com.name.www.app.MyImageView
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/image_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>


</android.support.design.widget.CoordinatorLayout>
© www.soinside.com 2019 - 2024. All rights reserved.