英文:
Android studio : issue using Zoom & scrollview together
问题
我使用这段代码来实现屏幕的双指缩放,它运行良好。但是当我在 XML 中添加 ScrollView 时,ScrollView 正常工作,但双指缩放屏幕不再起作用。
实际上,我需要同时使用这两个功能,因为我确实需要 ScrollView。
我的问题是如何同时使用 ScrollView 和双指缩放,因为我真的需要 ScrollView?
感谢帮助
//以下是我的Java代码Regle_du_jeu.java
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
英文:
I use this code to pinch zoom screen then it works fine. But when i add scrollview in XML then scroll view works fine but pinch zoom screen doesn't work any more.
actually i need both features
my question is how to use scrollview and pinch zoom in the same time because i really need scrollview ?
Thanks fo help
//Bellow is my java code Regle_du_jeu.java`
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
package com.yiking.openspirit.oracle_yi_king2;
//zoom is working text field by text field - they are 4 of them
import android.app.Activity;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import android.view.View;
import android.view.MotionEvent;
import android.widget.TextView;
public class Regle_du_jeu extends Activity implements View.OnTouchListener {
TextView text;TextView text1;TextView text2;
TextView text3;TextView text4;
TextView ScrollView;
final static float move = 200;
float ratio = 1.0f;
int bastDst;
float baseratio;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.regle_du_jeu);
text1 = findViewById(R.id.regle_jeu1);
text1.setTextSize(ratio + 15);
text2 = findViewById(R.id.regle_jeu2);
text2.setTextSize(ratio + 15);
text3 = findViewById(R.id.regle_jeu3);
text3.setTextSize(ratio + 15);
text4 = findViewById(R.id.regle_jeu4);
text4.setTextSize(ratio + 15);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if (event.getPointerCount() == 2) {
int action = event.getAction();
int mainaction = action & MotionEvent.ACTION_MASK;
if (mainaction == MotionEvent.ACTION_POINTER_DOWN) {
bastDst = getDistance(event);
baseratio = ratio;
} else {
// if ACTION_POINTER_UP then after finding the distance
// we will increase the text size by 15
float scale = (getDistance(event) - bastDst) / move;
float factor = (float) Math.pow(2, scale);
ratio = Math.min(1024.0f, Math.max(0.1f, baseratio \* factor));
// text.setTextSize(ratio + 15);
text1.setTextSize(ratio + 15);
text2.setTextSize(ratio + 15);
text3.setTextSize(ratio + 15);
text4.setTextSize(ratio + 15);
}
}
return true;
}
// get distance between the touch event
private int getDistance(MotionEvent event) {
int dx = (int) (event.getX(0) - event.getX(1));
int dy = (int) (event.getY(0) - event.getY(1));
return (int) Math.sqrt(dx * dx + dy * dy);
}
@Override
public boolean onTouch(View v, MotionEvent event) {
return false;
}
}
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
below is XML =\> regle_du_jeu.xml
<?xml version="1.0" encoding="utf-8"?\>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_height="wrap_content"
style="@style/monlayout_blanc"
android:layout_width="fill_parent"
android:layout_gravity="left"
android:background="#792B82"
tools:context="com.yiking.openspirit.oracle_yi_king2.Regle_du_jeu"
android:id="@+id/scrollView"
\>
<LinearLayout
style="@style/monlayout_blanc"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:orientation="vertical"
>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
>
<TextView
android:layout_width="wrap_content"
style="@style/titre_bleu"
android:layout_height="20dp"
android:layout_gravity="center_vertical"/>
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/regle_jeu_layout"
>
<TextView
android:text="@string/m_expli2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:justificationMode="inter_word"
style="@style/aff_std_gris_sp"
android:id="@+id/regle_jeu1"
/>
<TextView
android:text="@string/m_expli2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:justificationMode="inter_word"
style="@style/aff_std_gris_sp"
android:id="@+id/regle_jeu2"
/>
<TextView
android:text="@string/m_expli2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:justificationMode="inter_word"
style="@style/aff_std_gris_sp"
android:id="@+id/regle_jeu3"
/>
<TextView
android:text="@string/m_expli2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:justificationMode="inter_word"
style="@style/aff_std_gris_sp"
android:id="@+id/regle_jeu4"
/>
</LinearLayout>
答案1
得分: 1
Your ScrollView
的高度不能是wrap_content
- 整个目的是要有一个比其内容小的窗口,以便内容可以在其后上下滚动。我猜想你想要一个高度为match_parent
(这样可滚动区域填充整个屏幕),但你可以设置任何你想要的大小,只要不是wrap_content
。如果你这样做,它将会在屏幕底部扩展,不会可滚动(因为它的大小与其内容相同)。
我假设你的TextView
在更改其文本大小时会重新布局,但如果不是,你可能需要在它们上调用requestLayout()
。
英文:
Your ScrollView
's height can't be wrap_content
- the whole point is to have a window that's smaller than its content, so the content can scroll up and down behind it. I'm guessing you want a height of match_parent
(so the scrollable area fills the screen) but you can make it any size you want, so long as it's not wrap_content
. If you do that, it'll just expand off the bottom of the screen, and won't be scrollable (because it's the same size as its contents).
I'm assuming your TextView
s handle re-laying out when you change their text size, but if not you might need to call requestLayout()
on them
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论