简体   繁体   English

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

[英]How do i animate an image button?

i am trying to create an animated image button for my project but for some reason the image does not animate but the button works.我正在尝试为我的项目创建一个动画图像按钮,但由于某种原因图像没有动画但按钮有效。 it worked when i first tried it but stopped after i made some changes to it.它在我第一次尝试时有效,但在我对其进行了一些更改后停止了。 please send help as i do not know what went wrong with my codes.请发送帮助,因为我不知道我的代码出了什么问题。 the method i am using is animation drawable.我使用的方法是动画绘制。

main activity主要活动

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

main activity xml主要活动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>

animation xml动画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>

I posted an answer to animate an Image above a Button.我发布了一个答案来为按钮上方的图像设置动画。 That's what worked best from experience.根据经验,这就是最好的方法。 You put a ImageView over a Button inside a RelativeLayout .您将ImageView放在RelativeLayout内的Button With elevation you set different elevation level to let the Image lay above the Button.使用elevation您可以设置不同的高程级别,让图像位于按钮上方。 I then defined 2 function called runBtnAnimation() and stopBtnAnimation() to handle the animation of the image.然后我定义了两个名为runBtnAnimation()stopBtnAnimation()函数来处理图像的动画。 In terms of avoiding duplication please take a look at my related post .在避免重复方面,请查看我的相关帖子

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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