- 浏览: 899543 次
文章分类
最新评论
-
sunnyhappylg:
网上怎么复制了 这个页面内容这么多 没介绍怎么解决啊
服务器系统及软件常见漏洞 -
数据工厂:
我用JS实现的糗事百科的爬虫源码如下,运行步骤请参考我的git ...
[Python]网络爬虫(八):糗事百科的网络爬虫(v0.2)源码及解析 -
yzg0885:
解决了,谢谢你
Could not create the view: An unexpected exception was thrown. 电脑突然断电,myeclipse非正常关闭,出现错误 -
jjlin00:
楼主好,我今年报考南大软院,能分享计算机综合的资料吗,真心谢谢 ...
2013南京大学软件学院考研有感 -
govy.b:
楼主的资料能分享吗?QQ:282577229
2013南京大学软件学院考研有感
安卓开发20:动画之Animation 详细使用-主要通过java代码实现动画效果
AlphaAnimation 透明效果实现:
activity_main.xml中仅仅是一个简单的图片,下面的例子都会使用这个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" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <!-- 用于动画的图片 --> <ImageView android:id="@+id/imageView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_marginLeft="70dp" android:layout_marginTop="138dp" android:src="@drawable/jhs_button1_h" /> </RelativeLayout>
透明效果的java代码:
package com.example.test.com; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.view.Window; import android.view.animation.AlphaAnimation; import android.view.animation.Animation; import android.view.animation.DecelerateInterpolator; import android.widget.ImageView; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_main); ImageView imageView = (ImageView) findViewById(R.id.imageView1); //图片点击的时候,启动动画效果 imageView.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Animation anim = getAlphaAnimation(); v.startAnimation(anim); } }); } /** * 透明效果 * @return */ public Animation getAlphaAnimation() { //实例化 AlphaAnimation 主要是改变透明度 //透明度 从 1-不透明 0-完全透明 Animation animation = new AlphaAnimation(1.0f, 0.5f); //设置动画插值器 被用来修饰动画效果,定义动画的变化率 animation.setInterpolator(new DecelerateInterpolator()); //设置动画执行时间 animation.setDuration(1000); return animation; } }
ScaleAnimation 缩放效果实现:
package com.example.test.com; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.view.Window; import android.view.animation.AlphaAnimation; import android.view.animation.Animation; import android.view.animation.DecelerateInterpolator; import android.view.animation.ScaleAnimation; import android.widget.ImageView; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_main); ImageView imageView = (ImageView) findViewById(R.id.imageView1); //图片点击的时候,启动动画效果 imageView.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Animation scalegetAnim = getScaleAnimation(); v.startAnimation(scalegetAnim); } }); } /** * 缩放动画 * @return */ public Animation getScaleAnimation() { //实例化 ScaleAnimation 主要是缩放效果 //参数:fromX-动画开始前,x坐标 toX-动画结束后x坐标 //fromY-动画开始前,Y坐标 toY-动画结束后Y坐标 //pivotXType - 为动画相对于物件的X坐标的参照物 pivotXValue - 值 //pivotYType - 为动画相对于物件的Y坐标的参照物 pivotYValue - 值 Animation animation = new ScaleAnimation(1.0f, 0.0f, 1.0f, 0.0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); //设置动画插值器 被用来修饰动画效果,定义动画的变化率 animation.setInterpolator(new DecelerateInterpolator()); //设置动画执行时间 animation.setDuration(1000); return animation; } }
RotateAnimation 旋转效果实现:
package com.example.test.com; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.view.Window; import android.view.animation.AlphaAnimation; import android.view.animation.Animation; import android.view.animation.DecelerateInterpolator; import android.view.animation.RotateAnimation; import android.view.animation.ScaleAnimation; import android.widget.ImageView; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_main); ImageView imageView = (ImageView) findViewById(R.id.imageView1); //图片点击的时候,启动动画效果 imageView.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Animation rotateAnim = getRotateAnimation(); v.startAnimation(rotateAnim); } }); } /** * 旋转 * @return */ public Animation getRotateAnimation() { //实例化RotateAnimation //以自身中心为圆心,旋转360度 正值为顺时针旋转,负值为逆时针旋转 RotateAnimation animation = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); //设置动画插值器 被用来修饰动画效果,定义动画的变化率 animation.setInterpolator(new DecelerateInterpolator()); //设置动画执行时间 animation.setDuration(1000); return animation; } }
TranslateAnimation 移动效果实现:
package com.example.test.com; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.view.Window; import android.view.animation.AlphaAnimation; import android.view.animation.Animation; import android.view.animation.DecelerateInterpolator; import android.view.animation.RotateAnimation; import android.view.animation.ScaleAnimation; import android.view.animation.TranslateAnimation; import android.widget.ImageView; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_main); ImageView imageView = (ImageView) findViewById(R.id.imageView1); //图片点击的时候,启动动画效果 imageView.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Animation translateAnim = getTranslateAnimation(); v.startAnimation(translateAnim); } }); } /** * 移动 * @return */ public Animation getTranslateAnimation() { //实例化TranslateAnimation //以自身为坐标系和长度单位,从(0,0)移动到(1,1) TranslateAnimation animation = new TranslateAnimation( Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 1.0f, Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 1.0f); //设置动画插值器 被用来修饰动画效果,定义动画的变化率 animation.setInterpolator(new DecelerateInterpolator()); //设置动画执行时间 animation.setDuration(1000); return animation; } }
AnimationSet 动画集合实现和使用:
package com.example.test.com; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.view.Window; import android.view.animation.AlphaAnimation; import android.view.animation.Animation; import android.view.animation.AnimationSet; import android.view.animation.DecelerateInterpolator; import android.view.animation.RotateAnimation; import android.view.animation.ScaleAnimation; import android.view.animation.TranslateAnimation; import android.widget.ImageView; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_main); ImageView imageView = (ImageView) findViewById(R.id.imageView1); //图片点击的时候,启动动画效果 imageView.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { //AnimationSet类是一个Animation集合,里面可以许多Animation, //且在AnimationSet中设置的属性适用于里面的所有Animation。 //参数true 则共享@Interpolator AnimationSet set = new AnimationSet(true); //透明 Animation alphaAnim = getAlphaAnimation(); set.addAnimation(alphaAnim); //缩放 Animation scalegetAnim = getScaleAnimation(); set.addAnimation(scalegetAnim); //旋转 Animation rotateAnim = getRotateAnimation(); set.addAnimation(rotateAnim); //移动 上面三个动画是同时进行的,我现在需要让移动这个动画在上面的动画之后执行 //需要使用setStartOffset 设置动画开始的时间 Animation translateAnim = getTranslateAnimation(); translateAnim.setStartOffset(500); set.addAnimation(translateAnim); v.startAnimation(set); } }); } /** * 透明效果 * @return */ public Animation getAlphaAnimation() { //实例化 AlphaAnimation 主要是改变透明度 //透明度 从 1-不透明 0-完全透明 Animation animation = new AlphaAnimation(1.0f, 0.8f); //设置动画插值器 被用来修饰动画效果,定义动画的变化率 animation.setInterpolator(new DecelerateInterpolator()); //设置动画执行时间 animation.setDuration(1000); return animation; } /** * 缩放动画 * @return */ public Animation getScaleAnimation() { //实例化 ScaleAnimation 主要是缩放效果 //参数:fromX-动画开始前,x坐标 toX-动画结束后x坐标 //fromY-动画开始前,Y坐标 toY-动画结束后Y坐标 //pivotXType - 为动画相对于物件的X坐标的参照物 pivotXValue - 值 //pivotYType - 为动画相对于物件的Y坐标的参照物 pivotYValue - 值 Animation animation = new ScaleAnimation(1.0f, 0.0f, 1.0f, 0.0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); //设置动画插值器 被用来修饰动画效果,定义动画的变化率 animation.setInterpolator(new DecelerateInterpolator()); //设置动画执行时间 animation.setDuration(1000); return animation; } /** * 旋转 * @return */ public Animation getRotateAnimation() { //实例化RotateAnimation //以自身中心为圆心,旋转360度 正值为顺时针旋转,负值为逆时针旋转 RotateAnimation animation = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); //设置动画插值器 被用来修饰动画效果,定义动画的变化率 animation.setInterpolator(new DecelerateInterpolator()); //设置动画执行时间 animation.setDuration(1000); return animation; } /** * 移动 * @return */ public Animation getTranslateAnimation() { //实例化TranslateAnimation //以自身为坐标系和长度单位,从(0,0)移动到(1,1) TranslateAnimation animation = new TranslateAnimation( Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 1.0f, Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 1.0f); //设置动画插值器 被用来修饰动画效果,定义动画的变化率 animation.setInterpolator(new DecelerateInterpolator()); //设置动画执行时间 animation.setDuration(1000); return animation; } }
Interpolator 描述动画的速率:
安卓默认的Interpolator:
AccelerateInterpolator:动画开始时比较慢,然后逐渐加速。
DecelerateInterpolator:动画开始时比较快,然后逐渐减速。
AccelerateDecelerateInterpolator:动画开始时和结束时比较慢,中间过程加速。
LinearInterpolator:动画匀速进行。
CycleInterpolator:动画循环播放指定次数,速率沿着正弦曲线改变。
DecelerateInterpolator代码:主要实现getInterpolation ,也可以自定义
/* * Copyright (C) 2007 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package android.view.animation; import android.content.Context; import android.content.res.TypedArray; import android.util.AttributeSet; /** * An interpolator where the rate of change starts out quickly and * and then decelerates. * */ public class DecelerateInterpolator implements Interpolator { public DecelerateInterpolator() { } /** * Constructor * * @param factor Degree to which the animation should be eased. Setting factor to 1.0f produces * an upside-down y=x^2 parabola. Increasing factor above 1.0f makes exaggerates the * ease-out effect (i.e., it starts even faster and ends evens slower) */ public DecelerateInterpolator(float factor) { mFactor = factor; } public DecelerateInterpolator(Context context, AttributeSet attrs) { TypedArray a = context.obtainStyledAttributes(attrs, com.android.internal.R.styleable.DecelerateInterpolator); mFactor = a.getFloat(com.android.internal.R.styleable.DecelerateInterpolator_factor, 1.0f); a.recycle(); } public float getInterpolation(float input) { float result; if (mFactor == 1.0f) { result = (float)(1.0f - (1.0f - input) * (1.0f - input)); } else { result = (float)(1.0f - Math.pow((1.0f - input), 2 * mFactor)); } return result; } private float mFactor = 1.0f; }
使用:
//设置动画插值器 被用来修饰动画效果,定义动画的变化率 animation.setInterpolator(new DecelerateInterpolator());
AnimationListener 监听器:
可以监听动画前,动画结束,动画repeat的时候的动作,对上面代码中的移动效果进行动画监听:
/** * 移动 * @return */ public Animation getTranslateAnimation() { //实例化TranslateAnimation //以自身为坐标系和长度单位,从(0,0)移动到(1,1) TranslateAnimation animation = new TranslateAnimation( Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 1.0f, Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 1.0f); //设置动画插值器 被用来修饰动画效果,定义动画的变化率 animation.setInterpolator(new DecelerateInterpolator()); //设置重复动画 animation.setRepeatCount(2); //设置动画执行时间 animation.setDuration(1000); //设置监听器 animation.setAnimationListener(new AnimationListener() { @Override public void onAnimationStart(Animation animation) { //动画开始前 Toast.makeText(getBaseContext(), "Strart!", Toast.LENGTH_SHORT).show(); } @Override public void onAnimationRepeat(Animation animation) { //重复动画的时候, Toast.makeText(getBaseContext(), "Repeat!", Toast.LENGTH_SHORT).show(); } @Override public void onAnimationEnd(Animation animation) { // 结束动画的时候 Toast.makeText(getBaseContext(), "End!", Toast.LENGTH_SHORT).show(); } }); return animation; }
相关推荐
可以通过xml实现也可以通过java代码实现。逐帧动画适合实现比较简单的动画效果,如果要实现复杂动画不太建议使用逐帧动画。 xml方式实现: step 1 : 在res/drawable目录下一个文件sample_animlist.xml: <?xml ...
的 Android SDK 提供了在 Android 平台上使用 JaVa 语言进行 Android 应用开发必须的工具和 API 接口。 特性 • 应用程序框架 支持组件的重用与替换 • Dalvik Dalvik Dalvik Dalvik 虚拟机 专为移动设备优化 • ...
|--动画Animation详解 |--动画之view左右抖动 |--动画之移动动画 |--动画之组合动画 |--动画之缩放动画ScaleAnimation |--反序列化对象 |--发送短信 读天气 调音量 |--回调函数的定义 |--图片之BitMap、Drawable、...
Android的动画animation的两种方式,补间动画和逐帧动画都有实现,两种方式的XML实现和JAVACODE实现都有。
app使用的是sqlite数据库,使用的核心类及其组件:Base Adapter,Fragment,View Pager,Alert Dialog.Builder,Option,Animation Draw able(关键帧动画),Media Player(视频),Count Down Timer(倒计时 广告...
Photo Motion v1.3 - 照片中的动画和背景中的运动android studio动态壁纸app java项目源码 Photo Motion - Animation in Photos and Motion in backgrounds 动画创建器具有直观的箭头设置照片运动方向。带有令人惊叹...
Inspired by Effect Usage In your app's build.gradle file,add it to your dependencies: dependencies { compile 'org.seniorzhai.scoreboard:scoreboard:1.0.1' ...Clone and build this repo in Android Stud
在Android中,动画Animation的实现有两种方式:Tween Animation(补间动画)和Frame Animation(帧动画)。渐变动画是通过对场景里的对象不断做图像变换(平移、缩放、旋转等)产生动画效果。帧动画则是通过顺序播放...
将动画效果兼容至Android4.0 -- Usage java //参数一:当前Activity //参数二:跳转意图 //参数三:当前页面跳转至下一页面的View //参数四:下一页面关联的View id TransitionController.getInstance().start...
Android-Animation-DemoAndroid 动画调研该项目主要演示如何使用 ViewAnimation 、DrawableAnimation、PropertyAnimation、LayoutAnimation 的使用方式。配套博客地址:
在使用Animation时,可以在.java文件中用java代码对其进行设置,这样的优点是可以方便调试程序效果;另外一种方法就是在xml中对控件的属性做设置,好处是代码的重用性比较高,缺点是不方便调试。本节中就简单介绍下...
android_canvas_animation 如何使用画布创建动画的示例-Android
Bored with same animation for activity transition? Animatoo is a lightweight and easy to use Android library that provides many activity transition animations min SDK 16 (Android Jellybean 4.1) ...
[Android][Animation动画]
animation有四种动画类型:分别为alpha(透明的渐变)、rotate(旋转)、scale(尺寸伸缩)、translate(移动),二实现的分发有两种,一种是javaCode,另外一种是XML,而我今天要说的是XML实现的方法,个人感觉javaCode的...
android-animation-collections ShakeAnimation 仿IOS卸载的晃动动画&文本标签编辑的晃动动画 PropertyAnimation 属性动画、补间动画测试
2、修改ViewPager内部代码+使用nineoldandroids代替属性动画实现向下兼容 3、观察API的规律,自定义我们的切换动画 二、 1、自定义ViewPager实现动画切换效果 Translation Scale a、需要拿到当前切换的两个View -> ...
lib-animAndroid 动画库,动画相关的功能封装,方便使用, 支持动画的with操作和连续动画(组)操作,通过代理支持属性动画中的各种参数设置,完成同样的动画效果可以减少一半以上的y原生代码调用用法: 最新版 Add it ...
Ramotion开发的Android库,UI组件和自定义动画的精选列表 材料设计扩展了受折叠纸张材料启发的内容单元格 请随时关注最新更新: Android库Paper Onboarding是用Java编写的材质设计UI滑块 请随时关注最新更新: ...
之前分别介绍了View Animation和Drawable Animation,学了就要用啊,今天给大家一个使用View Animation实现动画加载界面的实现。 首先先看一下实现效果。 下面是实现代码 package com.example.animationloading; ...