Protossan
@Protossan
студент

Как перемещать увеличенную картинку?

Пишу небольшое простенькое приложение для школьников, и столкнулся с проблемой - есть текст по биологии, зоологии, анатомии. К текстам часто идут картинки

<?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>


Но бывает что картинка слишком большая для вывода на экран и в таком случае она масштабируется и плохо читается.
Для увеличения я сделал вот такой код

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>


Попробовал сменить код увеличения на такой

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;
            }
        });

Вероятно он более правильный...

Картинка увеличивается, но не видно того что за пределами екрана. Как можно подключить передвижение картинки в контейнере ImgView?
  • Вопрос задан
  • 167 просмотров
Пригласить эксперта
Ответы на вопрос 1
@WaterSmith
Android-разработчик. Java, Kotlin
Ну, смотрите, у вас в свойствах картинки
android:scaleType="centerCrop"
И размеры "match_parent" - при таком раскладе, там нечего перемещать, всё уже обрезалось и за пределами экрана ничего нет.
Попробуйте например поместить картинку в ScrollView с включенным горизонтальным и вертикальным скроллом, и установить размеры "wrap_content" у ImageView, а у родительского ScrollView "match_parent".
Ответ написан
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы