A
A
Alex Ivanov2021-03-09 02:33:14
Java
Alex Ivanov, 2021-03-09 02:33:14

How to move the enlarged image?

I am writing a small simple application for schoolchildren, and I ran into a problem - there is a text on biology, zoology, anatomy. Pictures often accompany texts.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <ImageView
        android:id="@+id/main_bg"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="centerCrop"
        app:srcCompat="@drawable/bg" />
    <ScrollView
        android:id="@+id/scrollView1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:padding="7dp"
        android:scrollbars="none" >

        <LinearLayout
            android:orientation="vertical" android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:weightSum="1">

            <TextView
                android:text="Заголовок"
                android:gravity="center"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/titlezag"
                android:layout_marginBottom="20dp"
                android:layout_marginTop="20dp"
                android:textAlignment="center"
                android:textSize="24sp"
                android:textColor="@color/white"
                />

            <LinearLayout
                android:orientation="vertical"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:id="@+id/buttonlayout">

                <ImageView
                    android:id="@+id/AtlasImg"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                     />
            </LinearLayout>
            
            <TextView
                android:text="описание"
                android:lineSpacingExtra="6sp"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/c_opis"
                android:textColor="@color/white"
                android:padding="10dp"
                android:layout_marginTop="20dp"
                android:layout_marginBottom="20dp"
                android:textSize="16sp" />

        </LinearLayout>
    </ScrollView>
</RelativeLayout>


But it happens that the picture is too large to be displayed on the screen, and in this case it is scaled and hard to read.
To increase, I made the following code

public class finish extends AppCompatActivity {
    private static final String TAG = "Main";
    String[] mTestArray;

    private boolean isImageScaled = false;

    /** Called when the activity is first created. */@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        requestWindowFeature(Window.FEATURE_NO_TITLE);

        setContentView(R.layout.finish);
        Intent mIntent = getIntent();
        String fValue = mIntent.getStringExtra("fVariableName");
        String aValue = mIntent.getStringExtra("aVariableName");

        final String splVar = fValue;
        final String[] splittedItem = fValue.split(":");

        int arrayName_ID= getResources().getIdentifier(splittedItem[2] , "array",this.getPackageName());
      mTestArray = getResources().getStringArray(arrayName_ID);

        final String[] ArrItem = mTestArray[0].split("::");

        ImageView ImgAnatom = (ImageView)findViewById(R.id.AtlasImg);

        <b>ImgAnatom.setOnClickListener(v -> {
            if (!isImageScaled) v.animate().scaleX(2f).scaleY(2f).setDuration(500);
            if (isImageScaled) v.animate().scaleX(1f).scaleY(1f).setDuration(500);
            isImageScaled = !isImageScaled;
});</b>


I tried to change the increase code to this

ImgAnatom.setOnTouchListener(new View.OnTouchListener()
        {

            @Override
            public boolean onTouch(View v, MotionEvent event)
            {
                if (!isImageScaled) v.animate().scaleX(2f).scaleY(2f).setDuration(500);
                if (isImageScaled) v.animate().scaleX(1f).scaleY(1f).setDuration(500);
                isImageScaled = !isImageScaled;
                return false;
            }
        });

It's probably more correct...

The picture is enlarged, but you can't see what's outside the screen. How can I enable image movement in the ImgView container?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Vodakov, 2021-03-09
@WaterSmith

Well, look, you have in the properties of the picture
android:scaleType="centerCrop"
AND the dimensions "match_parent" - in this situation, there is nothing to move, everything has already been cut off and there is nothing outside the screen.
For example, try placing an image in a ScrollView with both horizontal and vertical scrolling enabled, and setting the dimensions "wrap_content" on the ImageView, and on the parent ScrollView "match_parent".

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question