我正在尝试在ImageView
中使用zoom
。我已经搜索并尝试了很多代码,但问题是我在ScrollView
中有ImageView
,因为我有一些其他对象。
如果尝试,这是最后一个代码。
这是xml代码:
<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>
图像正常,但scroll
不工作。有什么方法可以解决ImageView
在ScrollView
中与其他对象一起使用的问题吗?
这可能是因为覆盖的ImageView正在处理所有触摸事件。它应该只需要两个手指事件,并将其余的事件传递回父对象。当onTouchEvent事件不适用于您的ImageView时,您应该从该事件返回false。
第二个选项是在ScrollView的dispatchTouchEvent方法中处理事件。您可以在ScrollView中处理单指事件,并将其余事件传递给子级。不建议这样做,因为它可能会坏很多。
我使用了一个简单的解决方案:
让我的布局保持原样,当单击ImageView
时,转到一个只有Image
的新布局,在这个问题上使用Mike Ortiz代码。
@Zielony提供的答案为我澄清了一些事情,因为我正在努力解决类似的问题。我在scrollView中有一个可缩放的图像视图,这两个视图都有冲突。所以我创建了一个自定义的scrollView,它只在一根手指触摸屏幕时拦截触摸事件。因此,您将能够用两根手指与imageview缩放功能交互,并用一根手指滚动我们的自定义滚动视图。
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
}
}
}