gpt4 book ai didi

android - 我想要 2 个按钮和 2 个独立的动画

转载 作者:行者123 更新时间:2023-11-29 00:29:21 26 4
gpt4 key购买 nike

我想要带有 2 个不同 XML 动画文件的按钮。

我首先尝试了这段代码:

package com.example.animateabutton;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;

public class MainActivity extends Activity implements View.OnClickListener
{

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

Button mybutton = (Button)findViewById(R.id.button1);
mybutton.setOnClickListener(this);

Button mybutton2 = (Button)findViewById(R.id.button2);
mybutton2.setOnClickListener(this);



}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

@Override
public void onClick(View v) {

// TODO Auto-generated method stub

Animation shake = AnimationUtils.loadAnimation(this, R.anim.rshake);
findViewById(R.id.button1).startAnimation(shake);


Animation vanish = AnimationUtils.loadAnimation(this ,R.anim.vanish);
findViewById(R.id.button2).startAnimation(vanish);


}

}

我把它改成了这个

package com.example.animateabutton;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;

public class MainActivity extends Activity
{

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

Button mybutton = (Button)findViewById(R.id.button1);
Button mybutton2 = (Button)findViewById(R.id.button2);

mybutton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Animation shake = AnimationUtils.loadAnimation(this, R.anim.rshake);
findViewById(R.id.button1).startAnimation(shake);
}
});

mybutton2.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub

Animation vanish = AnimationUtils.loadAnimation(this ,R.anim.vanish);
findViewById(R.id.button2).startAnimation(vanish);
}
});
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

}

这是vanish.xml动画文件

    <?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">

<scale android:duration="300"
android:fromXScale="1.0"
android:fromYScale="1.0"
android:toXScale="3"
android:toYScale="3"
android:pivotX="40%"
android:pivotY="40%"
android:interpolator="@android:anim/anticipate_overshoot_interpolator"

/>

<alpha android:duration="300"
android:fromAlpha="1.0"
android:toAlpha="0.0"
android:interpolator="@android:anim/bounce_interpolator"

/>

</set>

这是rshake.xml动画文件

      <?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromXDelta="0"
android:toXDelta="20"
android:duration="1000"
android:interpolator="@anim/cycleby" >

</translate>

这是 cycleby.xml

  <?xml version="1.0" encoding="utf-8"?>
<cycleInterpolator xmlns:android="http://schemas.android.com/apk/res/android" android:cycles="10" />

这是布局文件activity_main.xml

       <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="vertical"
android:padding="4dip"
tools:context=".MainActivity" >

<Button
android:id="@+id/button1"
style="@string/shakepress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="20dip"
android:hint="@string/shakepress"
android:text="@string/shakepress" />

<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="@string/vanishpress"
android:scrollHorizontally="true"
android:text="@string/vanishpress" />

</LinearLayout>

我希望动画为每个按钮单独工作。

最佳答案

这样做:

public class MainActivity extends Activity {

Button mybutton,mybutton2;

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

mybutton = (Button)findViewById(R.id.button1);
mybutton2 = (Button)findViewById(R.id.button2);

mybutton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {

// the Animation files were kept inside animator folder of res.
// Hence R.animator.shake and R.animator.vanish. in your case it could be R.anim.rshake and R.anim.vanish

Animation shake = AnimationUtils.loadAnimation(MainActivity.this, R.animator.shake);
mybutton.startAnimation(shake);
}
});

mybutton2.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub

Animation vanish = AnimationUtils.loadAnimation(MainActivity.this ,R.animator.vanish);
mybutton2.startAnimation(vanish);
}
});
}
}

Correct this in your layout file.

<!-- Check out the Style attribute of button1. it must be @style--> 

<Button android:id="@+id/button1"
style="@style/shakepress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="20dip"
android:hint="@string/shakepress"
android:text="@string/shakepress" />

关于android - 我想要 2 个按钮和 2 个独立的动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16831804/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com