java运用ascii实现动画效果_安卓开发20:动画之Animation 详细使用-主要通过java代码实现动画效果...
AlphaAnimation 透明效果实现:
activity_main.xml中仅仅是一个简单的图片,下面的例子都会使用这个xml:
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" >
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" />
透明效果的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;
}
java运用ascii实现动画效果_安卓开发20:动画之Animation 详细使用-主要通过java代码实现动画效果...相关推荐
- java扬声器和耳机自动切换_安卓插耳机也外放扬声器播放音频的java代码
最近遇到一个如何在耳机插入的情况下任然用扬声器播放音频的问题. 用搜索引擎找了一些网上的demo(案例) .发现按照他们的方法成功实现. 插入耳机的时候也可以选择使用扬声器播放音乐,来电铃声就是这么用 ...
- html鼠标悬停效果_【开发小技巧】023—如何使用HTML和CSS实现3D文字效果
来源 | https://www.geeksforgeeks.org/create-a-3d-text-effect-using-html-and-css/3D文字效果是网页设计领域中最常用的文字效果 ...
- JS实现仿新浪微博大厅和腾讯微博首页滚动效果_前端开发
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/ ...
- 安卓开发20:动画之Animation 详细使用-主要通过java代码实现动画效果
AlphaAnimation 透明效果实现: activity_main.xml中仅仅是一个简单的图片,下面的例子都会使用这个xml: <RelativeLayout xmlns:android ...
- android淡入淡出动画循环,Android应用开发之淡入淡出、缩放、旋转、平移、组合动画效果代码实现...
本文将带你了解Android应用开发Android动画开发之淡入淡出.缩放.旋转.平移.组合动画效果代码实现,希望本文对大家学Android有所帮助. 1.activity_main.xml文件 an ...
- gitee项目能用SVN拉取吗_安卓开发工程师-简历范文,【工作经历+项目经验+自我评价】怎么写...
安卓开发工程师-简历模板,项目经验+自我评价怎么写 [网盘下载]100+清新大气简历模板: https://zhuanlan.zhihu.com/p/115911695 https://zhuanla ...
- java c语言语法对比_c语言和java语法有区分吗?_后端开发
c言语和java语法有区分吗? c言语和java在语法上有区分,区分是: 1.C言语有指针,java没有指针: C言语的语法比较简单,然则它的亮点指针很轻易失足,想要好好的应用指针是件很难的事变,用好 ...
- android 获取控件高度_安卓开发入门教程UI控件_ImageView
什么是ImageView ImageView是用于显示图片的UI控件. 基础样例 1.展示本地图片 效果图 代码 <ImageViewandroid:layout_width="wra ...
- 安卓androidstudio访问本地接口_安卓开发之数据存储在本地的四种方式
安卓开发之数据存储在本地的四种方式 本地数据存储,在安卓开发过程中是不可避免的一个话题.这些本地的数据可能是用户的设置,程序的设置,用户的数据图片, 也可能是网络传输的一些缓冲数据. 基本上我们有 ...
最新文章
- ACM寒假训练第一周总结
- VB 字节数组和字符串的转换问题 (StringByte)
- Winform中实现自定义水晶按钮控件(附代码下载)
- python语句分为什么语句_在python中如何分句
- 计组之数据运算:6、原码乘法运算
- 【缓存】redis的基本使用
- python 太灵活_Python中的灵活参数
- 谷歌为安卓系统加入Fast Pair技术 蓝牙耳机将实现设备自动切换
- CDS ORF 5‘UTR 3'UTR
- OpenCV学习笔记——多种Smooth平滑处理
- h2 不能访问localhost_Spring 配置的 H2 控制台 frameOptions 导致无法访问
- Android Studio真机测试失败-----''No target device found
- 了解更多关于11gR2 diskmon
- 网站html超链接移动,HTML - 超链接
- MAC电脑 系统 恢复出厂设置
- python+openCV 获取背景(三)
- 使用python获取美股行情数据
- Python学习-1.基础语法元素
- 远程访问云服务器CentOS下 jupyter服务 【保姆级教程】
- Hadoop学习笔记——Hadoop常用命令
热门文章
- 广告点击率预测_用于广告点击率预测的逻辑回归你会了吗?
- docker jenkins 公钥_搭建 Jenkins 与 GitLab 的持续集成环境
- git版本库--常用命令
- (11)Node.js 核心模块fs – 同步函数( synchronization )
- 一.路径规划---二维路径规划仿真实现-gmapping+amcl+map_server+move_base
- mysql show 存储过程_mysql 存储过程 show errors
- php geteditor,wordpress函数get_editor_stylesheets()用法示例
- 惯性导航算法_自动驾驶关键技术报告:惯性导航和背后的芯片大战
- python怎么让画笔向下移动_Pandas 解决dataframe的一列进行向下顺移问题
- js获取request中的值_基于node.js的开发框架 — Koa