1

I'm trying to use zoom in an ImageView. I've searched and tried so many codes but the problem is that I have ImageView inside an ScrollView because I have some other objects.

This is the last code if tried.

And here is xml code:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="fill_parent"
    tools:context=".ConsumoActivity"
    android:orientation="vertical" >

    <LinearLayout 
        android:id="@+id/linearLayoutLogoFactor_consumo"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal" >

        <ImageView 
            android:id="@+id/imageViewLogoFactor_consumo"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/logo_factorenergia" />

    </LinearLayout>

    <ScrollView
        android:id="@+id/scrollViewConsumo"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:fillViewport="true" >

        <LinearLayout
            android:id="@+id/linearLayoutConsumo2"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_marginLeft="20dp"
            android:layout_marginRight="20dp"
            android:layout_gravity="center_horizontal"
            android:focusable="true"
            android:focusableInTouchMode="true"
            android:orientation="vertical" >

            <TextView 
                android:id="@+id/textViewVerConsumo"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="20dp"
                android:layout_marginRight="20dp"
                android:text="@string/etiqueta_ver_consumo"
                android:textSize="@dimen/textSize"
                android:layout_marginTop="10dp"
                android:textStyle="italic"
                android:textColor="#79b7e3" />

            <LinearLayout 
                android:id="@+id/linearLayoutDireccionConsumo"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="20dp"
                android:layout_marginRight="20dp"
                android:layout_marginTop="10dp"
                android:weightSum="1.0">

                <TextView
                    android:id="@+id/textViewlabelDireccionConsumo"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="0.3"
                    android:text="@string/etiqueta_direccion"
                    android:textSize="@dimen/textSize"
                    android:textStyle="bold" />

                <TextView
                    android:id="@+id/textViewDireccionConsumo"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="0.7"
                    android:textSize="@dimen/textSize" />

            </LinearLayout>

            <com.example.factorenergia.CustomImageView
                android:id="@+id/imageViewConsumo"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:layout_marginTop="15dp"
                android:adjustViewBounds="true" 
                android:scaleType="matrix" />

            <RelativeLayout
                android:id="@+id/linearLayoutImagenInferior_consumo"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_marginTop="20dp" >

                <ImageView
                    android:id="@+id/imageViewImagenInferior_consumo"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentBottom="true"
                    android:layout_centerHorizontal="true"
                    android:adjustViewBounds="true"
                    android:src="@drawable/la_electrica_de_las_empresas" />
            </RelativeLayout>

        </LinearLayout>

    </ScrollView>

</LinearLayout>

Image is ok but scroll is not working. Is there any way for solving that having ImageView inside ScrollView with some other objects?

Piotr Chojnacki
  • 6,837
  • 5
  • 34
  • 65
Lyd
  • 2,106
  • 3
  • 26
  • 33

3 Answers3

1

This is probably because the overriden ImageView is handling all touch events. It should take only two-finger events and pass the rest of them back to the parent. You should return false from onTouchEvent, when the event is not for your ImageView.

The second option is to handle events in dispatchTouchEvent method of the ScrollView. You can handle one-finger events in the ScrollView and pass the rest of events to children. This is not recomended, because it may break a lot.

Zielony
  • 16,239
  • 6
  • 34
  • 39
1

I've used an easy solution:

Let my layout as it was and when ImageView is clicked, go to a new one which only has the Image, using Mike Ortiz code at that question.

Community
  • 1
  • 1
Lyd
  • 2,106
  • 3
  • 26
  • 33
0

The answer provided by @Zielony clarified things for me, as I was struggling with a similar issue. I had a zoomable imageview inside scrollView, and both these views were conflicting. So I created a custom scrollView that intercepts touch events only if one finger is touching the screen. So, you will be able to interact with the imageview zooming feature with two fingers, and scroll our custom scrollView with one finger.

class CustomScrollView @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0) : ScrollView(context, attrs, defStyleAttr) {

    override fun onInterceptTouchEvent(ev: MotionEvent): Boolean {
       return if (ev.pointerCount < 2) {
           super.onInterceptTouchEvent(ev)
       } else {
           false
       }
    }
}
Zhangali Bidaibekov
  • 591
  • 1
  • 7
  • 13