Android图形动画 使用ScaleAnimation

Android图形动画 使用ScaleAnimation使用ScaleAnimation实现了一个类似于翻转的动画效果。感觉ScaleAnimation算是一个比较好用的动画类了,看了一下API感觉方法和构造方法也都很简单。就不再赘述太多直接上代码吧– 第一步:准备两张照片,放置在res/drawble下。首先在layout中写好布局文件,这里要用framelayout布局,让两张图片一张覆盖在另一张上。相信聪明的你…


Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺

使用ScaleAnimation实现了一个类似于翻转的动画效果。

感觉ScaleAnimation算是一个比较好用的动画类了,看了一下API感觉方法和构造方法也都很简单。

就不再赘述太多直接上代码吧- –

 

第一步:

准备两张照片,放置在res/drawble下。

首先在layout中写好布局文件,这里要用framelayout布局,让两张图片一张覆盖在另一张上。

相信聪明的你看到这里已经秒懂等下的图片处理方式了。

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/root"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.dfanzhuan.MainActivity" >

    <ImageView
        android:id="@+id/ivA"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:src="@drawable/image_a" />

    <ImageView
        android:id="@+id/ivB"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:src="@drawable/image_b" />

</FrameLayout>

 第二部:

MainActivity.java

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.ScaleAnimation;
import android.widget.ImageView;

public class MainActivity extends Activity {

	private ImageView imgA;
	private ImageView imgB;
	
	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 OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if (imgA.getVisibility() == View.VISIBLE) {
					imgA.startAnimation(sato0);
				}else {
					imgB.startAnimation(sato0);
				}
			}
		});
	}
	
	private void showImageA() {
		imgA.setVisibility(View.VISIBLE);
		imgB.setVisibility(View.INVISIBLE);
	}
	
	private void showImageB() {
		imgA.setVisibility(View.INVISIBLE);
		imgB.setVisibility(View.VISIBLE);
	}
	
	private void initView() {
		//指定执行时间
		imgA = (ImageView) findViewById(R.id.ivA);
		imgB = (ImageView) findViewById(R.id.ivB);
		showImageA();
		//动画执行时间
		sato0.setDuration(500);
		sato1.setDuration(500);
		
		sato0.setAnimationListener(new AnimationListener() {
			
			@Override
			public void onAnimationStart(Animation animation) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void onAnimationRepeat(Animation animation) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void onAnimationEnd(Animation animation) {
				if (imgA.getVisibility() == View.VISIBLE) {
					imgA.setAnimation(null);
					showImageB();
					imgB.startAnimation(sato1);
				} else {
					imgB.setAnimation(null);
					showImageA();
					imgA.startAnimation(sato1);
					
				}
			}
		});
	}

}

 个人感觉难点在于对时间和图片翻转情况的理解,不过写几次以后就just soso了~

下面是效果图,因为还不会做git。。。所以发四张好了:

Android图形动画 使用ScaleAnimation
 
Android图形动画 使用ScaleAnimation
 

Android图形动画 使用ScaleAnimation
 
Android图形动画 使用ScaleAnimation
 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/183892.html原文链接:https://javaforall.net

赞 (0)
全栈程序员-站长的头像全栈程序员-站长

相关推荐

  • 《chkconfig命令》-linux命令五分钟系列之四

    《chkconfig命令》-linux命令五分钟系列之四

    2021年9月8日
  • BP神经网络算法改进文献_bp神经网络算法流程图

    BP神经网络算法改进文献_bp神经网络算法流程图周志华机器学习BP改进试设计一个算法,能通过动态调整学习率显著提升收敛速度,编程实现该算法,并选择两个UCI数据集与标准的BP算法进行实验比较。1.方法设计传统的BP算法改进主要有两类:-启发式算法:如附加动量法,自适应算法-数值优化法:如共轭梯度法、牛顿迭代法、Levenberg-Marquardt算法(1)附加动量项这是一种广泛用于加速梯度下降法收敛…

    2022年9月11日
  • Linux下安装mysql-8.0.20

    Linux下安装mysql-8.0.20**Linux下安装mysql-8.0.20**环境介绍操作系统:CentOS7mysql下载地址:https://dev.mysql.com/downloads/mysql/下载版本:mysql-8.0.20-linux-glibc2.12-x86_64.tar.xz卸载mysql查看是否安装过mysql,命令:find/-namemysql如果安装过,进行卸载:删除相关目录:删除配置文件:删除mysql用户和用户组(如果有进程,杀掉在删)卸载完毕!安装mysq

    2022年5月15日
  • 【OpenCV入门教程之十二】OpenCV边缘检测:Canny算子,Sobel算子,Laplace算子,Scharr滤波器合辑

    【OpenCV入门教程之十二】OpenCV边缘检测:Canny算子,Sobel算子,Laplace算子,Scharr滤波器合辑本篇文章中,我们将一起学习OpenCV中边缘检测的各种算子和滤波器——Canny算子,Sobel算子,Laplace算子以及Scharr滤波器。文章中包含了五个浅墨为大家准备的详细注释的博文配套源代码。在介绍四块知识点的时候分别一个,以及最后的综合示例中的一个。依然是是放出一些程序运行截图吧:效果图看完,我们来唠唠嗑。首先,需要说明的是,浅墨这篇文章最后的示例代码是采用两周前刚刚发布的2.4.9来书写的。里面的lib都已经改成了2.4.9版本的。如果大家需要运行的话,要么配置好2.4.9.要么把浅墨

    2022年5月30日
  • jenkinsfile docker_python安装虚拟环境

    jenkinsfile docker_python安装虚拟环境前言之前我们用docker手动安装了jenkins环境,在jenkins中又安装了python3环境和各种安装包,如果我们想要在其他3台机器上安装,又是重复操作,重复劳动,那会显得很low,这里可以

    2022年7月28日
  • 配置Trunk接口

    配置Trunk接口1、Trunk接口1.1什么是TrunkTrunk接口属于干道,主干链路,通常用于交换机和交换机之间,通过一个接口传输多个vlan的数据包。当Trunk端口收到数据帧时,如果该帧不包含802.1

    2022年7月1日

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号