安卓动画 补间动画

来源:互联网 发布:网络于柚子是什么意思 编辑:程序博客网 时间:2024/05/21 07:54
补间动画相比于帧动画更加高级了,因为它可以设置监听了,
1.补间动画无需逐一定义每一帧,只要定义开始、结束的帧,和指定动画持续时间。补间动画有4种(均为Animation抽象类子类):AlphaAnimation(透明度,0~1)ScaleAnimation(大小缩放,X、Y轴缩放,还包括缩放中心pivotX、pivotY)TranslationAnimation(位移,X、Y轴位移)RotateAnimation(旋转,包括缩放中心pivotX、pivotY)
补间动画是安卓固定的动画,不好看,四个,旋转,平移,大小缩放,渐影。开代码
一、首先创建一个xml文件
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:app="http://schemas.android.com/apk/res-auto"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context="com.wj.administrator.mylayout.a2Activity">    <Button        android:id="@+id/button2"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_weight="1"        android:text="平移" />    <Button        android:id="@+id/button3"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_weight="1"        android:text="缩小" />    <Button        android:id="@+id/button4"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_weight="1"        android:text="旋转" />    <Button        android:id="@+id/button5"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_weight="1"        android:text="渐隐" />    <Button        android:id="@+id/button6"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_weight="1"        android:text="特效" /></LinearLayout>
二、在actvity中找到Button然后进行设置动作,然后设置动画,动画在哪里呢,首先简单点的在资源里设置。TweenAnimation这是真正动画,在资源文件夹R-android-resourse-directory这个地方里面新建一个anim,这就是和帧动画不同的,帧动画是放在drawable里面的,里面的标签是set,然后必须放一种动画,这个就是在xml中设置的样式,外面一个set标签,是母标签,而且在set下面有很多动画模式。
<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android"    android:duration="5000"    ><translate    android:fromXDelta="0"    android:toXDelta="300"    /></set>
三、上面的这个文件就是动画,代表给某一个控价设置一个动画,是让控这个的移动,下面
activty中的代码:这个是核心代码,主要需要拿到一个AnimationUitls,调用loadAnimation()方法,拿到一个Animation,关键是这个恶心的是控件动的假象;实实在在的按钮真身还在原处。
 ani= AnimationUtils.loadAnimation(Animation.this,R.anim.tran);//一个是centext,一个是动画的位置,就是那个anim里面的                but5.startAnimation(ani);
//这个是activty中的代码
public class Animation extends AppCompatActivity implements View.OnClickListener {      private Button but5,but6;    private AnimationDrawable anim;    private ImageView imag;    private TextView tv;      private android.view.animation.Animation ani;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_animation);        but5=(Button) findViewById(R.id.button5);        but6=(Button) findViewById(R.id.button6);        imag=(ImageView) findViewById(R.id.imageView123);        tv=(TextView) findViewById(R.id.text7);        but5.setOnClickListener(this);        but6.setOnClickListener(this);//        imag=(ImageView) findViewById(R.id.imageView123);//        imag.setImageResource(R.drawable.animdraw);////         animation= (AnimationDrawable)imag.getDrawable();//        animation.start();    }    @Override    public void onWindowFocusChanged(boolean hasFocus) {        super.onWindowFocusChanged(hasFocus);    }    @Override            public void onClick(View v) {            switch(v.getId())            {                case R.id.button5://                    if(anim==null)//                    {//                        tv.setBackgroundResource(R.drawable.animdraw);//                        anim= (AnimationDrawable)tv.getBackground();//                        anim.start();//                    }else//                        {//                            anim= (AnimationDrawable)tv.getBackground();//                            anim.start();//                        }                ani= AnimationUtils.loadAnimation(Animation.this,R.anim.tran);                but5.startAnimation(ani);                break;            case R.id.button6://                anim= (AnimationDrawable)tv.getBackground();//                anim.stop();                ani= AnimationUtils.loadAnimation(Animation.this,R.anim.tran);                but6.startAnimation(ani);                break;        }    }}
这里只有平移,还有旋转,渐影,放大缩小,还没做,那么就自己设计了

原创粉丝点击