您的位置:首页 > 移动开发 > Android开发

Android 动画简单实现

2017-05-03 22:47 232 查看
Andriod 动画
 

安卓动画主要有三种:帧动画、补间动画以及属性动画。

 

 

一、帧动画

         是将做好的图片通过一定的排列顺序进行一张一张的进行快速播放,就好像以前放电影时一张张的图片快速播放以达到运动的效果。

开发步骤:

 

1、  在drawable文件夹下创建标签是animtion_list文件。

2、  把准备好的图片放进项目res/drawable文件夹下。

3、  在ImageView种加载animtion_list里的图片。

4、  在Activity中开始动画。

 

XML:

 

<?xml version="1.0"
encoding="utf-8"?>

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

    android:orientation="vertical"
android:layout_width="match_parent"

    android:layout_height="match_parent">

    <ImageView

        android:id="@+id/img"

        android:layout_width="300dp"

        android:layout_height="200dp"

        android:background="@drawable/animtion_list"

        />

</LinearLayout>
 

 

<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/g1"
        android:duration="100">
    </item>
    <item android:drawable="@drawable/g2"
        android:duration="100">
    </item>
    <item android:drawable="@drawable/g3"
        android:duration="100">
    </item>
    <item android:drawable="@drawable/g4"
        android:duration="100">
    </item>
    <item android:drawable="@drawable/g5"
        android:duration="100">
    </item>
    <item android:drawable="@drawable/g6"
        android:duration="100">
    </item>
    <item android:drawable="@drawable/g7"
        android:duration="100">
    </item>
    <item android:drawable="@drawable/g8"
        android:duration="100">
    </item>
    <item android:drawable="@drawable/g9"
        android
b35e
:duration="100">
    </item>
    <item android:drawable="@drawable/g10"
        android:duration="100">
    </item>
    <item android:drawable="@drawable/g11"
        android:duration="100">
    </item>

</animation-list>

 

 

Activity:

 

public class Frame_Activity extends Activity{
    ImageView img;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.frame_activity);
        img = (ImageView) findViewById(R.id.img);
        AnimationDrawable ad = (AnimationDrawable) img.getBackground();
         ad.start();
    }

 

 

二、补间动画

 

补间动画主要有四个重要的属性:透明、旋转、平移、缩放。

 

所有动画都具有的一些属性:

 

setDuration(2000):动画动作的时间。

setFillAfter(true/false):动画动作完成后是否保持当前状态。

setRepeatCount(1):动画重复动作的次数

setRepeatMode(Animation.REVERSE(反向)/Animation.RESTART(重新开始)):动画重复动作的模式

 

 

 

透明(代码实现):

代码实现:在XML中监听控件,然后在方法中实现透明的相应代码。

 

XML

 

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:onClick="alpha"
    android:text="透明"/>

 

Activity实现透明:

 

/**
 * 透明
 * */
public void alpha(View view){
    AlphaAnimation alphaAnimation = new AlphaAnimation(1.0f,0.0f);
    alphaAnimation.setDuration(5000);
    alphaAnimation.setFillAfter(true);
    img_tween.startAnimation(alphaAnimation);
}

 

 

旋转(代码实现):

 

XML:

 

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:onClick="rotate"
    android:text="旋转"/>

 

Activity:

 

/**
 *旋转
 * */
public void rotate(View view){
    RotateAnimation ra = new RotateAnimation(0.0f,-180.0f,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
    ra.setDuration(2000);
    ra.setFillAfter(true);
    img_tween.startAnimation(ra);
}

 

 

平移(通过XML实现):

 

XML实现步骤:

 

1、  res文件夹下面创建一个anim文件夹

2、  在anim文件夹下创建对应的动画标签文件

3、  在activity中通过AnimationUtils.loadAnimation()

4、  控件调startAnimation()

 

XML中填写属性:

 

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

</scale>

 

 

5、  Activity中通过AnimationUtils.loadAnimation()调用

 

/**
 * 缩放
 * */
public void scale(View view
){
   Animation sa = AnimationUtils.loadAnimation(getApplicationContext(),R.anim.scale);
    img_tween.startAnimation(sa);
}

 

 

 

平移(方法同上):

 

XML:

 

<?xml version="1.0" encoding="utf-8"?>
<translate android:fromXDelta="0"
    android:toXDelta="-80"
    android:fromYDelta="0"
    android:toYDelta="-80"
    android:duration="2000"
    android:fillAfter="true"
    xmlns:android="http://schemas.android.com/apk/res/android">

</translate>

 

 

Activity:

 

  /**
   * 平移
   * */
  public void translate(View view){
Animation ta =  AnimationUtils.loadAnimation(getApplicationContext(),R.anim.translate);
  img_tween.startAnimation(ta);
  }

 

 

 

 

 

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: