安卓上的动画

3

我一直在寻找答案,并试图深入了解Android编程,然后再提问。我知道这个网站可以提供帮助,但也不能让人们变得冗余和懒惰。尽管我是一个初学者,需要一些帮助,如果你们有时间的话。我看了一个问题,认为它会有所帮助(链接:Button animations in android),但最终只导致我的程序崩溃。我还查看了这个链接http://mobile.tutsplus.com/tutorials/android/android-sdk-creating-a-simple-property-animation/,但也没有得到我想要的正确答案。现在我没有代码了,但已经尝试复制我提供的这两个示例网站的代码。当我到达实际Java代码中的动画对象声明时,它们都会崩溃。

    ImageView confettiStart = (ImageView) findViewById(R.id.confetti);
    AnimatorSet confettiSet =(AnimatorSet)AnimatorInflater.loadAnimator(this,R.animator.startconfettianimations);

我的目标基本上只是想让一个五彩缤纷的动画从屏幕顶部滚到底部。我已经创建了大约8个不同的图像,以创建一种“动画”,但我真的不知道如何在Android中实现这一点。如果有人能帮助或指导我方向,我将非常感激。非常感谢。

3个回答

2
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;
      }
    } 

而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>

2

使用objectAnimator来操作该视图,在此过程中,保持x参数不变,同时将y参数更改为0到600或类似的值。希望它有效。或者使用translator animator也可以实现这个效果,尝试一下,保持x1=0、x2=0和y1=0、y2=600。


1

试试这个

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;
      }
    } 

太棒了,我得试试看。我猜它基本上只是将动画设置为以垂直和水平倾斜滚动?对于我的需求来说,这看起来应该工作得很好,如果需要的话我可以稍微修改一下。但是我只是好奇(就像我说的,是初学者:- /) xml部分是什么样子的?我是否使用多张图片,就好像我实际上正在制作动画一样?还是只是将一张图片从上到下移动?非常感谢您的帮助! - ThorinOakenshield

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接