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.
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 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 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
. With elevation
you set different elevation level to let the Image lay above the Button. I then defined 2 function called runBtnAnimation()
and stopBtnAnimation()
to handle the animation of the image. In terms of avoiding duplication please take a look at my related post .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.