android动画
2015-11-16 12:05
459 查看
Animation: 不管怎么变换,只有原始位置会响应事件,新位置不会响应事件
*
属性动画: 变换后的位置可以响应事件,原来的位置不再响应事件
//自定义Animtion
MyAnimation animation=new MyAnimation();
v.startAnimation(animation);
CircleView civ=(CircleView) findViewById(R.id.civ);
//属性动画方式1
PropertyValuesHolder pvh1=PropertyValuesHolder.ofFloat("translationX", 300);
PropertyValuesHolder pvh2=PropertyValuesHolder.ofFloat("scaleX", 1,0,1f);
PropertyValuesHolder pvh3=PropertyValuesHolder.ofFloat("scaleY", 1,0,1);
final ObjectAnimator anim=ObjectAnimator.ofPropertyValuesHolder(civ, pvh1,pvh2,pvh3);
anim.setDuration(3000).start();
anim.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
}
});
//
//属性动画方式2
CircleView civ2=(CircleView) findViewById(R.id.civ2);
// 3.0之后才有animate()方法
civ2.animate().alpha(0.5f).rotationY(360).translationX(100)
.setDuration(3000).scaleX(0.5f).scaleY(0.5f)
.withEndAction(new Runnable() {
@Override
public void run() {
}
}).start();
/*
* 布局动画
*
* 在给ViewGroup中addView时添加的一个动画过度效果(每一个子控件都会执行,可以选择执行顺序)。
* 最简单的方式是在布局文件中的父布局中添加如下代码
* android:animateLayoutChanges="true"
* 不过这种方式是安卓默认的效果,无法改变
*
* 可以通过如下方式来改变效果
*/
final ViewGroup viewGroup=(ViewGroup) findViewById(R.id.container);
ScaleAnimation sa=new ScaleAnimation(0, 1, 0, 1); //x方向从0到1 y方向从0到1
sa.setDuration(5000);
//0.5 每个子控件执行动画的延时
final LayoutAnimationController lac=new LayoutAnimationController(sa,0.5f);
/*
* LayoutAnimationController.ORDER_NORMAL 顺序执行
* LayoutAnimationController.ORDER_RANDOM 随机执行
* LayoutAnimationController.ORDER_REVERSE 反序执行
*
*/
lac.setOrder(LayoutAnimationController.ORDER_NORMAL);
//调用该方法后会执行动画(每个子控件都会执行)
viewGroup.setLayoutAnimation(lac);
civ.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
ImageView child=new ImageView(getApplicationContext());
child.setImageResource(R.drawable.pic);
// TODO Auto-generated method stub
viewGroup.addView(child);
//
viewGroup.setLayoutAnimation(lac);
}
});
*
属性动画: 变换后的位置可以响应事件,原来的位置不再响应事件
//自定义Animtion
MyAnimation animation=new MyAnimation();
v.startAnimation(animation);
package com.example.androidtest; import android.graphics.Camera; import android.graphics.Matrix; import android.view.animation.Animation; import android.view.animation.LinearInterpolator; import android.view.animation.ScaleAnimation; import android.view.animation.Transformation; public class MyAnimation extends Animation { private int centerX; private int centerY; //用来修改动画变换矩阵 private Camera camera; private final int rotateY=300; //可以在这初始化 @Override public void initialize(int width, int height, int parentWidth, int parentHeight) { // TODO Auto-generated method stub super.initialize(width, height, parentWidth, parentHeight); setDuration(3000); setFillAfter(true); setInterpolator(new LinearInterpolator()); centerX=parentWidth/2; centerY=parentHeight/2; camera=new Camera(); } @Override protected void applyTransformation(float interpolatedTime, Transformation t) { // TODO Auto-generated method stub Matrix matrix=t.getMatrix(); camera.save(); camera.rotateY(interpolatedTime*rotateY); //把旋转变换作用到矩阵上 camera.getMatrix(matrix); camera.restore(); //下面两行代码改变旋转时的默认旋转中心 matrix.preTranslate(centerX, centerY); matrix.postTranslate(-centerX, -centerY); } }
CircleView civ=(CircleView) findViewById(R.id.civ);
//属性动画方式1
PropertyValuesHolder pvh1=PropertyValuesHolder.ofFloat("translationX", 300);
PropertyValuesHolder pvh2=PropertyValuesHolder.ofFloat("scaleX", 1,0,1f);
PropertyValuesHolder pvh3=PropertyValuesHolder.ofFloat("scaleY", 1,0,1);
final ObjectAnimator anim=ObjectAnimator.ofPropertyValuesHolder(civ, pvh1,pvh2,pvh3);
anim.setDuration(3000).start();
anim.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
}
});
//
//属性动画方式2
CircleView civ2=(CircleView) findViewById(R.id.civ2);
// 3.0之后才有animate()方法
civ2.animate().alpha(0.5f).rotationY(360).translationX(100)
.setDuration(3000).scaleX(0.5f).scaleY(0.5f)
.withEndAction(new Runnable() {
@Override
public void run() {
}
}).start();
/*
* 布局动画
*
* 在给ViewGroup中addView时添加的一个动画过度效果(每一个子控件都会执行,可以选择执行顺序)。
* 最简单的方式是在布局文件中的父布局中添加如下代码
* android:animateLayoutChanges="true"
* 不过这种方式是安卓默认的效果,无法改变
*
* 可以通过如下方式来改变效果
*/
final ViewGroup viewGroup=(ViewGroup) findViewById(R.id.container);
ScaleAnimation sa=new ScaleAnimation(0, 1, 0, 1); //x方向从0到1 y方向从0到1
sa.setDuration(5000);
//0.5 每个子控件执行动画的延时
final LayoutAnimationController lac=new LayoutAnimationController(sa,0.5f);
/*
* LayoutAnimationController.ORDER_NORMAL 顺序执行
* LayoutAnimationController.ORDER_RANDOM 随机执行
* LayoutAnimationController.ORDER_REVERSE 反序执行
*
*/
lac.setOrder(LayoutAnimationController.ORDER_NORMAL);
//调用该方法后会执行动画(每个子控件都会执行)
viewGroup.setLayoutAnimation(lac);
civ.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
ImageView child=new ImageView(getApplicationContext());
child.setImageResource(R.drawable.pic);
// TODO Auto-generated method stub
viewGroup.addView(child);
//
viewGroup.setLayoutAnimation(lac);
}
});
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- Android IPC进程间通讯机制
- Android Manifest 用法
- [转载]Activity中ConfigChanges属性的用法
- Android之获取手机上的图片和视频缩略图thumbnails
- Android之使用Http协议实现文件上传功能
- Android学习笔记(二九):嵌入浏览器
- android string.xml文件中的整型和string型代替
- i-jetty环境搭配与编译
- android之定时器AlarmManager
- android wifi 无线调试
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- android 代码实现控件之间的间距
- android FragmentPagerAdapter的“标准”配置
- Android"解决"onTouch和onClick的冲突问题
- android:installLocation简析
- android searchView的关闭事件
- SourceProvider.getJniDirectories