Android动画:同步与顺序
我正在使用两个图像(Android动画:同步与顺序,android,android-animation,android-asynctask,Android,Android Animation,Android Asynctask,我正在使用两个图像(img\u heart\u 1和img\u heart\u 2)。我有两个动画,一个是平移的,另一个是缩放的text\u anim.xml:animi(动画),bounce\u up.xml:bounce\u up\u anim(动画) 顺序:一个接一个的动画 同时:两个图像动画同时进行 独家:两个动画(每个图像一个) 包含:两个图像动画仅使用一个动画(AnimationUtils.loadAnimation(this,R.anim.Same)) 结果是连续和不同的动画,
img\u heart\u 1
和img\u heart\u 2
)。我有两个动画,一个是平移的,另一个是缩放的text\u anim.xml:animi(动画)
,bounce\u up.xml:bounce\u up\u anim(动画)
- 顺序:一个接一个的动画
- 同时:两个图像动画同时进行
- 独家:两个动画(每个图像一个)
- 包含:两个图像动画仅使用一个动画(
)AnimationUtils.loadAnimation(this,R.anim.Same)
secAnim.execute()代码>从firstAnimationAsync
并在恢复时放入onResume
,两个动画将同时运行,仅运行一次
如果我在onResume()
中保留secAnim.execute()
,并将img\u heart\u 2.启动动画(animi)代码>而不是img\u heart\u 2.启动动画(反弹动画)代码>它将同时运行一次
如果我将secAnim.execute()
放在firstAnimationAsync
的onPostExecute()
中,并为两个图像保留startAnimation(animi)
,现在,第一个动画将第一次运行,第二次两个动画都将运行
为什么会这样
另外,如果两个图像同时进行,则第二个图像看起来有点受压(垂直向下压缩)。我还提出了da=null
,(现在评论)它不应该使动画无效吗
我还希望我的平移动画能够一直保持到最后,而不是突然恢复或变得不可见
代码:text\u anim.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false"
>
<translate
android:fromXDelta="0"
android:toXDelta="50"
android:fromYDelta="0"
android:toYDelta="100"
android:duration="3000"
android:fillAfter="false"/>
</set>
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false">
<scale
android:fromXScale="1.0"
android:toXScale="2.0"
android:fromYScale="1.0"
android:toYScale="3.0"
android:pivotX="50%"
android:pivotY="0%"
android:duration="3000"/>
</set>
代码:bounce\u up.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false"
>
<translate
android:fromXDelta="0"
android:toXDelta="50"
android:fromYDelta="0"
android:toYDelta="100"
android:duration="3000"
android:fillAfter="false"/>
</set>
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false">
<scale
android:fromXScale="1.0"
android:toXScale="2.0"
android:fromYScale="1.0"
android:toYScale="3.0"
android:pivotX="50%"
android:pivotY="0%"
android:duration="3000"/>
</set>
代码:anidro.java
package my.trials.anidro;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
public class anidro extends Activity {
ImageView img_heart_1,img_heart_2, img_heart_3;
Animation animi, bounce_up_anim;
Bitmap b1,b2;
firstAnimationAsync da;
secondAnimAsync secAnim;
@Override
public void onPause(){
super.onPause();
}
@Override
public void onResume(){
super.onResume();
InitializeLayouts();
**da.execute();**
}
@Override
public void onCreate(Bundle savedInstanceState) {
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
private void InitializeLayouts() {
img_heart_1=(ImageView)findViewById(R.id.lay_main_heartImg);
img_heart_2 = (ImageView)findViewById(R.id.lay_main_heart2Img);
**bounce_up_anim**=AnimationUtils.loadAnimation(this, R.anim.bounce_up);
**animi** = AnimationUtils.loadAnimation(this, R.anim.text_anim);
b1 = BitmapFactory.decodeResource(getResources(),R.drawable.anidro_heart2);
b2=BitmapFactory.decodeResource(getResources(), R.drawable.anidro_heart3);
da= new firstAnimationAsync();
secAnim = new secondAnimAsync();
//img_heart_12=(ImageView)findViewById(R.id.lay_main_koalaImg);
}
private class firstAnimationAsync extends AsyncTask<Void, Void, Void>{
protected void onPreExecute(){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
img_heart_1.setImageBitmap(b1);
img_heart_1.setVisibility(View.VISIBLE);
img_heart_1.startAnimation(**animi**);
//img_heart_1.setVisibility(View.INVISIBLE);
}
@Override
protected Void doInBackground(Void...params){
try{
Thread.sleep(1800);
}
catch(InterruptedException e){
e.printStackTrace();
}
return null;
}
protected void onPostExecute(Void v){
**secAnim.execute();**
//da=null;
return;
}
}
private class secondAnimAsync extends AsyncTask<Void, Void, Void>{
protected void onPreExecute(){
img_heart_2.setImageBitmap(b2);
//da=null;
img_heart_2.setVisibility(View.VISIBLE);
//img_heart_1.setVisibility(View.INVISIBLE);
img_heart_2.startAnimation(**bounce_up_anim**);
}
protected Void doInBackground(Void...params){
try{
Thread.sleep(5000);
}
catch(InterruptedException e){
e.printStackTrace();
}
return null;
}
protected void onPostExecute(Void v){
//secAnim = null;
return;
}
}
}
包my.trials.anidro;
导入android.app.Activity;
导入android.graphics.Bitmap;
导入android.graphics.BitmapFactory;
导入android.os.AsyncTask;
导入android.os.Bundle;
导入android.view.view;
导入android.view.Window;
导入android.view.animation.animation;
导入android.view.animation.AnimationUtils;
导入android.widget.ImageView;
公共类anidro扩展活动{
图像视图img_heart_1、img_heart_2、img_heart_3;
动画动画,弹跳动画;
位图b1、b2;
第一次同步da;
secondanim异步secAnim;
@凌驾
公共无效暂停(){
super.onPause();
}
@凌驾
恢复时公开作废(){
super.onResume();
InitializeLayouts();
**da.execute()**
}
@凌驾
创建时的公共void(Bundle savedInstanceState){
this.requestWindowFeature(Window.FEATURE\u NO\u TITLE);
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
私有void InitializeLayouts(){
img\u heart\u 1=(图像视图)findViewById(R.id.lay\u main\u heartImg);
img_heart_2=(图像视图)findViewById(R.id.lay_main_heart 2img);
**bounce\u up\u anim**=AnimationUtils.loadAnimation(这是R.anim.bounce\u up);
**animi**=AnimationUtils.loadAnimation(this,R.anim.text\u anim);
b1=BitmapFactory.decodeResource(getResources(),R.drawable.anidro_heart2);
b2=BitmapFactory.decodeResource(getResources(),R.drawable.anidro_heart3);
da=新的firstAnimationAsync();
secAnim=新的secondanimsync();
//img_heart_12=(图像视图)findViewById(R.id.lay_main_koalaImg);
}
私有类firstAnimationAsync扩展异步任务{
受保护的void onPreExecute(){
试一试{
睡眠(1000);
}捕捉(中断异常e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
img_heart_1.设置图像位图(b1);
img_heart_1.设置可见性(View.VISIBLE);
心之意象1.开始想象(**动物**);
//img_heart_1.设置可见性(视图不可见);
}
@凌驾
受保护的Void doInBackground(Void…参数){
试一试{
睡眠(1800);
}
捕捉(中断异常e){
e、 printStackTrace();
}
返回null;
}
受保护的void onPostExecute(void v){
**secAnim.execute()**
//da=null;
返回;
}
}
私有类secondAnimAsync扩展异步任务{
受保护的void onPreExecute(){
img_heart_2.设置图像位图(b2);
//da=null;
img_heart_2.设置可见性(View.VISIBLE);
//img_heart_1.设置可见性(视图不可见);
模拟心脏2.启动动画(**弹跳动画**);
}
受保护的Void doInBackground(Void…参数){
试一试{
睡眠(5000);
}
捕捉(中断异常e){
e、 printStackTrace();
}
返回null;
}
受保护的void onPostExecute(void v){
//secAnim=null;
返回;
}
}
}
您真的不应该将ASyncTask用于图形化的东西。没有人能够准确地预测任务将在系统中何时执行,因此这是一个非常糟糕的主意
你为什么不改用它呢?您只需要创建第一个动画,并添加一个新的动画侦听器,该侦听器将在第一个动画的开始或结束时启动第二个动画
对于空化动画,只需调用
img_heart.clearAnimation();
对于“坚持到底”,我不确定您想要什么,但很可能您希望图像保持在动画结束时的位置,因此,您需要在动画声明中设置此属性:
android:fillAfter="true"
android:fillBefore="false"
android:fillEnabled="true"
只有fillAfter似乎是必需的,所以请先尝试,但如果它不起作用,请添加下两行。您确实不应该将ASyncTask用于图形处理。没有人能够准确预测任务将在何时在s中执行