简体   繁体   中英

Android studio : issue using Zoom & scrollview together

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>

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM