Animation(二)

一、利用res文件夹下的anim文件下的xml创建:

Activity代码:

public class Animations2Activity extends Activity {

    private Button btn1=null;

    private ImageView img=null;

    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

        

        img=(ImageView)findViewById(R.id.img);

        btn1=(Button)findViewById(R.id.btn);

        btn1.setOnClickListener(new Btn());

    }

    class Btn implements OnClickListener{

 

       public void onClick(View v) {

           // TODO Auto-generated method stub

           //使用AnimationUtils.loadAnimation来装载anim中定义的xml形式的动画

           Animation animation=AnimationUtils.loadAnimation(Animations2Activity.this, R.anim.rotate);

           //设置动画速率

           animation.setInterpolator(new AccelerateDecelerateInterpolator());

           img.startAnimation(animation);

       }

    

    }

}

Anim文件夹下的anim.xml文件代码:

<?xmlversion="1.0"encoding="utf-8"?>

<!-- android:interpolator设置动画速率 -->

<setxmlns:android="http://schemas.android.com/apk/res/android"

android:interpolator="@android:anim/decelerate_interpolator" 

<rotate

android:fromDegrees="0"

android:toDegrees="+350"

android:pivotX="50%p"

android:pivotY="50%p"

android:duration="3000"

</rotate>

</set>

posted @ 2011-08-29 23:13  rorshach  阅读(148)  评论(0编辑  收藏  举报