animation rotate_canvas scale

animation rotate_canvas scaleScaleAnimation、RotateAnimation、ScaleAnimation、TranslateAnimation详解

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE稳定放心使用

animation rotate_canvas scale

点击按钮有相应的效果!

解析在代码注释里!!!

main.xml

<RelativeLayout 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"
    tools:context="${relativePackage}.${activityClass}" >

    <TextView
        android:id="@+id/text"
        android:background="#FFFF00"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world"
    
        />

    <Button
        android:id="@+id/translate"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:text="测试translate效果" />

    <Button
        android:id="@+id/alpha"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_above="@id/translate"
        android:text="测试alpha效果" />

    <Button
        android:id="@+id/rotate"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_above="@id/alpha"
        android:text="测试rotate效果"
         />
    
     <Button
        android:id="@+id/scale"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_above="@id/rotate"
        android:text="测试scale效果"
         />

      

</RelativeLayout>


main.java

package com.fzq.animation;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
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.Button;
import android.widget.TextView;

public class MainActivity extends Activity {
	private Button translate, alpha, rotate, scale;
	private TextView text;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		translate = (Button) findViewById(R.id.translate);
		alpha = (Button) findViewById(R.id.alpha);
		rotate = (Button) findViewById(R.id.rotate);
		scale = (Button) findViewById(R.id.scale);
		text = (TextView) findViewById(R.id.text);

		translate.setOnClickListener(new transLateListener());
		alpha.setOnClickListener(new alphaListener());
		rotate.setOnClickListener(new rotateListener());
		scale.setOnClickListener(new scaleListener());
	}

	class transLateListener implements OnClickListener {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub

			AnimationSet animationSet = new AnimationSet(true);
			/*
			 * tanslateAnimation的2种参数方法
			 */

			/*
			 * 第一种 ----> TranslateAnimation(fromXDelta, toXDelta, fromYDelta, toYDelta)
			 * 从坐标(fromXDelta,fromYDelta)移动到(toXDelta,toYDelta),这些坐标都表示增量坐标
			 * 
			 * 解释如下:
			 * 设当前A坐标为(50,50) TranslateAnimation(10,15,20,25),
			 * 那么A点是从起始点(50+10,50+20)【注意!并不是从(50,50)】移动到当前点(50+15,50+25)
			 * 
			 * 效果:
			 * A(50, 50)跳跃到相对于该位置(10,20)的点B【B的坐标为(50+10,50+20)】,在移动到相对于A点(15,25 )的点C【C的坐标为(50+15,50+25)】
			 * 
			 * TranslateAnimation translateAnimation=new
			 * TranslateAnimation(10,15,20,25);
			 */

			/*
			 * 第二种 ---> TranslateAnimation(fromXType, fromXValue, toXType,toXValue, fromYType, fromYValue, toYType, toYValue)移动的坐标还是增量坐标
			 * Type有三种,Value的值一起说明: Animation.ABSOLUTE,
			 * 为绝对位置。value值为50,既为相对于当前位置增量为50的坐标点
			 * Animation.RELATIVE_TO_SELF(相对自己),Animation.RELATIVE_TO_PARENT(相对父控件)。value的值都为百分比(0-1.0) 
			 * 
			 * 解释如下:
			 * new TranslateAnimation( Animation.RELATIVE_TO_SELF, 0.5f,
			 *  Animation.ABSOLUTE, 10, Animation.RELATIVE_TO_SELF, 0.5f,
			 * Animation.ABSOLUTE, 10); 
			 * 设View的宽为100,高为50     就是从(100*0.5,50*0.5)的增量点移动到(10,10)增量点
			 * 
			 * 效果:
			 *假设当前坐标为(50,50),那么A(50,50)跳跃到相对于该位置
			 * (100*0.5,50*0.5)的点B【B的坐标为(50+100*0.5,50+50*0.5)】,
			 * 在移动到相对于A点(10,10)的点C【C的坐标为(50+10,50+10)】
			 */

			TranslateAnimation translateAnimation = new TranslateAnimation(
					Animation.RELATIVE_TO_SELF, 1f, Animation.ABSOLUTE, 400,
					Animation.RELATIVE_TO_SELF, 1f, Animation.ABSOLUTE, 400);

			translateAnimation.setDuration(2000);
			animationSet.addAnimation(translateAnimation);
			text.setAnimation(animationSet);
		}

	}

	class alphaListener implements OnClickListener {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub

			// 创建一个Animatioset对象
			AnimationSet animationSet = new AnimationSet(true);
			// 创建AlphaAnimation对象
			
			/*
			 * 
			  AlphaAnimation(float fromAlpha, float toAlpha)
			  第一个参数fromAlpha为 动画开始时候透明度
			  第二个参数toAlpha为 动画结束时候透明度
			 
			 */
			AlphaAnimation alphaAnimation = new AlphaAnimation(1, 0);
			
			
			// 设置动画执行所需要的时间
			alphaAnimation.setDuration(1000);
			// 将alphaAnimation对象添加到AnimationSet当中
			animationSet.addAnimation(alphaAnimation);
			// 使用控件执行动画效果
			text.setAnimation(animationSet);

		}

	}

	class rotateListener implements OnClickListener {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			AnimationSet animationSet = new AnimationSet(true);
			/*
			 * 
			 * fromDegrees    起始时物件的角度  
			 * toDegrees        结束时物件的角度 可以大于360度
			 * 【度数为负数—表示逆时针旋转】
			 * 
			 * pivotX,        动画相对于物件的X坐标的开始位置
			 * pivotY       动画相对于物件的X坐标的开始位置
			 * pivotX pivotY都从0-100%中取值】
			 * 
			    pivotXType:X轴的伸缩模式,可以取值为ABSOLUTE、RELATIVE_TO_SELF、RELATIVE_TO_PARENT。
				pivotXValue:X坐标的伸缩值。
				pivotYType:Y轴的伸缩模式,可以取值为ABSOLUTE、RELATIVE_TO_SELF、RELATIVE_TO_PARENT。
                pivotYValue:Y坐标的伸缩值
			 * 
			 *  new RotateAnimation(0, 360,Animation.RELATIVE_TO_PARENT, 0f,Animation.RELATIVE_TO_PARENT, 0.5f);
			 *  解释如下:
			 *  条件:设图片大小为(100,80),图片左上角的坐标为(0,0);
			 *  效果:以(图片的父控件的宽度*0,图片的父控件的高度*0.5)为原点,图片旋转360度。
			 * 
			 */
			
			RotateAnimation rotateAnimation = new RotateAnimation(0, 360,
					Animation.RELATIVE_TO_PARENT, 0f,
					Animation.RELATIVE_TO_PARENT, 0.5f);
			rotateAnimation.setDuration(1000);
			animationSet.addAnimation(rotateAnimation);
			text.setAnimation(animationSet);
		}

	}

	class scaleListener implements OnClickListener {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			AnimationSet animationSet = new AnimationSet(true);

			/*  ScaleAnimation的三种参数方法*/
			/*
			 * fromX:起始X坐标上的伸缩尺寸
			 * toX:结束X坐标上的伸缩尺寸
			 * fromY:起始Y坐标上的伸缩尺寸
			 * toY:结束Y坐标上的伸缩尺寸
			 * 
			 * pivotX表示缩放的中轴点X坐标,距离自身左边缘的位置
			 * pivotY表示缩放的中轴点Y坐标,距离自身上边缘的位置
			 * 
			 * 
			 * pivotXType:X轴的伸缩模式,可以取值为ABSOLUTE、RELATIVE_TO_SELF、RELATIVE_TO_PARENT。
				pivotXValue:X坐标的伸缩值。
				pivotYType:Y轴的伸缩模式,可以取值为ABSOLUTE、RELATIVE_TO_SELF、RELATIVE_TO_PARENT。
                pivotYValue:Y坐标的伸缩值
			
			 第一种 ---> new ScaleAnimation(fromX, toX, fromY, toY);
			 解释如下:
			 ScaleAnimation(0.8f, 0.2f, 0.8f, 0.2f),
			 条件:设图片起始尺寸为(100,80),左上角的坐标为(0,0)
			 效果:以左上角的坐标为准, 从 宽100*0.8,高80*0.8  伸缩到 100*0.2, 高80*0.2
			 
			 第二种 --->new ScaleAnimation(fromX, toX, fromY, toY, pivotX, pivotY)
			 
			 
			 第三种 --->new ScaleAnimation(fromX, toX, fromY, toY, pivotXType, pivotXValue, pivotYType, pivotYValue)
			 解释如下:
			 ScaleAnimation(0.8f, 0.5f, 0.8f,0.5f, Animation.RELATIVE_TO_SELF ,0.5f,Animation.RELATIVE_TO_SELF, 0.5f);
			 条件:设图片的尺寸为(100,80),左上角的坐标为(0,0),那么中心点的坐标为(50,40)
			 效果:以中心点(50,40)为准, 图片从宽100*0.8f,高80*0.8 伸缩到 100*0.5, 高80*0.5

			*/
			
			ScaleAnimation scaleAnimation = new ScaleAnimation(0.8f, 0.5f, 0.8f,0.5f, Animation.RELATIVE_TO_SELF ,0.5f,
					Animation.RELATIVE_TO_SELF, 0.5f);
					

			scaleAnimation.setDuration(1000);
			animationSet.addAnimation(scaleAnimation);
			text.setAnimation(animationSet);

		}
	}
}


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

发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/181804.html原文链接:https://javaforall.cn

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)
blank

相关推荐

  • hdfs命令大全_hdfs创建文件夹命令

    hdfs命令大全_hdfs创建文件夹命令hdfs常用命令:第一部分:hdfs文件系统命令第一类:文件路径增删改查系列:hdfsdfs-mkdirdir创建文件夹hdfsdfs-rmrdir删除文件夹dirhdfsdfs-ls查看目录文件信息hdfsdfs-lsr递归查看文件目录信息hdfsdfs-statpath返回指定路径的信息第二类:空间大小查看系列命令:hdfsdfs-du-h…

  • googleearth离线地图_谷歌插件离线安装

    googleearth离线地图_谷歌插件离线安装Google离线地图API概要解析发布时间:2018-01-17版权: 1.说明离线地图发布有多种方式均可以实现,可以利用ArcGisServer、GeoServer等构建地图Web服务器,还可以使用谷歌地图、百度地图等API进行地图发布服务。本篇主要简单介绍如何调用Google离线地图API实现地图标注、获取坐标、及其他参数的设置。【如何发布Google离线地图】2.实…

  • 如何避免重复提交?分布式服务的幂等性设计!

    来源:https://www.cnblogs.com/QG-whz/p/10372458.html作者:melonstreet 为什么需要保证幂等性 编程中的“幂等性”是指任意多次…

  • 键盘钩子入门

    键盘钩子入门1钩子   钩子是操作系统消息处理的一种机制。通过钩子,应用程序可以安装一个钩子回调过程让系统调用,从而监视系统中的消息队列。在这些消息到达目标窗口之前对这些消息进行处理。1.1钩子函数1)钩子函数会降低操作系统的性能,因为它增加系统处理每一个消息的开销。所以用户除非必须才要安装钩子,而且还要尽可能早地去除钩子。2)操作系统支持多种类型的钩子,每种类型都提供了它特有的消息处理机制。3)对…

  • 深度学习在图像处理中的应用(tensorflow2.4以及pytorch1.10实现)

    深度学习在图像处理中的应用(tensorflow2.4以及pytorch1.10实现)本人在读研期间的研究方向是图像处理以及深度学习(主要是图像分类和目标检测)。在做深度学习时使用的是tensorflow深度学习框架,学习全是自学,很多资源都是在Github上找的。我发现现在Github上很多深度学习的开源项目都是用的tensorflow和pytorch框架。所以现在也开始学习pytorch框架,之前一直用的是tensorflow1.x版本,今年正好迎来了新的tensorlfow大…

  • Java多态实现原理

    Java多态实现原理##前言多态是Java语言重要的特性之一,它允许基类的指针或引用指向派生类的对象,而在具体访问时实现方法的动态绑定。Java对于方法调用动态绑定的实现主要依赖于方法表,但通过引用调用(invokevitual)和接口引用调用(invokeinterface)的实现则有所不同。Java多态实现原理的大致过程:首先是Java编译器将Java源代码编译成class文件。在编译过程中,会根据静态类型将调用的符号引用写到class文件中。在执行时,JVM根据class文件找到调用方法的符号引用,然后在静态类型的方

发表回复

您的电子邮箱地址不会被公开。

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