3

我一直在四处寻找答案,并在提出任何问题之前真正尝试深入研究 android 编程。我知道这个网站是来帮忙的,但也不是多余和懒惰的出路。虽然我是 android 的初学者,如果你们中的一些人有时间,我需要一些帮助。我看了一个我真的认为会有所帮助的问题(URL: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 本身中实现它。如果有人可以帮助或指出我正确的方向,我将不胜感激。太感谢了。

4

3 回答 3

2

对该视图使用 objectAnimator 并在其中保持 x 参数相同,同时将 y 参数更改为 0 到 600 或相同的值。希望它有效。或者翻译动画师会尝试保持 x1=0 x2=0 和 y1=0 y2=600。

于 2013-05-18T04:57:39.237 回答
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>
于 2013-05-20T04:33:41.850 回答
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;
      }
    } 
于 2013-05-18T04:52:43.580 回答