zoukankan      html  css  js  c++  java
  • 动画

    代码编写ui

    public class MainActivity extends Activity { 

     @Override

     protected void onCreate(Bundle savedInstanceState) {

      super.onCreate(savedInstanceState);

      LinearLayout layout = new LinearLayout(this);

      layout.setGravity(Gravity.CENTER);

      TextView textView = new TextView(this);

      textView.setText("hello world");

      textView.setTextColor(Color.BLUE);

      textView.setTextSize(30);

      layout.addView(textView);

      setContentView(layout);

     }

    帧动画

    package com.itheima.frameanimation;

    import android.app.Activity;

    import android.graphics.drawable.AnimationDrawable;

    import android.os.Bundle;

    import android.view.View;

    import android.widget.ImageView;

    public class MainActivity extends Activity {

     private ImageView iv;

     private AnimationDrawable animation;

     @Override

     protected void onCreate(Bundle savedInstanceState) {

      super.onCreate(savedInstanceState);

      setContentView(R.layout.activity_main);

      this.iv = (ImageView) this.findViewById(R.id.iv);

      iv.setBackgroundResource(R.drawable.girl_list);

      animation = (AnimationDrawable) iv.getBackground();

    /** 启动主线程进行动画的播放
    getMainLooper().myQueue().addIdleHandler(new IdleHandler() {
    @Override
        public boolean queueIdle() {
           this.animation.start(); 
            return false;
        }
    });

    */

     }

     public void start(View view) {

      this.animation.start();

     }

     public void stop(View view) {

      this.animation.stop();

     }

    }

    ?

    <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" >

        <ImageView

            android:id="@+id/iv"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:background="@drawable/a1" />

        <Button

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:onClick="start"

            android:text="播放" />

        <Button

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:onClick="stop"

            android:text="停止" />

    </LinearLayout>

    在res目录下新建一个文件夹:drawable,在 drawable 下新建animation-list 的xml文件,图片资源也放置在drawabe 

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

    <animation-list xmlns:android="http://schemas.android.com/apk/res/android"

        android:oneshot="false" >

    <item android:drawable="@drawable/a1" android:duration="100"></item>

    <item android:drawable="@drawable/a2" android:duration="100"></item>

    <item android:drawable="@drawable/a3" android:duration="100"></item>

    <item android:drawable="@drawable/a4" android:duration="100"></item>

    <item android:drawable="@drawable/a5" android:duration="100"></item>

    <item android:drawable="@drawable/a6" android:duration="100"></item>

    <item android:drawable="@drawable/a7" android:duration="100"></item>

    <item android:drawable="@drawable/a8" android:duration="100"></item>

    <item android:drawable="@drawable/a9" android:duration="100"></item>

    <item android:drawable="@drawable/a10" android:duration="100"></item>

    <item android:drawable="@drawable/a11" android:duration="100"></item>

    <item android:drawable="@drawable/a12" android:duration="100"></item>

    <item android:drawable="@drawable/a13" android:duration="100"></item>

    <item android:drawable="@drawable/a14" android:duration="100"></item>

    <item android:drawable="@drawable/a15" android:duration="100"></item>

    <item android:drawable="@drawable/a16" android:duration="100"></item>

    <item android:drawable="@drawable/a17" android:duration="100"></item>

    <item android:drawable="@drawable/a18" android:duration="100"></item>

    <item android:drawable="@drawable/a19" android:duration="100"></item>

    <item android:drawable="@drawable/a21" android:duration="100"></item>

    <item android:drawable="@drawable/a22" android:duration="100"></item>

    <item android:drawable="@drawable/a23" android:duration="100"></item>

    <item android:drawable="@drawable/a24" android:duration="300"></item>

    <item android:drawable="@drawable/a25" android:duration="300"></item>

    <item android:drawable="@drawable/a26" android:duration="300"></item>

    <item android:drawable="@drawable/a27" android:duration="300"></item>

    <item android:drawable="@drawable/a28" android:duration="300"></item>

    <item android:drawable="@drawable/a29" android:duration="300"></item>

    <item android:drawable="@drawable/a31" android:duration="300"></item>

    <item android:drawable="@drawable/a32" android:duration="300"></item>

    <item android:drawable="@drawable/a33" android:duration="100"></item>

    <item android:drawable="@drawable/a34" android:duration="100"></item>

    <item android:drawable="@drawable/a35" android:duration="100"></item>

    <item android:drawable="@drawable/a36" android:duration="100"></item>

    <item android:drawable="@drawable/a37" android:duration="100"></item>

    <item android:drawable="@drawable/a38" android:duration="100"></item>

    <item android:drawable="@drawable/a39" android:duration="100"></item>

    <item android:drawable="@drawable/a40" android:duration="100"></item>

    <item android:drawable="@drawable/a41" android:duration="100"></item>

    <item android:drawable="@drawable/a42" android:duration="100"></item>

    <item android:drawable="@drawable/a43" android:duration="100"></item>

    <item android:drawable="@drawable/a44" android:duration="100"></item>

    <item android:drawable="@drawable/a45" android:duration="100"></item>

    <item android:drawable="@drawable/a46" android:duration="100"></item>

    <item android:drawable="@drawable/a47" android:duration="100"></item>

    <item android:drawable="@drawable/a48" android:duration="100"></item>

    <item android:drawable="@drawable/a49" android:duration="100"></item>

    <item android:drawable="@drawable/a50" android:duration="100"></item>

    <item android:drawable="@drawable/a51" android:duration="100"></item>

    <item android:drawable="@drawable/a52" android:duration="100"></item>

    <item android:drawable="@drawable/a53" android:duration="100"></item>

    <item android:drawable="@drawable/a54" android:duration="100"></item>

    <item android:drawable="@drawable/a55" android:duration="100"></item>

    <item android:drawable="@drawable/a56" android:duration="100"></item>

    <item android:drawable="@drawable/a57" android:duration="100"></item>

    <item android:drawable="@drawable/a58" android:duration="100"></item>

    <item android:drawable="@drawable/a59" android:duration="100"></item>

    </animation-list>

    代码创建常见的tween动画

    package com.itheima.tween;

    import android.app.Activity;

    import android.content.Context;

    import android.os.Bundle;

    import android.util.AttributeSet;

    import android.view.View;

    import android.view.animation.AlphaAnimation;

    import android.view.animation.Animation;

    import android.view.animation.AnimationSet;

    import android.view.animation.RotateAnimation;

    import android.view.animation.ScaleAnimation;

    import android.view.animation.TranslateAnimation;

    import android.widget.ImageView;

    public class MainActivity extends Activity {

     private ImageView iv;

     @Override

     protected void onCreate(Bundle savedInstanceState) {

      super.onCreate(savedInstanceState);

      setContentView(R.layout.activity_main);

      this.iv = (ImageView) this.findViewById(R.id.iv);

     }

     /**

      * 透明度

      */

     public void click1(View view) {

      float fromAlpha = 0.0f;

      float toAlpha = 1.0f;

      /**

       * fromAlpha Starting alpha value for the animation, where 1.0 means

       * fully opaque and 0.0 means fully transparent. toAlpha Ending alpha

       * value for the animation.

       */

      AlphaAnimation animation = new AlphaAnimation(fromAlpha, toAlpha);

      long durationMillis = 1000;

      animation.setDuration(durationMillis);

      animation.setRepeatCount(2);

      animation.setRepeatMode(Animation.REVERSE);

      iv.startAnimation(animation);

     }

     /**

      * 缩放

      */

     public void click2(View view) {

      float fromX = 1.0f;// 水平方向从原来的1.0倍开始缩放

      float toX = 3.0f; // 水平方向缩放到原来的3.0倍

      float fromY = 1.0f; // 垂直方向从原来的1.0倍开始缩放

      float toY = 3.0f;// 水平方向缩放到原来的3.0倍

      int pivotXType = Animation.RELATIVE_TO_SELF;// 水平方向以相对自身

      float pivotXValue = 0.5f; // 以x轴为0.5f为中心

      int pivotYType = Animation.RELATIVE_TO_SELF;// 垂直方向以相对自身

      float pivotYValue = 1.0f;// 以y轴为1.0f为中心

      /**

       * fromX <br>

       * Horizontal scaling factor to apply at the start of the animation <br>

       * toX<br>

       * Horizontal scaling factor to apply at the end of the animation<br>

       * fromY<br>

       * Vertical scaling factor to apply at the start of the animation<br>

       * toY<br>

       * Vertical scaling factor to apply at the end of the animation

       * pivotXType<br>

       * Specifies how pivotXValue should be interpreted. One of

       * Animation.ABSOLUTE, Animation.RELATIVE_TO_SELF, or

       * Animation.RELATIVE_TO_PARENT. <br>

       * pivotXValue<br>

       * The X coordinate of the point about which the object is being scaled,

       * specified as an absolute number where 0 is the left edge. (This point

       * remains fixed while the object changes size.) This value can either

       * be an absolute number if pivotXType is ABSOLUTE, or a percene

       * (where 1.0 is 100%) otherwise. <br>

       * pivotYType<br>

       * Specifies how pivotYValue should be interpreted. One of

       * Animation.ABSOLUTE, Animation.RELATIVE_TO_SELF, or

       * Animation.RELATIVE_TO_PARENT.<br>

       * pivotYValue<br>

       * The Y coordinate of the point about which the object is being scaled,

       * specified as an absolute number where 0 is the top edge. (This point

       * remains fixed while the object changes size.) This value can either

       * be an absolute number if pivotYType is ABSOLUTE, or a percene

       * (where 1.0 is 100%) otherwise.

       */

      ScaleAnimation animation = new ScaleAnimation(fromX, toX, fromY, toY,

        pivotXType, pivotXValue, pivotYType, pivotYValue);

      long durationMillis = 10000;

      animation.setDuration(durationMillis);

      animation.setRepeatCount(2);

      animation.setRepeatMode(Animation.REVERSE);

      iv.startAnimation(animation);

     }

     /**

      * 旋转

      */

     public void click3(View view) {

      float fromDegrees = -45f; // 起始旋转的角度,水平x轴为0度,向上为负度数,向下为正度数;

      float toDegrees = 45f;// 中止旋转的角度

      int pivotXType = Animation.RELATIVE_TO_SELF;

      float pivotXValue = 0.5f;

      int pivotYType = Animation.RELATIVE_TO_SELF;

      float pivotYValue = 0f;

      RotateAnimation animation = new RotateAnimation(fromDegrees, toDegrees,

        pivotXType, pivotXValue, pivotYType, pivotYValue);

      long durationMillis = 100;

      animation.setDuration(durationMillis);

      animation.setRepeatCount(2000);

      animation.setRepeatMode(Animation.REVERSE);

      iv.startAnimation(animation);

     }

     /**

      * 位移

      */

     public void click4(View view) {

      int fromXType = Animation.RELATIVE_TO_SELF;

      float fromXValue = 0f;

      int toXType = Animation.RELATIVE_TO_SELF;

      float toXValue = 1f;

      int fromYType = Animation.RELATIVE_TO_SELF;

      float fromYValue = 1f;

      int toYType = Animation.RELATIVE_TO_SELF;

      float toYValue = 0f;

      TranslateAnimation animation = new TranslateAnimation(fromXType,

        fromXValue, toXType, toXValue, fromYType, fromYValue, toYType,

        toYValue);

      long durationMillis = 1000;

      animation.setDuration(durationMillis);

      animation.setRepeatCount(20);

      animation.setRepeatMode(Animation.REVERSE);

      iv.startAnimation(animation);

     }

     /**

      * 综合

      */

     public void click5(View view) {

      boolean shareInterpolator = false;

      AnimationSet set = new AnimationSet(shareInterpolator);

      AlphaAnimation aa = new AlphaAnimation(0.0f, 1.0f);

      aa.setDuration(500);

      aa.setRepeatCount(10);

      aa.setRepeatMode(Animation.REVERSE);

      ScaleAnimation sa = new ScaleAnimation(1.0f, 3.0f, 1.0f, 3.0f,

        Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,

        1.0f);

      sa.setDuration(500);

      sa.setRepeatCount(10);

      sa.setRepeatMode(Animation.REVERSE);

      RotateAnimation ra = new RotateAnimation(-45f, 45f,

        Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,

        0f);

      ra.setDuration(500);

      ra.setRepeatCount(10);

      ra.setRepeatMode(Animation.REVERSE);

      TranslateAnimation ta = new TranslateAnimation(

        Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, 1f,

        Animation.RELATIVE_TO_SELF, 1f, Animation.RELATIVE_TO_SELF, 0f);

      ta.setDuration(500);

      ta.setRepeatCount(10);

      ta.setRepeatMode(Animation.REVERSE);

      set.addAnimation(aa);

      set.addAnimation(sa);

      set.addAnimation(ra);

      set.addAnimation(ta);

      iv.startAnimation(set);

     }

    }

    <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" >

        <LinearLayout

            android:layout_width="match_parent"

            android:layout_height="wrap_content"

            android:orientation="horizontal" >

            <Button

                android:layout_width="wrap_content"

                android:layout_height="wrap_content"

                android:onClick="click1"

                android:text="透明度" />

            <Button

                android:layout_width="wrap_content"

                android:layout_height="wrap_content"

                android:onClick="click2"

                android:text="缩放" />

            <Button

                android:layout_width="wrap_content"

                android:layout_height="wrap_content"

                android:onClick="click3"

                android:text="旋转" />

            <Button

                android:layout_width="wrap_content"

                android:layout_height="wrap_content"

                android:onClick="click4"

                android:text="位移" />

            <Button

                android:layout_width="wrap_content"

                android:layout_height="wrap_content"

                android:onClick="click5"

                android:text="综合" />

        </LinearLayout>

        <LinearLayout

            android:layout_width="match_parent"

            android:layout_height="match_parent"

            android:gravity="center"

            android:orientation="horizontal" >

            <ImageView

                android:id="@+id/iv"

                android:layout_width="wrap_content"

                android:layout_height="wrap_content"

                android:src="@drawable/ic_launcher" >

            </ImageView>

        </LinearLayout>

    </LinearLayout>

    xml文件定义动画

    public class MainActivity extends Activity { 

     private ImageView iv;

     @Override

     protected void onCreate(Bundle savedInstanceState) {

      super.onCreate(savedInstanceState);

      setContentView(R.layout.activity_main);

      this.iv = (ImageView) this.findViewById(R.id.iv);

     }

     /**

      * 透明度

      */

     public void click1(View view) {

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

      iv.startAnimation(animation );

     }

     /**

      * 缩放

      */

     public void click2(View view) {

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

      iv.startAnimation(animation);

     }

     /**

      * 旋转

      */

     public void click3(View view) {

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

      iv.startAnimation(animation);

     }

     /**

      * 位移

      */

     public void click4(View view) {

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

      iv.startAnimation(animation);

     }

     /**

      * 综合

      */

     public void click5(View view) {

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

      iv.startAnimation(animation);

     }

    }

    res下新建文件夹:anim

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

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

        <translate

            android:duration="2000"

            android:fromXDelta="0"

            android:fromYDelta="0"

            android:repeatCount="3"

            android:repeatMode="reverse"

            android:toXDelta="150"

            android:toYDelta="150" >

        </translate>

        <scale

            android:duration="1000"

            android:fillAfter="true"

            android:fromXScale="1"

            android:fromYScale="1"

            android:pivotX="10%p"

            android:pivotY="0%p"

            android:repeatCount="2"

            android:toXScale="3"

            android:toYScale="3" >

        </scale>

        <alpha

            android:duration="1000"

            android:fillAfter="true"

            android:fromAlpha="1"

            android:repeatCount="3"

            android:repeatMode="reverse"

            android:toAlpha="0" >

        </alpha>

        <rotate

            android:duration="700"

            android:fromDegrees="0"

            android:pivotX="120"

            android:pivotY="120"

            android:repeatCount="1000000"

            android:repeatMode="reverse"

            android:toDegrees="360" >

        </rotate>

    </set>

  • 相关阅读:
    Select与Epoll的区别
    C++ 多态详解及常见面试题
    Linux进程状态详解及状态转换
    C++ 设计模式之单例模式
    DS 图解归并排序
    TCP三次握手,四次挥手详解及常见面试题
    Linux 进程间通信(管道、共享内存、消息队列、信号量)
    # maven
    # select sort 选择排序
    # gitlab与git私钥公钥
  • 原文地址:https://www.cnblogs.com/freenovo/p/4469799.html
Copyright © 2011-2022 走看看