Android 动画(一)

来源:互联网 发布:淘宝卖家注册账号 编辑:程序博客网 时间:2024/04/29 11:00

以下是以读取动画资源xml文件的形式,使用动画。

1》在res/anim文件夹下新建一个动画文件popin.xml

<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android"    android:interpolator="@android:anim/accelerate_interpolator">    <scale        android:duration="2000"        android:fromXScale="0.0"        android:fromYScale="0.0"        android:pivotX="50%"        android:pivotY="50%"        android:toXScale="1.0"        android:toYScale="1.0" /></set>


2》Activity的布局文件 activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"    android:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    tools:context=".MainActivity">    <Button        android:id="@+id/start_anim_bt"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="@string/start_anim_bt_txt"/>    <TextView        android:id="@+id/info_tv"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="@string/hello_world" />    <ImageView        android:id="@+id/img_iv"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:src="@drawable/image2"/></LinearLayout>

效果如下:



3》MainActivity.java代码如下(内有详细注示):

package com.demo.cxc.animationdemo;import android.media.Image;import android.support.v7.app.ActionBarActivity;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.view.animation.Animation;import android.view.animation.AnimationUtils;import android.widget.Button;import android.widget.ImageView;import android.widget.TextView;import android.widget.Toast;import org.w3c.dom.Text;public class MainActivity extends AppCompatActivity {    private Button start_anim_bt;    private ImageView im;    private TextView info_tv;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        initViews();    }    private void initViews(){        start_anim_bt=(Button) findViewById(R.id.start_anim_bt);        im=(ImageView)findViewById(R.id.img_iv);        info_tv=(TextView) findViewById(R.id.info_tv);        start_anim_bt.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                showAnim(im);                showAnim(info_tv);            }        });    }    private void showAnim(View v){        //得到在res/anim/popin.xml文件中定义的动画资源        Animation myAnimation= AnimationUtils.loadAnimation(MainActivity.this, R.anim.popin);        //设置重复模式与重复计数        myAnimation.setRepeatMode(Animation.RESTART);        myAnimation.setRepeatCount(Animation.INFINITE);        //设置动画监听器        myAnimation.setAnimationListener(myAnimaitonListener);        //调用startAnimation方法,可以将动画应用到任意的View中        v.startAnimation(myAnimation);    }    //定义动画监听器    Animation.AnimationListener myAnimaitonListener=new Animation.AnimationListener() {        @Override        public void onAnimationStart(Animation animation) {            //// TODO: 15/6/7 在动画开始时执行处理            Toast.makeText(MainActivity.this,"动画开始了。。。",Toast.LENGTH_SHORT).show();        }        @Override        public void onAnimationEnd(Animation animation) {            //// TODO: 15/6/7  在动画结束时执行处理             Toast.makeText(MainActivity.this,"动画结束了。。。",Toast.LENGTH_SHORT).show();        }        @Override        public void onAnimationRepeat(Animation animation) {            //// TODO: 15/6/7  在动画重复时执行处理            Toast.makeText(MainActivity.this,"动画重复执行了。。。",Toast.LENGTH_SHORT).show();        }    };}


本程序的目录结构如下:



0 0
原创粉丝点击