动画(基础)

经过一段时间,终于将群英传与开发艺术过一遍,现对其内容进行归纳总结,并写相应的代码:

ps:1.drawable.xml 文件名必须为小写

1.Drawable分类:

1)BitmapDrawable //表示一张图片


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

  <bitmap android:src="@drawable/ic_launcher_background"

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

    android:antialias="true" 1.抗锯齿

    android:dither="true" 2.是否支持抖动

    android:filter="true"3.是否支持过滤

    android:gravity="center" 

    android:mipMap="false"4.是否支持纹理映射

    android:tileMode="mirror"  5.平铺模式: disabled clamp repeat mirror  不支持,拓展,水平与垂直平铺,水平与垂直方向的镜面映射

    />

2)ShapeDrawable //具有渐变的图形图片


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

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

        android:shape="rectangle">

        <corners

            android:bottomLeftRadius="10dp"

            android:bottomRightRadius="10dp"

            android:radius="10dp"

            android:topLeftRadius="10dp"

            android:topRightRadius="10dp">

            <!--圆角-->

        </corners>

        <gradient

            android:angle="90"

            android:centerColor="#213"

            android:centerX="0.5"

            android:centerY="0.5"

            android:endColor="#fff"

            android:gradientRadius="41dp"

            android:startColor="#000"

            android:type="sweep"

            android:useLevel="false">

            <!--1.渐变角度

            2.渐变中心点

            3.渐变开始到结束颜色  radial:环形 line:线形  sweep:扫描式  angle当为sweep时,不起效果

            4.渐变半径:只有设置为type=redial有效

            -->

        </gradient>

        <!--纯色-->

        <!--<solid android:color="#fff"></solid>-->

        <stroke

            android:width="1dp"

            android:color="#000"

            android:dashGap="1dp"

            android:dashWidth="1dp">

            <!--描边:1.边宽度

            2.边颜色

            3.虚线宽度

            4.虚线距离

            -->

        </stroke>

        <padding

            android:bottom="10dp"

            android:left="10dp"

            android:right="10dp"

            android:top="10dp"></padding>

        <size

            android:width="200dp"

            android:height="200dp">

            <!--图片固有大小-->

        </size>

    </shape> 

3)layer-list layerDrawable 层叠加


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

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

      <item>

          <shape android:shape="rectangle">

              <solid android:color="#f2f2f2"></solid>

          </shape>

          >

      </item>

      <item

          android:bottom="10dp"

          android:left="10dp"

          android:right="10dp"

          android:top="10dp">

          <shape android:shape="oval">

              <solid android:color="#000"></solid>

          </shape>

          >

      </item>

  </layer-list>

4)sateListDrawable(selector) 更多用于状态改变


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

  <selector xmlns:android="http://schemas.android.com/apk/res/android" android:constantSize="true" android:dither="true" android:variablePadding="true">

      <!--1.大小不会随状态而改变

          2.防止抖动

          3.padding不随着状态的改变不随着状态的改变而改变

       -->

      <item android:drawable="@drawable/dots_2" android:state_pressed="true"></item>

      <item android:drawable="@drawable/dots_2" android:state_focused="true"></item>

  </selector>

5)levelListDrawable


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

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


      <item android:drawable="@drawable/dots_2" android:maxLevel="1"/>

      <item android:maxLevel="1" android:drawable="@drawable/test"></item>

  </level-list>

6)transitionDrawable 淡入淡出效果


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

  两张图片,一张淡入一张淡出

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

      <item android:drawable="@drawable/dots_2"></item>

      <item android:drawable="@drawable/test"></item>

  </transition>

7)内嵌drawable


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

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

      android:inset="10dp">

      <shape android:shape="rectangle">

          <solid android:color="#000"></solid>

      </shape>

  </inset>


8)scaleDrawable 缩小图片


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

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

    android:drawable="@drawable/test"

    android:scaleGravity="center"

    android:scaleHeight="70%"

    android:scaleWidth="70%"

   />

  //scale

  ImageView img2=findViewById(R.id.im_02);

  ScaleDrawable drawable2= (ScaleDrawable) img2.getBackground();

  drawable2.setLevel(1); //设置等级,若默认为0不显示

9)ClipDraWale裁剪图片(感觉可以做个图片处理工具,后续补充项目代码)


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

<clip

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

    android:drawable="@drawable/test"

    android:clipOrientation="vertical"

    android:gravity="center_vertical"

    ></clip>

  //clip

  ImageView clip_img=findViewById(R.id.im_clip);

  ClipDrawable cliDra= (ClipDrawable) clip_img.getBackground();

  cliDra.setLevel(5000);//0-10000  10000不裁剪  5000  裁剪50%

2.animation分类:

1)View动画xml:TranslateAnimation scaleAnimation rotateAnimation AlphaAnimation


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

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

    android:shareInterpolator="false" >

    <!--shareinterpolator :是否指定插值器

       -->

    <translate

        android:fromXDelta="0"

        android:toXDelta="100"

        android:fromYDelta="100"

        android:toYDelta="0"

        ></translate>

    <scale

        android:fromXScale="0.5"

        android:fromYScale="0.5"

        android:toXScale="1"

        android:toYScale="1"

        android:pivotX="100"

        android:pivotY="100"

        ></scale>

    <!-- 1.开始时比例与结束是比例

         2.坐标

    -->

    <alpha

        android:fromAlpha="0.5"

        android:toAlpha="1"

        ></alpha>

    <rotate

        android:pivotY="100"

        android:pivotX="100"

        android:fromDegrees="0"

        android:toDegrees="360"></rotate>

</set>

  final Button bt=findViewById(R.id.bt_anim);

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

    animation.setDuration(10000);

    bt.startAnimation(animation);

    bt.setOnClickListener(new View.OnClickListener() {

      @Override

      public void onClick(View v) {

        bt.startAnimation(animation);

      }

    });

2)帧动画(顺序播放图片)


  Button bt_02=findViewById(R.id.bt_anim2);

  bt_02.setBackgroundResource(R.drawable.animation_list);

  AnimationDrawable animation1= (AnimationDrawable) bt_02.getBackground();

  animation1.start();


  <?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/test" android:duration="500"></item>

      <item android:drawable="@drawable/dots_2" android:duration="500"></item>

  </animation-list>

3)特殊场景(LayoutAnimation) 适用于ListView等groudView


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

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

    android:animation="@anim/animation_item_test"

    android:animationOrder="normal"

    android:delay="500">

    <!--delay 动画延迟时间 即每一个子元素延迟相应的时间

    animationorder: 动画顺序 normal:顺序 reverse:逆向 random:随机-->

</layoutAnimation>

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

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

    android:duration="500"

    android:shareInterpolator="true" >

       -->

    <scale

        android:fromXScale="0"

        android:fromYScale="1"

        android:toXScale="1"

        android:toYScale="1"

        ></scale>

    <alpha

        android:fromAlpha="0"

        android:toAlpha="1"

        ></alpha>

</set>

  List<String> strings = new ArrayList<>();

  strings.add("111111");

  strings.add("222222");

  strings.add("33333");

  ListView listView = findViewById(R.id.list_001);

  listView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, strings));

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

  LayoutAnimationController layoutAnimationController = new LayoutAnimationController(animation);

  layoutAnimationController.setDelay(1f);

  layoutAnimationController.setOrder(LayoutAnimationController.ORDER_RANDOM);

  listView.setLayoutAnimation(layoutAnimationController);

4)特殊场景(activity跳转动画)


enter.xml


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

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

      android:shareInterpolator="false"

      android:duration="5000">

      <!--shareinterpolator :是否指定插值器

         -->


      <scale

          android:fromXScale="0"

          android:fromYScale="0"

          android:toXScale="1"

          android:toYScale="1"

          ></scale>

      <alpha

          android:fromAlpha="0"

          android:toAlpha="1"

          ></alpha>

      <rotate

          android:fromDegrees="0"

          android:toDegrees="360"></rotate>

  </set>

exit.xml


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

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

    android:shareInterpolator="false"

    android:duration="1000">

    <!--shareinterpolator :是否指定插值器

       -->

    <scale

        android:fromXScale="1"

        android:fromYScale="1"

        android:toXScale="0"

        android:toYScale="0"

        ></scale>

    <alpha

        android:fromAlpha="0"

        android:toAlpha="1"

        ></alpha>

</set>

调用:

   Intent intent =new Intent( getApplicationContext(),MainActivity.class);

  startActivity(intent);

  overridePendingTransition(R.anim.animation_enter,R.anim.animation_exit);

  PS:overridPendingTransition(...)只能调用与startActivity()或者finish()后面。

4.属性动画:

1)属性方法调用:


  /**

   * 方法一:封装一个类 使得view有get与set方法

   */

  private void performAnimation() {

    ViewWrapper wrapper = new ViewWrapper(button);

    ObjectAnimator.ofInt(wrapper, "width", 500).setDuration(5000).start();

  }

  /**

   * 方法二:监听方法

   * @param view

   * @param start

   * @param end

   */

  private void performAnimate(final View view, final int start, final int end) {

    ValueAnimator valueAnimator = ValueAnimator.ofInt(1, 100);

    valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {

      private IntEvaluator mEvaluator = new IntEvaluator();

      @Override

      public void onAnimationUpdate(ValueAnimator animation) {

        int currentValue = (int) animation.getAnimatedValue();

        float fraction = animation.getAnimatedFraction();

        view.getLayoutParams().width = mEvaluator.evaluate(fraction, start, end);

        view.requestLayout();

      }

    });

    valueAnimator.setDuration(5000).start();

  }

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 221,548评论 6 515
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 94,497评论 3 399
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 167,990评论 0 360
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 59,618评论 1 296
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 68,618评论 6 397
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 52,246评论 1 308
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,819评论 3 421
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,725评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 46,268评论 1 320
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 38,356评论 3 340
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,488评论 1 352
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 36,181评论 5 350
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,862评论 3 333
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 32,331评论 0 24
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,445评论 1 272
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,897评论 3 376
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 45,500评论 2 359

推荐阅读更多精彩内容