Java Android上的动画

Java Android上的动画,java,android,animation,xhtml,Java,Android,Animation,Xhtml,我一直在环顾四周,寻找答案,在问任何问题之前,我真的试图深入android编程。我知道这个网站是来帮忙的,但也不是一个多余和懒惰的出口。虽然我是安卓的乞丐,如果你们有时间的话,我需要一些帮助。我看了一个我真的认为会有帮助的问题(URL:),但它最终使我的程序崩溃。我也检查了这一个,这也没有真正让我得到正确的答案,我正在寻找。不幸的是,现在我没有代码,但我尝试从我提供的这两个示例站点复制代码。当我在实际java代码中声明动画对象时,它们都崩溃了 i、 e 我的目标基本上就是让一个五彩纸屑动画从我的

我一直在环顾四周,寻找答案,在问任何问题之前,我真的试图深入android编程。我知道这个网站是来帮忙的,但也不是一个多余和懒惰的出口。虽然我是安卓的乞丐,如果你们有时间的话,我需要一些帮助。我看了一个我真的认为会有帮助的问题(URL:),但它最终使我的程序崩溃。我也检查了这一个,这也没有真正让我得到正确的答案,我正在寻找。不幸的是,现在我没有代码,但我尝试从我提供的这两个示例站点复制代码。当我在实际java代码中声明动画对象时,它们都崩溃了 i、 e

我的目标基本上就是让一个五彩纸屑动画从我的屏幕的顶部滚到屏幕的底部。我已经创建了大约8个不同的图像,以创建一个“动画”,但我真的不知道如何在Android本身实现这一点。如果有人能帮我或给我指出正确的方向,我将不胜感激。非常感谢你

试试这个

public class HitActivity extends Activity {

    private ObjectAnimator animation1;
      private ObjectAnimator animation2;
      private Button button;
      private Random randon;
      private int width;
      private int height;
      private AnimatorSet set;

      @Override
      protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.doctor_chemist_list);
        width = getWindowManager().getDefaultDisplay().getWidth();
        height = getWindowManager().getDefaultDisplay().getHeight();
        randon = new Random();

        set = createAnimation();
        set.start();
        set.addListener(new AnimatorListenerAdapter() {

          @Override
          public void onAnimationEnd(Animator animation) {
            int nextX = randon.nextInt(width);
            int nextY = randon.nextInt(height);
            animation1 = ObjectAnimator.ofFloat(button, "x", button.getX(),
                nextX);
            animation1.setDuration(1400);
            animation2 = ObjectAnimator.ofFloat(button, "y", button.getY(),
                nextY);
            animation2.setDuration(1400);
            set.playTogether(animation1, animation2);
            set.start();
          }
        });
      }

      public void onClick(View view) {
        String string = button.getText().toString();
        int hitTarget = Integer.valueOf(string) + 1;
        button.setText(String.valueOf(hitTarget));
      }

      @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    @SuppressLint("NewApi")
    private AnimatorSet createAnimation() {
        int nextX = randon.nextInt(width);
        int nextY = randon.nextInt(height);
        button = (Button) findViewById(R.id.Button01);
        animation1 = ObjectAnimator.ofFloat(button, "x", nextX);
        animation1.setDuration(1400);
        animation2 = ObjectAnimator.ofFloat(button, "y", nextY);
        animation2.setDuration(1400);
        AnimatorSet set = new AnimatorSet();
        set.playTogether(animation1, animation2);
        return set;
      }
    } 

对该视图使用objectAnimator,并在该视图中保持x参数不变,同时将y参数更改为0到600或相同的值。希望它能起作用。或者翻译器动画师会做这个把戏,试着保持x1=0 x2=0和y1=0 y2=600。

package com.shubh;
package com.shubh;

import android.os.Build;
import android.os.Bundle;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Paint;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.animation.AnimationUtils;
import android.view.animation.LayoutAnimationController;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class MainActivity extends Activity {

     @Override
      public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_main);




      }

      @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    @SuppressLint("NewApi")
    public void startAnimation(View view) {
        float dest = 0;
        ImageView aniView = (ImageView) findViewById(R.id.imageView1);
        switch (view.getId()) {

        case R.id.Button01:
          dest = 360;
          if (aniView.getRotation() == 360) {
            System.out.println(aniView.getAlpha());
            dest = 0;
          }
          ObjectAnimator animation1 = ObjectAnimator.ofFloat(aniView,
              "rotation", dest);
          animation1.setDuration(2000);
          animation1.start();
          // Show how to load an animation from XML
          // Animation animation1 = AnimationUtils.loadAnimation(this,
          // R.anim.myanimation);
          // animation1.setAnimationListener(this);
          // animatedView1.startAnimation(animation1);
          break;

        case R.id.Button02:
          // Shows how to define a animation via code
          // Also use an Interpolator (BounceInterpolator)
          Paint paint = new Paint();
          TextView aniTextView = (TextView) findViewById(R.id.textView1);
          float measureTextCenter = paint.measureText(aniTextView.getText()
              .toString());
          dest = 0 - measureTextCenter;
          if (aniTextView.getX() < 0) {
            dest = 0;
          }
          ObjectAnimator animation2 = ObjectAnimator.ofFloat(aniTextView,
              "x", dest);
          animation2.setDuration(2000);
          animation2.start();
          break;

        case R.id.Button03:
          // Demonstrate fading and adding an AnimationListener
            RelativeLayout mainContainer = (RelativeLayout) findViewById(R.id.layout);
            LayoutAnimationController controller = AnimationUtils.loadLayoutAnimation(this, R.anim.main_layout_animation);
            mainContainer.setLayoutAnimation(controller);

          dest = 1;
          Button button3=(Button)findViewById(R.id.Button03);
            button3.startAnimation(AnimationUtils.loadAnimation(MainActivity.this, R.anim.hyperspace_jump));
          if (aniView.getAlpha() > 0) {
            dest = 0;
          }
          ObjectAnimator animation3 = ObjectAnimator.ofFloat(aniView,
              "alpha", dest);
          animation3.setDuration(2000);
          animation3.start();
          break;

        case R.id.Button04:

          ObjectAnimator fadeOut = ObjectAnimator.ofFloat(aniView, "alpha",
              0f);
          fadeOut.setDuration(2000);
          ObjectAnimator mover = ObjectAnimator.ofFloat(aniView,
              "translationX", -500f, 0f);
          mover.setDuration(2000);
          ObjectAnimator fadeIn = ObjectAnimator.ofFloat(aniView, "alpha",
              0f, 1f);
          fadeIn.setDuration(2000);
          AnimatorSet animatorSet = new AnimatorSet();

          animatorSet.play(mover).with(fadeIn).after(fadeOut);
          animatorSet.start();
          break;

        default:
          break;
        }

      }

      @Override
      public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return super.onCreateOptionsMenu(menu);
      }

      @Override
      public boolean onOptionsItemSelected(MenuItem item) {
        Intent intent = new Intent(this, HitActivity.class);
        startActivity(intent);
        return true;
      }
    } 
导入android.os.Build; 导入android.os.Bundle; 导入android.animation.AnimatorSet; 导入android.animation.ObjectAnimator; 导入android.annotation.SuppressLint; 导入android.annotation.TargetApi; 导入android.app.Activity; 导入android.content.Intent; 导入android.graphics.Paint; 导入android.view.Menu; 导入android.view.MenuItem; 导入android.view.view; 导入android.view.animation.AnimationUtils; 导入android.view.animation.LayoutAnimationController; 导入android.widget.Button; 导入android.widget.ImageView; 导入android.widget.LinearLayout; 导入android.widget.RelativeLayout; 导入android.widget.TextView; 公共类MainActivity扩展了活动{ @凌驾 创建时的公共void(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.act_main); } @TargetApi(构建版本代码蜂窝) @SuppressLint(“新API”) 公共void startAnimation(视图){ 浮点数=0; ImageView aniView=(ImageView)findViewById(R.id.imageView1); 开关(view.getId()){ 案例R.id.Button01: dest=360; if(aniView.getRotation()==360){ System.out.println(aniView.getAlpha()); dest=0; } ObjectAnimator animation1=ObjectAnimator.offload(aniView, “轮换”,目的地); 动画1.设定持续时间(2000年); 动画1.start(); //演示如何从XML加载动画 //Animation animation1=AnimationUtils.loadAnimation(此, //R.anim.myanimation); //animation1.setAnimationListener(这个); //动画视图1.开始动画(动画1); 打破 案例R.id.Button02: //演示如何通过代码定义动画 //还可以使用插值器(BounceInterpolator) 油漆=新油漆(); TextView aniTextView=(TextView)findViewById(R.id.textView1); float measureTextCenter=paint.measureText(aniTextView.getText()) .toString()); dest=0—被测ExtCenter; if(aniTextView.getX()<0){ dest=0; } ObjectAnimator animation2=ObjectAnimator.offload(aniTextView, “x”,目的地); 动画2.设定持续时间(2000年); 动画2.start(); 打破 外壳R.id.按钮03: //演示淡入淡出和添加AnimationListener RelativeLayout mainContainer=(RelativeLayout)findViewById(R.id.layout); LayoutImationController=AnimationUtils.LoadLayoutImation(这是R.anim.main\u布局\u动画); mainContainer.SetLayoutImation(控制器); dest=1; Button button3=(Button)findViewById(R.id.Button03); 按钮3.startAnimation(AnimationUtils.loadAnimation(MainActivity.this,R.anim.hyperspace_jump)); 如果(aniView.getAlpha()>0){ dest=0; } ObjectAnimator animation3=ObjectAnimator.offload(aniView, “阿尔法”,目的地); 动画3.设定持续时间(2000年); 动画3.start(); 打破 案例R.id.按钮04: ObjectAnimator淡出=ObjectAnimator.offload(aniView,“alpha”, 0f); 衰减时间(2000年); ObjectAnimator mover=ObjectAnimator.offload(aniView, “平移X”、-500f、0f); 移动时间(2000年); ObjectAnimator fadeIn=ObjectAnimator.offload(aniView,“alpha”, 0f、1f); fadeIn.setDuration(2000年); AnimatorSet AnimatorSet=新的AnimatorSet(); 动画集。播放(移动)。使用(淡出)。之后(淡出); animatorSet.start(); 打破 违约: 打破 } } @凌驾 公共布尔onCreateOptions菜单(菜单){ getMenuInflater().充气(R.menu.main,menu); 返回super.onCreateOptions菜单(菜单); } @凌驾 公共布尔值onOptionsItemSelected(菜单项项){ Intent Intent=新的Intent(这个,HitActivity.class); 星触觉(意向); 返回true; } }
xml是

<?xml version="1.0" encoding="utf-8" ?> 
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"            android:id="@+id/layout" 
    android:layout_width="match_parent" 
   android:layout_height="match_parent"
  android:orientation="vertical">
- <LinearLayout android:id="@+id/test" 
  android:layout_width="wrap_content" 
  android:layout_height="wrap_content">

  <Button android:id="@+id/Button01" 
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:onClick="startAnimation"
  android:text="Rotate" /> 

  <Button android:id="@+id/Button04" 
  android:layout_width="wrap_content" 
  android:layout_height="wrap_content"
  android:onClick="startAnimation" 
  android:text="Group" /> 

  <Button android:id="@+id/Button03"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content" 
   android:onClick="startAnimation" 
   android:text="Fade" /> 

  <Button android:id="@+id/Button02"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content" 
  android:onClick="startAnimation"  
  android:text="Animate" /> 
  </LinearLayout>

  <ImageView android:id="@+id/imageView1" 
   android:layout_width="wrap_content"
   android:layout_height="wrap_content" 
   android:layout_centerHorizontal="true"
   android:layout_centerVertical="true"
   android:src="@drawable/img" /> 

   <TextView android:id="@+id/textView1"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:layout_above="@+id/imageView1"
   android:layout_alignRight="@+id/imageView1"
   android:layout_marginBottom="30dp"
   android:text="Large Text" 
   android:textAppearance="?android:attr/textAppearanceLarge" /> 
  </RelativeLayout>

- 
- 

太棒了,我必须试一试。我假设它基本上只是将动画设置为垂直和水平倾斜滚动?这看起来像我所做的应该工作得很好,如果需要,我可以修改
<?xml version="1.0" encoding="utf-8" ?> 
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"            android:id="@+id/layout" 
    android:layout_width="match_parent" 
   android:layout_height="match_parent"
  android:orientation="vertical">
- <LinearLayout android:id="@+id/test" 
  android:layout_width="wrap_content" 
  android:layout_height="wrap_content">

  <Button android:id="@+id/Button01" 
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:onClick="startAnimation"
  android:text="Rotate" /> 

  <Button android:id="@+id/Button04" 
  android:layout_width="wrap_content" 
  android:layout_height="wrap_content"
  android:onClick="startAnimation" 
  android:text="Group" /> 

  <Button android:id="@+id/Button03"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content" 
   android:onClick="startAnimation" 
   android:text="Fade" /> 

  <Button android:id="@+id/Button02"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content" 
  android:onClick="startAnimation"  
  android:text="Animate" /> 
  </LinearLayout>

  <ImageView android:id="@+id/imageView1" 
   android:layout_width="wrap_content"
   android:layout_height="wrap_content" 
   android:layout_centerHorizontal="true"
   android:layout_centerVertical="true"
   android:src="@drawable/img" /> 

   <TextView android:id="@+id/textView1"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:layout_above="@+id/imageView1"
   android:layout_alignRight="@+id/imageView1"
   android:layout_marginBottom="30dp"
   android:text="Large Text" 
   android:textAppearance="?android:attr/textAppearanceLarge" /> 
  </RelativeLayout>