1

我一直有一个问题,我在下面的代码中进行了简化。当我按下 activity_main.xml 布局中的按钮时,它应该将图像向下移动大约 1% 的屏幕高度。问题是如果我每次按下按钮时都使用相同的班次(ImageFragment.java 的第 45 行),它只会工作一次。再次按下该按钮后它不会执行任何操作。但是,如果我取消注释 ImageFragment.java (m_counter++) 的第 46 行并在每次按下按钮时更改班次,则每次按下按钮时图像都会移动。我错过了什么?谢谢。

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginTop="8dp"
        android:gravity="center_horizontal|top"
        android:text="Shift Images"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toTopOf="@id/imageFragment"
        android:id="@+id/button"/>


    <fragment
        android:id="@+id/imageFragment"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:name="com.e.dualimageanimation.ImageFragment"
        android:layout_marginStart="0dp"
        android:layout_marginTop="0dp"
        android:layout_marginEnd="0dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/button"
        app:layout_constraintBottom_toBottomOf="parent"/>

</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity.java

package com.e.dualimageanimation;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity
{
    private ImageFragment m_fragment;
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        Button button = findViewById(R.id.button);
        m_fragment = (ImageFragment) getSupportFragmentManager().findFragmentById(R.id.imageFragment);
        button.setOnClickListener(new View.OnClickListener()
        {
            public void onClick(View v)
            {
                m_fragment.ShiftImages();
            }
        });
    }
}

image_fragment.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="horizontal"
    tools:context=".ImageFragment"
    android:measureAllChildren="true">

    <ImageView
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:id="@+id/leftImage"
        android:layout_gravity="top|start"
        android:src="@drawable/minidonkey"/>

    <ImageView
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:id="@+id/rightImage"
        android:layout_gravity="top|end"
        android:src="@drawable/minidonkey"/>
</FrameLayout>

ImageFragment.java

package com.e.dualimageanimation;

import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import java.util.ArrayList;
import java.util.List;

import androidx.fragment.app.Fragment;

public class ImageFragment extends Fragment
{
    private ImageView m_images[];
    private int m_counter;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup containter, Bundle savedInstanceState)
    {
        m_counter = 1;
        m_images = new ImageView[2];

        View view = inflater.inflate(R.layout.image_fragment, containter, false);

        m_images[0] = view.findViewById(R.id.leftImage);
        m_images[1] = view.findViewById(R.id.rightImage);

        return view;
    }

    public void ShiftImages()
    {
        List<Animator> animators = new ArrayList<Animator>();
        float shift = 0.01f*m_counter*getResources().getDisplayMetrics().heightPixels;
        //m_counter++;

        for(int i=0; i<2; i++)
        {
            animators.add(new ObjectAnimator().ofFloat(m_images[i], "translationY", shift));
        }

        AnimatorSet animatorSet = new AnimatorSet();
        animatorSet.playTogether(animators);
        animatorSet.start();
    }
}

4

0 回答 0