我想知道如何让我的 BUTTON START 我的动画在 ANDROID
I want to know how to make my BUTTON START my animation in ANDROID
我的 drawable 文件夹中有简单的 4 个绘图(称为帧 1、2、3、4)和我的动画 xml(称为 animationf)文件。
我的布局中有一个按钮,我想让它一键启动我的动画。我的按钮是不可见的,所以当有人触摸智能手机屏幕时,它会启动我的动画。
我认为我必须在我的 MainActivity java 文件中使用一些带有 OnClickLestener 的东西。
MainActivity.java:
package e.ferreira.explodestress;
import ...
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
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:id="@+id/ConstraintLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF"
android:maxWidth="50000dp"
android:maxHeight="50000dp"
android:minWidth="1dp"
android:minHeight="1dp"
android:visibility="visible"
tools:context=".MainActivity">
<ImageView
android:id="@+id/imageView"
android:layout_width="0dp"
android:layout_height="0dp"
android:scaleType="fitXY"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/start_button"
android:visibility="invisible"/>
</androidx.constraintlayout.widget.ConstraintLayout>
animationf.xml :
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:drawable="@drawable/frame1"
android:duration="125"/>
<item
android:drawable="@drawable/frame2"
android:duration="125"/>
<item
android:drawable="@drawable/frame3"
android:duration="125"/>
<item
android:drawable="@drawable/frame4"
android:duration="125"/>
</animation-list>
首先,您需要通过将按钮分配给一个变量来获取该按钮:
Button yourButton = findViewById(R.id.buttonId);
然后给按钮添加一个 onClick 监听器:
yourButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Code for displaying your frames
}
});
我的 drawable 文件夹中有简单的 4 个绘图(称为帧 1、2、3、4)和我的动画 xml(称为 animationf)文件。
我的布局中有一个按钮,我想让它一键启动我的动画。我的按钮是不可见的,所以当有人触摸智能手机屏幕时,它会启动我的动画。
我认为我必须在我的 MainActivity java 文件中使用一些带有 OnClickLestener 的东西。
MainActivity.java:
package e.ferreira.explodestress;
import ...
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
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:id="@+id/ConstraintLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF"
android:maxWidth="50000dp"
android:maxHeight="50000dp"
android:minWidth="1dp"
android:minHeight="1dp"
android:visibility="visible"
tools:context=".MainActivity">
<ImageView
android:id="@+id/imageView"
android:layout_width="0dp"
android:layout_height="0dp"
android:scaleType="fitXY"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/start_button"
android:visibility="invisible"/>
</androidx.constraintlayout.widget.ConstraintLayout>
animationf.xml :
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:drawable="@drawable/frame1"
android:duration="125"/>
<item
android:drawable="@drawable/frame2"
android:duration="125"/>
<item
android:drawable="@drawable/frame3"
android:duration="125"/>
<item
android:drawable="@drawable/frame4"
android:duration="125"/>
</animation-list>
首先,您需要通过将按钮分配给一个变量来获取该按钮:
Button yourButton = findViewById(R.id.buttonId);
然后给按钮添加一个 onClick 监听器:
yourButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Code for displaying your frames
}
});