透明度动画、旋转动画、尺寸伸缩动画、移动动画

package com.javen.tween;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.os.Build;
import android.os.Bundle;
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.Button;
import android.widget.ImageView; public class MainActivity extends Activity {
private ImageView image;
private Button but1;
private Button but2;
Bitmap bm; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
but1.setOnClickListener(new View.OnClickListener() { @Override
public void onClick(View v) {
image.setImageBitmap(rotateToFit(bm, -90f));
bm = rotateToFit(bm, -90f);
}
});
but2.setOnClickListener(new View.OnClickListener() { @Override
public void onClick(View v) {
image.setImageBitmap(rotateToFit(bm, 90f));
bm = rotateToFit(bm, 90f);
}
}); image.setOnClickListener(new View.OnClickListener() { @TargetApi(Build.VERSION_CODES.HONEYCOMB)
@Override
public void onClick(View v) {
System.out.println("你点击了图片");
initAnimation();
}
});
} @SuppressLint("NewApi")
private void init() {
image = (ImageView) findViewById(R.id.image);
but1 = (Button) findViewById(R.id.but1);
but2 = (Button) findViewById(R.id.but2);
but1.setText("向左转");
but2.setText("向右转");
bm = BitmapFactory.decodeResource(getResources(), R.drawable.eee);
image.setImageBitmap(bm);
image.setAlpha(alpha);
} public static Bitmap rotateToFit(Bitmap bm, float degrees) {
int wight = bm.getWidth();
int height = bm.getHeight(); Matrix matrix = new Matrix();
matrix.postRotate(degrees);
Bitmap resBitmap = Bitmap.createBitmap(bm, 0, 0, wight, height, matrix,
true);
return resBitmap; }
private Animation animation_alpha,animation_scale,animation_translate,animation_rotate;
private AnimationSet animationSet;
private void initAnimation() {
//透明度控制动画效果 alpha
animation_alpha=new AlphaAnimation(0.1f,1.0f);
//第一个参数fromAlpha为 动画开始时候透明度
//第二个参数toAlpha为 动画结束时候透明度
animation_alpha.setRepeatCount(0);//设置循环
animation_alpha.setDuration(5000);//设置时间持续时间为 5000毫秒 // 旋转效果rotate
animation_rotate = new RotateAnimation(0, -720,
RotateAnimation.RELATIVE_TO_SELF, 0.5f,
RotateAnimation.RELATIVE_TO_SELF, 0.5f);
//第一个参数fromDegrees为动画起始时的旋转角度
//第二个参数toDegrees为动画旋转到的角度
//第三个参数pivotXType为动画在X轴相对于物件位置类型
//第四个参数pivotXValue为动画相对于物件的X坐标的开始位置
//第五个参数pivotXType为动画在Y轴相对于物件位置类型
//第六个参数pivotYValue为动画相对于物件的Y坐标的开始位置
animation_rotate.setRepeatCount(0);
animation_rotate.setDuration(5000);//设置时间持续时间为 5000毫秒 //尺寸伸缩动画效果 scale
animation_scale=new ScaleAnimation(0.1f,3.0f,0.1f,3.0f,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
//第一个参数fromX为动画起始时 X坐标上的伸缩尺寸
//第二个参数toX为动画结束时 X坐标上的伸缩尺寸
//第三个参数fromY为动画起始时Y坐标上的伸缩尺寸
//第四个参数toY为动画结束时Y坐标上的伸缩尺寸
/*说明:
以上四种属性值
0.0表示收缩到没有
1.0表示正常无伸缩
值小于1.0表示收缩
值大于1.0表示放大
*/
//第五个参数pivotXType为动画在X轴相对于物件位置类型
//第六个参数pivotXValue为动画相对于物件的X坐标的开始位置
//第七个参数pivotXType为动画在Y轴相对于物件位置类型
//第八个参数pivotYValue为动画相对于物件的Y坐标的开始位置
animation_scale.setRepeatCount(0);
animation_scale.setDuration(5000);//设置时间持续时间为 5000毫秒 //移动动画效果translate
animation_translate=new TranslateAnimation(-20f,300f,-20f,300f);
//第一个参数fromXDelta为动画起始时 X坐标上的移动位置
//第二个参数toXDelta为动画结束时 X坐标上的移动位置
//第三个参数fromYDelta为动画起始时Y坐标上的移动位置
//第三个参数toYDelta为动画结束时Y坐标上的移动位置
animation_translate.setRepeatCount(0);//设置动画执行多少次,如果是-1的话就是一直重复
animation_translate.setDuration(5000);//设置时间持续时间为 5000毫秒 animationSet=new AnimationSet(true); animationSet.addAnimation(animation_alpha);//透明度
animationSet.addAnimation(animation_rotate);//旋转
animationSet.addAnimation(animation_scale);//尺寸伸缩
animationSet.addAnimation(animation_translate);//移动
image.startAnimation(animationSet);//开始播放
} }

Android动画Animation之Tween用代码实现动画的更多相关文章

  1. Android动画效果之Tween Animation(补间动画)

    前言: 最近公司项目下个版本迭代里面设计了很多动画效果,在以往的项目中开发中也会经常用到动画,所以在公司下个版本迭代开始之前,抽空总结一下Android动画.今天主要总结Tween Animation ...

  2. Android动画学习(二)——Tween Animation

    前两天写过一篇Android动画学习的概述,大致的划分了下Android Animation的主要分类,没有看过的同学请移步:Android动画学习(一)——Android动画系统框架简介.今天接着来 ...

  3. Android 动画——Frame Animation与Tween Animation

    很多手机应用的引导页都是动画的,添加动画后的应用画面会更加生动灵活,今天博主也学习了Android中Animation的使用,下面来总结下.  android中的Animation分为两种,一种是Fr ...

  4. Android Property Animation动画

    3.0以前,android支持两种动画模式,tween animation,frame animation,在android3.0中又引入了一个新的动画系统:property animation,这三 ...

  5. 【Android动画】之Tween动画 (渐变、缩放、位移、旋转)

    Android 平台提供了两类动画. 一类是Tween动画,就是对场景里的对象不断的进行图像变化来产生动画效果(旋转.平移.放缩和渐变). 第二类就是 Frame动画,即顺序的播放事先做好的图像,与g ...

  6. Android Property Animation 物业动画

    效果图:   Property Animation介绍: 出生在sdk3.0,是利用了View所拥有的属性,进行一系列的操作. 比方一个View有什么样的setAbc的属性,那么理论上就能够设置它. ...

  7. Android动画Animation简单示例

    Animation是Android给我们提供的一个可以实现动画效果的API,利用Animation我们可以实现一系列的动画效果,比如缩放动画,透明度动画,旋转动画,位移动画,布局动画,帧动画等等.An ...

  8. 虾扯蛋:Android View动画 Animation不完全解析

    本文结合一些周知的概念和源码片段,对View动画的工作原理进行挖掘和分析.以下不是对源码一丝不苟的分析过程,只是以搞清楚Animation的执行过程.如何被周期性调用为目标粗略分析下相关方法的执行细节 ...

  9. 安卓开发20:动画之Animation 详细使用-主要通过java代码实现动画效果

    AlphaAnimation 透明效果实现: activity_main.xml中仅仅是一个简单的图片,下面的例子都会使用这个xml: <RelativeLayout xmlns:android ...

随机推荐

  1. 登陆shell与交互式非登陆shell的区别

    登录shell 所谓登录shell,指的是当用户登录系统时所取的那个shell,登录shell属于交互式shell. 登录shell将查找4个不同的启动文件来处理其中的命令. bash shell处理 ...

  2. C语言一维数组中的数据随机排列

    #include <stdio.h>#include <stdlib.h> void randomlize(int *a, int n){        int i = 0,j ...

  3. freemarker 如何获得list的索引值

    <#list toplist as toplists> ${toplists_index} </#list> 相当方便

  4. 修改Android默认背光值

    /********************************************************************* * 修改Android默认背光值 * 说明: * 本文主要 ...

  5. Java [leetcode 19]Remove Nth Node From End of List

    题目描述: Given a linked list, remove the nth node from the end of list and return its head. For example ...

  6. data guard折腾记一

    终于有空闲的机器腾出来了,生产环境上的一套Oracle环境终于可以鸟枪换炮了,生产环境有Data Guard,为了减少停机时间,而且避免重新构建Data Guard的麻烦(其实也不麻烦,就是浪费时间) ...

  7. C语言块内变量回收问题

    之前有一个错误认识,错误的认为局部变量的回收是发生在函数返回时.其实在块结束时块内使用的内容就会被回收了. 以下的实例说明了问题 ]; ; i < ; ++i) { int item = i; ...

  8. postInvalidate、removeAllViewsInLayout、refreshDrawableState用法

    postInvalidate.invalidate:会调用控件的onDraw()重绘控件 refreshDrawableState:当控件在使用一个对控件状态敏感的Drawable对象时使用,如一个B ...

  9. memcached 学习笔记

    memcached是高性能的分布式内存缓存服务器.一般的使用目的是,通过缓存数据库查询结果,减少数据库访问次数,以提高动态应用的速度.提高可扩展性. Memcached基于一个存储键/值对的hashm ...

  10. [selenium webdriver Java]检查元素状态

    许多测试失败是因为点击一个元素失败或者在一个不可见的字段中输入文字,或者是在不可输入的文本中输入文字. 我们可以在具体操作之前,检查一下元素的状态.WebElement类提供了这样的方法. 方法 目的 ...