gpt4 book ai didi

android - 如何制作可缩放的 ScrollView ?

转载 作者:可可西里 更新时间:2023-11-01 18:45:16 24 4
gpt4 key购买 nike

在我的 android 应用程序中,我需要创建可缩放的 Activity 。我找到了用于缩放线性布局的有用代码 here .但是在我的应用程序中,有几个 Activity 是从 scrollview 开始的,而这段代码无法识别 scrollview。如何为可滚动 Activity 进行缩放?这是我的布局之一。

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/scrollViewZoom"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" >

<LinearLayout
android:id="@+id/wd_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="vertical" >

<!-- Start Circle -->

<TableRow
android:id="@+id/row_circl1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_marginTop="30dp"
android:background="@color/purple_color" >

<RelativeLayout
android:id="@+id/circle_layout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/white_color" >

<ImageView
android:id="@+id/img_engin_circle1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:background="@drawable/circle_engin_bg"
android:contentDescription="TODO" />
</RelativeLayout>

<RelativeLayout
android:id="@+id/circle_layout2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/white_color" >

<ImageView
android:id="@+id/img_engin_circle2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:background="@drawable/circle_engin_bg"
android:contentDescription="TODO" />
</RelativeLayout>
</TableRow>

<TableRow
android:id="@+id/row_name_circle"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_marginTop="30dp" >

<RelativeLayout
android:id="@+id/circle_name_layout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="-15dp"
android:background="@color/white_color" >

<ImageView
android:id="@+id/img_name_circle1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:background="@drawable/circle_gauge_name"
android:contentDescription="TODO" />
</RelativeLayout>

<RelativeLayout
android:id="@+id/circle_name_layout2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="-15dp"
android:background="@color/white_color" >

<ImageView
android:id="@+id/img_name_circle2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:background="@drawable/circle_gauge_name"
android:contentDescription="TODO" />
</RelativeLayout>
</TableRow>

<!-- End Circle -->

</LinearLayout>

</ScrollView>

任何想法都会对我有所帮助。谢谢。

最佳答案

好的。翻遍网络后,终于找到了我的答案。我发现 ScrollView 的 onTouchEvent() 不起作用,所以我必须使用 dispatchTouchEvent() 而不是 onTouchEvent()。在顶部,您可以看到我的 xml 代码(在我的问题中),这是我的 Activity 代码,当然还有注释。

    // step 1: add some instance
private float mScale = 1f;
private ScaleGestureDetector mScaleDetector;
GestureDetector gestureDetector;

//step 2: create instance from GestureDetector(this step sholude be place into onCreate())
gestureDetector = new GestureDetector(this, new GestureListener());

// animation for scalling
mScaleDetector = new ScaleGestureDetector(this, new ScaleGestureDetector.SimpleOnScaleGestureListener()
{
@Override
public boolean onScale(ScaleGestureDetector detector)
{
float scale = 1 - detector.getScaleFactor();

float prevScale = mScale;
mScale += scale;

if (mScale < 0.1f) // Minimum scale condition:
mScale = 0.1f;

if (mScale > 10f) // Maximum scale condition:
mScale = 10f;
ScaleAnimation scaleAnimation = new ScaleAnimation(1f / prevScale, 1f / mScale, 1f / prevScale, 1f / mScale, detector.getFocusX(), detector.getFocusY());
scaleAnimation.setDuration(0);
scaleAnimation.setFillAfter(true);
ScrollView layout =(ScrollView) findViewById(R.id.scrollViewZoom);
layout.startAnimation(scaleAnimation);
return true;
}
});


// step 3: override dispatchTouchEvent()
@Override
public boolean dispatchTouchEvent(MotionEvent event) {
super.dispatchTouchEvent(event);
mScaleDetector.onTouchEvent(event);
gestureDetector.onTouchEvent(event);
return gestureDetector.onTouchEvent(event);
}

//step 4: add private class GestureListener

private class GestureListener extends GestureDetector.SimpleOnGestureListener {
@Override
public boolean onDown(MotionEvent e) {
return true;
}
// event when double tap occurs
@Override
public boolean onDoubleTap(MotionEvent e) {
// double tap fired.
return true;
}
}

非常感谢。

关于android - 如何制作可缩放的 ScrollView ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19615697/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com