import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.ImageView;import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;public class testBallSliding extends Activity {/** Called when the activity is first created. */private static final int[] INTERPOLATORS = {//定义动画类型数组
android.R.anim.accelerate_interpolator,android.R.anim.decelerate_interpolator,android.R.anim.accelerate_decelerate_interpolator,android.R.anim.anticipate_interpolator,android.R.anim.overshoot_interpolator,android.R.anim.anticipate_overshoot_interpolator,android.R.anim.bounce_interpolator };
Button  btnDrawBall;//定义一个启动按钮static int width ;static int height  ;//这个变量接受server端传来的数据
static int h =380 ;static Animation animation;@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);        //隐藏标题栏  
        requestWindowFeature(Window.FEATURE_NO_TITLE);  
        //设置成全屏  
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,  
                WindowManager.LayoutParams.FLAG_FULLSCREEN);  setContentView(R.layout.main);btnDrawBall = (Button) this.findViewById(R.id.Button01);//加入DrawSin按钮width = getWindowManager().getDefaultDisplay().getWidth();height = getWindowManager().getDefaultDisplay().getHeight();
LinearLayout layout = (LinearLayout) findViewById(R.id.layout);
 final ImageView iv=new ImageView(this);iv.setImageResource(R.drawable.blueball);// animation = new TranslateAnimation(width/2-20,width/2-20, h ,0);animation.setInterpolator(getApplicationContext(), INTERPOLATORS[2]);animation.setDuration(2000); layout.addView(iv,new LayoutParams(40, 40));animation.setFillAfter(true);//animation.setFillBefore(false);iv.setVisibility(View.INVISIBLE);btnDrawBall.setOnClickListener(new Button.OnClickListener() {//设置按钮监听器并设置按下按钮之后的动作public void onClick(View v)   {  //animation = new TranslateAnimation(width/2-20,width/2-20, h ,0);iv.startAnimation(animation);//运行动画  //h-=30;   }  });
}
}
这个程序是实现一个小球从屏幕底部向上运动的动画,每次按了按钮之后,小球都会执行一遍动画。我想把它做成:每按一次按钮,小球的初始高度都减去30,然后执行动画,但是遇到了麻烦:如果直接在onClick中中操作初始高度,则没有任何变化,如果把animation = new TranslateAnimation(width/2-20,width/2-20, h ,0)在onClick中;则系统报错,有谁知道我这个动画该如何实现吗?谢谢大家了。下面是main.xml代码:<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent" android:layout_height="fill_parent"android:orientation="horizontal">
<LinearLayout android:layout_width="fill_parent"android:layout_height="fill_parent"android:orientation="vertical"android:id="@+id/layout"  ><Button android:id="@+id/Button01" android:text="DrawBall" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button></LinearLayout></RelativeLayout>
小球的图片R.drawable.blueball大家随便放个图片都可以

解决方案 »

  1.   

    创建的animation都没初始化public void onClick(View v)
    {
           animation = new TranslateAnimation(width/2-20,width/2-20, h ,0);
           animation.setInterpolator(getApplicationContext(), INTERPOLATORS[2]);
           animation.setDuration(1000); 
         iv.startAnimation(animation);//运行动画
           h-=30;
    }
      

  2.   

    animation可以用配置文件来实现,很方便
    <set xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/accelerate_interpolator">
    <scale android:fromXScale="1" android:toXScale="1"
    android:fromYScale="0.1" android:toYScale="1.0" android:duration="500"
    android:pivotX="50%" android:pivotY="50%" android:startOffset="100" />
    </set>在网上找个例子,我是在android pro 2 中看的
      

  3.   

    我运行没问题了public class testBallSliding extends Activity {
    /** Called when the activity is first created. */
    private static final int[] INTERPOLATORS = {//定义动画类型数组
    android.R.anim.accelerate_interpolator,
    android.R.anim.decelerate_interpolator,
    android.R.anim.accelerate_decelerate_interpolator,
    android.R.anim.anticipate_interpolator,
    android.R.anim.overshoot_interpolator,
    android.R.anim.anticipate_overshoot_interpolator,
    android.R.anim.bounce_interpolator };Button btnDrawBall;//定义一个启动按钮
    static int width ;
    static int height ;//这个变量接受server端传来的数据
    static int h =380 ;
    static Animation animation;@Override
    public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      //隐藏标题栏  
      requestWindowFeature(Window.FEATURE_NO_TITLE);  
      //设置成全屏  
      getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,  
      WindowManager.LayoutParams.FLAG_FULLSCREEN);  
      setContentView(R.layout.main);
      btnDrawBall = (Button) this.findViewById(R.id.Button01);//加入DrawSin按钮
      width = getWindowManager().getDefaultDisplay().getWidth();
      height = getWindowManager().getDefaultDisplay().getHeight();
      LinearLayout layout = (LinearLayout) findViewById(R.id.layout);
      final ImageView iv=new ImageView(this);
      iv.setImageResource(R.drawable.icon);
      animation = new TranslateAnimation(width/2-20,width/2-20, h ,0);
      animation.setInterpolator(getApplicationContext(), INTERPOLATORS[2]);
      animation.setDuration(1000); 
      layout.addView(iv,new LayoutParams(40, 40));
      animation.setFillAfter(true);
      //animation.setFillBefore(false);
      iv.setVisibility(View.INVISIBLE);
      btnDrawBall.setOnClickListener(new Button.OnClickListener() {//设置按钮监听器并设置按下按钮之后的动作
        public void onClick(View v)
        {
           animation = new TranslateAnimation(width/2-20,width/2-20, h ,0);
           animation.setInterpolator(getApplicationContext(), INTERPOLATORS[2]);
           animation.setDuration(1000); 
         iv.startAnimation(animation);//运行动画
           h-=30;
        }
      });
    }
    }
      

  4.   

    我的意思就是:不管把animation = new TranslateAnimation(width/2-20,width/2-20, h ,0);放在onClick(View v)内还是在外部,都无法实现每按一次按钮,小球都会在比上次高30个像素点的地方开始运动,有谁知道该怎么解决这个问题吗,貌似一旦animation = new TranslateAnimation(width/2-20,width/2-20, h ,0);初始化之后,就无法在通过改变h的值来改变小球的初始高度了
      

  5.   

    谢谢!真的可行!animation = new TranslateAnimation(width/2-20,width/2-20, h ,0);必须先初始化,然后再onClick中再初始化一次,这次的h是变化的