如何为图像按钮设置动画?

问题描述

我正在尝试为我的项目创建一个动画图像按钮,但由于某种原因图像没有动画但按钮有效。它在我第一次尝试时有效,但在我对其进行了一些更改后停止了。请发送帮助,因为我不知道我的代码出了什么问题。我使用的方法是可绘制动画。

主要活动

package com.example.themuse;

import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity
{
AnimationDrawable loginAnimation;

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ImageButton imageButton = (ImageButton) findViewById(R.id.imageButton);
    imageButton.setBackgroundResource(R.drawable.animationlogin);
    loginAnimation = (AnimationDrawable) imageButton.getBackground();

    /*
    requestwindowFeature(Window.FEATURE_NO_TITLE);
    this.getwindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
    getSupportActionBar().hide();
    setContentView(R.layout.activity_main);
    */
}
@Override
public void onWindowFocusChanged(boolean hasFocus)
{
    super.onWindowFocusChanged(hasFocus);
    loginAnimation.start();
}
public void handleSelection(View myView)
{
    String resourceId = getResources().getResourceEntryName(myView.getId());
    Log.d("temasek","the index of the array is:");
    sendDataToActivity();
}
public void sendDataToActivity()
{
    Intent intent = new Intent(this,Login.class);
    startActivity(intent);
}
}

主活动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"
android:background="#000000"
tools:context=".MainActivity">

<ImageButton
    android:id="@+id/imageButton"
    android:layout_width="259dp"
    android:layout_height="259dp"
    android:layout_marginStart="64dp"
    android:layout_marginTop="260dp"
    android:background="@null"
    android:onClick="handleSelection"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:srcCompat="@drawable/group_28" />

</androidx.constraintlayout.widget.ConstraintLayout>

动画xml

<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="true">>
<item android:drawable="@drawable/group_28" android:duration="1000"/>
<item android:drawable="@drawable/group_1" android:duration="1000"/>
<item android:drawable="@drawable/group_2" android:duration="1500"/>
</animation-list>

解决方法

我发布了一个答案来为按钮上方的图像设置动画。根据经验,这就是最好的方法。您将 ImageView 放在 Button 内的 RelativeLayout 上。使用 elevation,您可以设置不同的高度级别,让图像位于按钮上方。 然后我定义了 2 个名为 runBtnAnimation()stopBtnAnimation() 的函数来处理图像的动画。在避免重复方面,请查看我的related post