Android开发笔记: android实用6种特效总结
来源:互联网 发布:淘宝卖家采集器破解版 编辑:程序博客网 时间:2024/05/29 19:18
1 先看效果图
2 动画
布局文件
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="animate.cuanbo.myanimate.MainActivity"> <Button android:id="@+id/btn_animate1" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="1-按钮 渐变动画"/> <Button android:id="@+id/btn_animate2" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="2-按钮 旋转动画"/> <Button android:id="@+id/btn_animate3" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="3-按钮 移动特效"/> <Button android:id="@+id/btn_animate4" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="4-按钮 缩放动画"/> <Button android:id="@+id/btn_animate5" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="5-混合动画"/> <Button android:id="@+id/btn_animate6" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="6-自定义动画"/></LinearLayout>
1 渐变动画
// 1 渐变动画 Button btn_animate1 = (Button)findViewById(R.id.btn_animate1); btn_animate1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { // 点击按钮后,慢慢显示出来 aa1 = new AlphaAnimation(0,1); aa1.setDuration(1000); view.startAnimation(aa1); // 使用布局文件加载特效 //view.startAnimation(AnimationUtils.loadAnimation(getBaseContext(),R.anim.animate_alpha)); } });
资源文件:
<?xml version="1.0" encoding="utf-8"?><alpha xmlns:android="http://schemas.android.com/apk/res/android" android:fromAlpha="0" android:toAlpha="1" android:duration = "1000"></alpha>
2 旋转动画
Button btn_animate2 = (Button)findViewById(R.id.btn_animate2); btn_animate2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { // 点击按钮后,绕着(100,50)坐标点旋转 // RotateAnimation aa = new RotateAnimation(0,360,100,50); // 围绕自身中心位置旋转 ra2 = new RotateAnimation(0,360, Animation.RELATIVE_TO_SELF,0.5F,Animation.RELATIVE_TO_SELF,0.5F); ra2.setDuration(1000); view.startAnimation(ra2); // 使用布局文件加载特效 //view.startAnimation(AnimationUtils.loadAnimation(getBaseContext(),R.anim.animate_rotate)); } });
资源文件:
<?xml version="1.0" encoding="utf-8"?><rotate xmlns:android="http://schemas.android.com/apk/res/android" android:fromDegrees="0" android:toDegrees="360" android:duration = "1000" android:pivotX="50%" android:pivotY="50%" ></rotate>
3 移动动画
Button btn_animate3 = (Button)findViewById(R.id.btn_animate3); btn_animate3.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { //当前位置移动一个增量,向有移动200,向下移动200 ta3 = new TranslateAnimation(0,200,0,300); ta3.setDuration(1000); view.startAnimation(ta3); // 使用布局文件加载特效 //view.startAnimation(AnimationUtils.loadAnimation(getBaseContext(),R.anim.animate_trans)); } });
资源文件:
<?xml version="1.0" encoding="utf-8"?><translate xmlns:android="http://schemas.android.com/apk/res/android" android:fromXDelta="0" android:toXDelta="200" android:fromYDelta="0" android:toYDelta="200" android:duration = "1000"></translate>
4 缩放动画
Button btn_animate4 = (Button)findViewById(R.id.btn_animate4); btn_animate4.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { //左上角向右下角伸缩 ScaleAnimation sa1 = new ScaleAnimation(0,1,0,1); // 在(100,50)位置缩放 ScaleAnimation sa2 = new ScaleAnimation(0,1,0,1,100,50); //自身中心位置缩放 sa4 = new ScaleAnimation(0,1,0,1, Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f); sa4.setDuration(1000); view.startAnimation(sa4); // 使用布局文件加载特效 //view.startAnimation(AnimationUtils.loadAnimation(getBaseContext(),R.anim.animate_scale)); } });
资源文件:
<?xml version="1.0" encoding="utf-8"?><scale xmlns:android="http://schemas.android.com/apk/res/android" android:fromXScale="0" android:toXScale="1" android:fromYScale="0" android:toYScale="1" android:duration = "1000" android:pivotX="50%" android:pivotY="50%" ></scale>
5 混合动画 + 动画监听器
Button btn_animate5 = (Button)findViewById(R.id.btn_animate5); btn_animate5.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { // 动画补间:在动画运行过程中使用动画属性,比如是否加速,匀速等 as = new AnimationSet(true); as.setDuration(1000); as.addAnimation(aa1); //as.addAnimation(ra2); as.addAnimation(ta3); //as.addAnimation(sa4); view.startAnimation(as); // 使用布局文件加载动画 //view.startAnimation(AnimationUtils.loadAnimation(MainActivity.this,R.anim.animate_set)); as.setAnimationListener(new Animation.AnimationListener() { @Override public void onAnimationStart(Animation animation) { } @Override public void onAnimationEnd(Animation animation) { Toast.makeText(MainActivity.this,"animate end",Toast.LENGTH_SHORT).show(); } @Override public void onAnimationRepeat(Animation animation) { } }); } });
资源文件:
<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android" android:shareInterpolator="true" android:duration="1000"> <alpha android:fromAlpha="0" android:toAlpha="1" /> <translate android:fromYDelta ="0" android:toXDelta="200" android:fromXDelta="0" android:toYDelta="200"/></set>
6 自定义动画
Button btn_animate6 = (Button)findViewById(R.id.btn_animate6); btn_animate6.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { CustomAnimate ca = new CustomAnimate(); ca.setDuration(1000); view.startAnimation(ca); } });
package animate.cuanbo.myanimate;import android.util.Log;import android.view.animation.Animation;import android.view.animation.Transformation;/** * Created by Administrator on 2017/10/17. */public class CustomAnimate extends Animation { @Override protected void applyTransformation(float interpolatedTime, Transformation t) { // interpolatedTime 补间时间,动画执行完后为1,interpolatedTime从 0渐变到1 Log.d("Animate",""+interpolatedTime); //渐变透明效果 //t.setAlpha(interpolatedTime); //直接设置到200,200位置 // t.getMatrix().setTranslate(200,200); // 慢慢到达(200,200)位置 //t.getMatrix().setTranslate(200 *interpolatedTime ,200*interpolatedTime); // 左右摆动 t.getMatrix().setTranslate((float)(Math.sin(interpolatedTime *20) * 50),0); super.applyTransformation(interpolatedTime, t); } @Override public void initialize(int width, int height, int parentWidth, int parentHeight) { Log.d("Animate","init"); super.initialize(width, height, parentWidth, parentHeight); }}
5 混合动画 + 动画监听器
Button btn_animate5 = (Button)findViewById(R.id.btn_animate5); btn_animate5.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { // 动画补间:在动画运行过程中使用动画属性,比如是否加速,匀速等 as = new AnimationSet(true); as.setDuration(1000); as.addAnimation(aa1); //as.addAnimation(ra2); as.addAnimation(ta3); //as.addAnimation(sa4); view.startAnimation(as); // 使用布局文件加载动画 //view.startAnimation(AnimationUtils.loadAnimation(MainActivity.this,R.anim.animate_set)); as.setAnimationListener(new Animation.AnimationListener() { @Override public void onAnimationStart(Animation animation) { } @Override public void onAnimationEnd(Animation animation) { Toast.makeText(MainActivity.this,"animate end",Toast.LENGTH_SHORT).show(); } @Override public void onAnimationRepeat(Animation animation) { } }); } });
资源文件:
<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android" android:shareInterpolator="true" android:duration="1000"> <alpha android:fromAlpha="0" android:toAlpha="1" /> <translate android:fromYDelta ="0" android:toXDelta="200" android:fromXDelta="0" android:toYDelta="200"/></set>
6 自定义动画
Button btn_animate6 = (Button)findViewById(R.id.btn_animate6); btn_animate6.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { CustomAnimate ca = new CustomAnimate(); ca.setDuration(1000); view.startAnimation(ca); } });
package animate.cuanbo.myanimate;import android.util.Log;import android.view.animation.Animation;import android.view.animation.Transformation;/** * Created by Administrator on 2017/10/17. */public class CustomAnimate extends Animation { @Override protected void applyTransformation(float interpolatedTime, Transformation t) { // interpolatedTime 补间时间,动画执行完后为1,interpolatedTime从 0渐变到1 Log.d("Animate",""+interpolatedTime); //渐变透明效果 //t.setAlpha(interpolatedTime); //直接设置到200,200位置 // t.getMatrix().setTranslate(200,200); // 慢慢到达(200,200)位置 //t.getMatrix().setTranslate(200 *interpolatedTime ,200*interpolatedTime); // 左右摆动 t.getMatrix().setTranslate((float)(Math.sin(interpolatedTime *20) * 50),0); super.applyTransformation(interpolatedTime, t); } @Override public void initialize(int width, int height, int parentWidth, int parentHeight) { Log.d("Animate","init"); super.initialize(width, height, parentWidth, parentHeight); }}
阅读全文
0 0
- Android开发笔记: android实用6种特效总结
- Android实用小特效
- Android 开发笔记:Android特效 五种Toast详解
- android+手机开发实用总结集锦
- android特效---笔记
- Android开发UI特效
- Android 实用 知识点 总结
- Android Volley 实用总结
- Android开发笔记(九十七)图片的特效处理
- Android开发实用网站!
- Android开发实用网址
- android实用开发工具
- Android开发Toast特效详解
- [置顶]eclipse android实用开发快捷键使用总结
- Android开发最实用的功能代码块总结
- Android学习笔记: Android动画特效
- Android学习笔记: Android动画特效
- Android获得时间总结实用
- C# 委托(Delegate)
- 用模拟退火算法求解TSP问题
- 判断二叉树是否是平衡树
- 01背包简讲(这个表很清晰)
- 素数判断的两种常用办法
- Android开发笔记: android实用6种特效总结
- eclipse导入IDEA java source 1.5中不支持diamond 运算符
- c#实验解答(2)
- include include_lib 的区别
- 【质因数分解 && n = x^p求最大p】LightOJ
- noip 提高备战(低级错误)
- C# 反射(Reflection)
- 使用JVisualVM远程监控Tomcat
- scala(1:函数式编程)