如何访问视图下的视图

问题描述 投票:0回答:1

我是 android 的初学者,我有一个 frameLayout,它包含顶部的 Draw view 和背面的 ImageView(使用 PhotoView),所以我可以在上面绘图。 所以我想向我的 ImageView 添加一个功能,以便我可以缩放它向左移动它向右移动所以我为此使用了第三方库(PhotoView)但问题是我无法到达图像因为我的 Draw 视图在它上面,如果我隐藏绘图视图,用户绘图将在调整图像大小时隐藏,我不想要那样,我想要它就像 instagram 故事一样,你可以调整你的故事大小而不隐藏你的绘图..

所以我找到了一个解决方案,但我发现它很奇怪或不太好或不专业哈哈, 我在我的绘图视图顶部的框架布局中添加了重复的 ImageView(PhotoView 库)并使其透明,我阅读了 PhotoView 库代码并将我需要的功能附加到背景 ImageView 所以当我缩放前景图像时背景将与它一起缩放等等第四而不隐藏绘图导致其透明...... 当我点击绘图按钮时,前景 ImageView 将隐藏,我可以继续正常绘图,

这是在 MainActivity 中:

override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        b = ActivityMainBinding.inflate(layoutInflater)
        val view = b.root
        setContentView(view)

//imgtest forground transparent ImageView
//img Background ImageView
//b ViewBinding

attach()

b.DrawOrSize.setOnClickListener {
            if (b.imgtest.visibility == GONE) {
                b.imgtest.visibility = VISIBLE
                b.imgtest.text = "DRAW"
            } else {
                b.imgtest.visibility = GONE
                b.imgtest.text = "SIZE"
            }
        }
}
 fun attach() {

//attach imgtest(forground) movement to img(background)

         b.imgtest.attacher.maximumScale = 500f
         b.img.attacher.maximumScale = 500f
         b.imgtest.setOnScaleChangeListener { _, _, _ ->
             b.img.setScale(b.imgtest.scale, false) 

        }
        b.imgtest.setOnTouchListener { view, motionEvent ->
            b.img.attacher.onTouch(view, motionEvent)
        }
    }

我的 xml 框架布局:

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:animateLayoutChanges="true"
    android:orientation="vertical"
    android:layout_gravity="center"
    android:gravity="center"
    tools:context="com.at.test.MainActivity">

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/strokedballe"
>

    <com.github.chrisbanes.photoview.PhotoView 
        //background image
        android:id="@+id/img"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="5dp"
        android:background="@android:color/transparent"
        android:src="@mipmap/untitled"

        />
    <com.at.test.Draw
        android:id="@+id/Draw"
        android:layout_margin="5dp
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@android:color/transparent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

    </com.at.test.Draw>

    <com.github.chrisbanes.photoview.PhotoView
        //Forground image
        android:id="@+id/imgtest"
        android:layout_width="match_parent"
        android:src="@android:color/transparent"
        android:layout_gravity="center"
        android:layout_marginTop="5dp"
        android:layout_height="match_parent" />
</FrameLayout>
.
.
.

我的问题是无论如何要在视图下传递触摸事件,尤其是在绘图案例中,还是我应该坚持使用我的 sol?

作为 android 初学者的另一个问题是我应该始终使用文档还是应该记住代码 idk 我有点困惑我知道 80% 的基础知识(类、函数、OOP...)但是当涉及到 android 时喜欢处理permessions,从存储中导入文件,添加提供程序,从应用程序中导出文件,以及一些我总是忘记如何做的android功能,程序员是使用所有这些东西的文档还是他们将代码保存在注释中,例如我相当困惑并知道我在这个 qts 上取得的进展,我希望我能在这里找到答案,谢谢

android kotlin view drawing pinchzoom
1个回答
0
投票

我找到了解决方案! 我没有使用另一个透明图像视图并使设计更复杂,而是想出了另一种方法,它改变了 Draw 类中的 onTouchEvent 方法

在我的 Draw 类中,函数是:

    override fun onTouchEvent(event: MotionEvent?): Boolean {

    val x=event?.x
    val y =event?.y

    when(event?.action){
        MotionEvent.ACTION_DOWN -> {
           
            drawPaint!!.color=color
            drawPaint!!.strokeWidth=brushsize

            if (y != null && x != null) {

                drawpath!!.moveTo(x,y)
                drawpath!!.lineTo(x,y)
                pathlist.add(drawpath!!)
            }
        }
      
        MotionEvent.ACTION_MOVE ->{
            if (y != null && x != null) {
                    drawpath!!.lineTo(x,y)}
            }

        }
        MotionEvent.ACTION_UP ->{
            if (y != null && x != null) {
                drawpath=CustomPath(color,brushsize)


            }}
        else -> return false
    }
    invalidate()
    return true
}

覆盖函数 OnTouchEvent 是绘制所必需的,并且在活动 xml 中声明绘制视图时会自动调用它 但它阻止我调整大小或触摸背景 imageView 所以解决方案是避免覆盖它并将其创建为 Draw 类中的普通函数我只是删除了覆盖词并将函数重命名为自定义名称(我的情况是 onTouch) 现在该函数不会自动调用,所以我可以在我的主要活动中手动控制它,当我想绘制时我调用它,当我想访问它下面的视图时我取消它(要调整大小的背景图像)

在我的主要活动中:

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    b = ActivityMainBinding.inflate(layoutInflater)
    val view = b.root
    setContentView(view)
    //img Background ImageView
    //b ViewBinding

     setDrawOriSizeButton()

     }
     private fun setDrawOrSizeButton() {

    var canDraw=false //to verify if we are drawing or resizing
    b.DrawOrSize.setOnClickListener {
        if (canDraw==false){
            canDraw=true/*Drawing enabled resize disabled cannot access to the under View*/
            b.Draw.setOnTouchListener { view, motionEvent ->
                b.Draw.onTouch(motionEvent)} //calling the touch methode

        }else {
            b.Draw.setOnTouchListener(null) //canceling touch methode
            canDraw=false//drawing disabled resize enabled accesing to the under view possible
        }


    }
}

}

不要忘记在xml上添加DrawVieww,否则它将无法工作!

        android:clickable="false"
        android:focusableInTouchMode="false"
        android:focusable="false"

重要提示:如果您仅将这些行添加到 xml 布局中的视图,它将无法工作如果 forground View 的 onTouchEvent 方法正在工作,您将无法访问 under View 并且在 Draw 视图的情况下,即使使用这些行,它也会继续绘制:)

© www.soinside.com 2019 - 2024. All rights reserved.