android中有很多动画效果,用起来非常会对APP展示有不错的效果,这里就来简单实现一个图片2D翻转的动画效果,以此来熟悉Android中动画效果的使用。后面会给出源码(注意:本事例使用Android studio 开发的),

1、首先看看主布局:

<FrameLayout 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:id="@+id/root"    tools:context=".MainActivity">    <ImageView        android:id="@+id/iamge1"        android:layout_width="match_parent"        android:layout_height="match_parent"        android:src="@drawable/img1"/>    <ImageView        android:id="@+id/iamge2"        android:layout_width="match_parent"        android:layout_height="match_parent"        android:src="@drawable/img2"/></FrameLayout>

主布局很简单,就是使用FrameLayout作为父容器,在其中加入两个imageview;用这两个imageview作为动画演示。


2、主代码:

package com.example.my.scaleanimation;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import android.view.View;import android.view.animation.Animation;import android.view.animation.ScaleAnimation;import android.widget.ImageView;public class MainActivity extends AppCompatActivity {    private ImageView image1,image2,image3,image4,image5;    private ScaleAnimation sato0 = new ScaleAnimation(1,0,1,1,            Animation.RELATIVE_TO_PARENT,0.5f,Animation.RELATIVE_TO_PARENT,0.5f);    private ScaleAnimation sato1 = new ScaleAnimation(0,1,1,1,            Animation.RELATIVE_TO_PARENT,0.5f,Animation.RELATIVE_TO_PARENT,0.5f);    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        initView();        findViewById(R.id.root).setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                if (image1.getVisibility() == View.VISIBLE){                    image1.startAnimation(sato0);                }else{                    image2.startAnimation(sato0);                }            }        });    }    private void showImage1(){        image1.setVisibility(View.VISIBLE);        image2.setVisibility(View.GONE);    }    private void showImage2(){        image1.setVisibility(View.GONE);        image2.setVisibility(View.VISIBLE);    }    private void initView(){        image1 = (ImageView) findViewById(R.id.iamge1);        image2 = (ImageView) findViewById(R.id.iamge2);        showImage1();        sato0.setDuration(500);        sato1.setDuration(500);        sato0.setAnimationListener(new Animation.AnimationListener() {            @Override            public void onAnimationStart(Animation animation) {            }            @Override            public void onAnimationEnd(Animation animation) {                if (image1.getVisibility() == View.VISIBLE){                    image1.setAnimation(null);                    showImage2();                    image2.startAnimation(sato1);                }else{                    image2.setAnimation(null);                    showImage1();                    image1.startAnimation(sato1);                }            }            @Override            public void onAnimationRepeat(Animation animation) {            }        });    }}

关于ScaleAnimation 官方给出了非常详细的解释:

请点击详解地址查看,作为开发者,要学会查看官方文档,具备一定的英文能力是很关键的。

下面请看看效果,是不是很酷,实现起来也非常简单:



点击下载源码



更多相关文章

  1. android Animation
  2. Android自定义Transition动画
  3. Kotlin在AndroidStudio中的环境搭建以及使用Kotlin开发第一个And
  4. Android(安卓)Animation 大全
  5. Android(安卓)开机图片/文字/动画 修改
  6. Android中shape定义控件的使用
  7. 用Gradle 构建你的android程序-依赖管理篇
  8. Android(安卓)小项目之--SQLite 使用法门 (附源码)
  9. android 实战 AppCompat实现Action Bar

随机推荐

  1. MYSQL 最近知识总结(2)
  2. Netdata---Linux系统性能实时监控平台部
  3. 【exp/imp】将US7ASCII字符集的dmp文件导
  4. 计算机网络技术之OSI和TCP
  5. 盘点 Oracle 11g 中新特性带来的10大性能
  6. CSS选择器
  7. 【MOS】Parameter FILESIZE - Multiple
  8. MySQL备份
  9. 宝塔面板专业版7.5.2破解版一键脚本
  10. Alert Log中“Fatal NI connect error 12