Android进阶学习-一般动画(使用Animation封装特效工具类2)
2016-04-13 00:00
1006 查看
下面就开始封装一个View显示与隐藏的工具类吧,其实也没什么 跟上一篇文章相比,就是多了一个获取屏幕尺寸的方法.
1.下面就直接上代码了,注释也懒得写啦 哈哈.
2.测试调用,布局文件:
3.测试调用,调用代码:
[code=plain]DisplayMetrics outMetrics = new DisplayMetrics(); wm.getDefaultDisplay().getMetrics(outMetrics); int screenWidth = outMetrics.widthPixels;
1.下面就直接上代码了,注释也懒得写啦 哈哈.
[code=plain]package com.example.august.animator; import android.util.DisplayMetrics; import android.view.View; import android.view.WindowManager; import android.view.animation.AlphaAnimation; import android.view.animation.Animation; import android.view.animation.AnimationSet; import android.view.animation.Interpolator; import android.view.animation.TranslateAnimation; /** * Created by August on 16/4/12. */ public class Utils { public static final int FROM_TOP = 0; public static final int FROM_LEFT = 1; public static final int FROM_RIGHT = 2; public static final int TO_TOP = 0; public static final int TO_LEFT = 1; public static final int TO_RIGHT = 2; public static void showLayout(View v, int direction, int time, Interpolator interpolator, WindowManager wm, int offset) { v.setVisibility(v.VISIBLE); v.setTag(-1, direction); DisplayMetrics outMetrics = new DisplayMetrics(); wm.getDefaultDisplay().getMetrics(outMetrics); int screenWidth = outMetrics.widthPixels; AlphaAnimation alphaAnimation = new AlphaAnimation(0, 1); TranslateAnimation translateAnimation = null; switch (direction) { case FROM_TOP: translateAnimation = new TranslateAnimation(v.getLeft(), v.getLeft(), -v.getHeight(), offset); break; case FROM_LEFT: translateAnimation = new TranslateAnimation(-v.getWidth(), offset, v.getTop(), v.getTop()); break; case FROM_RIGHT: translateAnimation = new TranslateAnimation(screenWidth + v.getWidth(), screenWidth - v.getWidth() - offset, v.getTop(), v.getTop()); break; } AnimationSet set = new AnimationSet(true); set.setDuration(time); set.setInterpolator(interpolator); set.setFillEnabled(true); set.setFillAfter(true); set.addAnimation(alphaAnimation); set.addAnimation(translateAnimation); v.startAnimation(set); } public static void hideLayout(final View v, int time, Interpolator interpolator, WindowManager wm) { int direction = 0; direction = (int) v.getTag(-1); DisplayMetrics outMetrics = new DisplayMetrics(); wm.getDefaultDisplay().getMetrics(outMetrics); int screenWidth = outMetrics.widthPixels; AlphaAnimation alphaAnimation = new AlphaAnimation(1, 0); TranslateAnimation translateAnimation = null; switch (direction) { case TO_TOP: translateAnimation = new TranslateAnimation(v.getLeft(), v.getLeft(), v.getTop(), -v.getHeight()); break; case TO_LEFT: translateAnimation = new TranslateAnimation(v.getLeft(), -v.getWidth(), v.getTop(), v.getTop()); break; case TO_RIGHT: translateAnimation = new TranslateAnimation(v.getLeft(), screenWidth + v.getWidth(), v.getTop(), v.getTop()); break; } AnimationSet set = new AnimationSet(true); set.setDuration(time); set.setInterpolator(interpolator); set.setFillEnabled(true); set.setFillAfter(true); set.setAnimationListener(new Animation.AnimationListener() { @Override public void onAnimationStart(Animation animation) { } @Override public void onAnimationEnd(Animation animation) { v.postInvalidate(0, 0, v.getWidth(), v.getHeight()); } @Override public void onAnimationRepeat(Animation animation) { } }); set.addAnimation(alphaAnimation); set.addAnimation(translateAnimation); v.startAnimation(set); } }
2.测试调用,布局文件:
[code=plain]<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:id="@+id/mLayout" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#2b2b2b" android:orientation="vertical"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:padding="30dp" android:text="This is a test!" android:textColor="#fff" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:orientation="horizontal"> <Button android:id="@+id/showBtn" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="show" /> <Button android:id="@+id/hideBtn" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="hide" /> </LinearLayout> </RelativeLayout>
3.测试调用,调用代码:
[code=plain]package com.example.august.animator; import android.animation.ObjectAnimator; import android.content.Context; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.view.WindowManager; import android.view.animation.AlphaAnimation; import android.view.animation.Animation; import android.view.animation.AnimationSet; import android.view.animation.DecelerateInterpolator; import android.view.animation.Interpolator; import android.view.animation.TranslateAnimation; import android.widget.Button; import android.widget.LinearLayout; import android.widget.Toast; public class MainActivity extends AppCompatActivity implements View.OnClickListener { private Button showBtn; private Button hideBtn; private LinearLayout mLayout; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.content_main); showBtn = (Button) findViewById(R.id.showBtn); hideBtn = (Button) findViewById(R.id.hideBtn); mLayout = (LinearLayout) findViewById(R.id.mLayout); showBtn.setOnClickListener(this); hideBtn.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.showBtn: Utils.showLayout(mLayout, Utils.FROM_RIGHT, 300, new DecelerateInterpolator(), (WindowManager) getSystemService(Context.WINDOW_SERVICE), 0); break; case R.id.hideBtn: Utils.hideLayout(mLayout, 300, new DecelerateInterpolator(), (WindowManager) getSystemService(Context.WINDOW_SERVICE)); break; } } }
相关文章推荐
- Android开发之图形图像与动画(二)Animation实现图像的渐变/缩放/位移/旋转
- Android开发重写Animation实现下拉图片后弹射回去效果示例
- Android编程之Animation动画详解
- Android Animation实战之屏幕底部弹出PopupWindow
- Android Animation实战之一个APP的ListView的动画效果
- How to delete all the animations in a presentation
- Transform/Transition/Animation的区别与联系
- HTML前端开发之路——Animation
- Android 应用的动画实践--View Animation篇
- angular.animation的使用
- IOS中常用动画方法总结
- Qt-4.6动画Animation快速入门三字决
- 研究一下Android中的动画效果
- 自定义dialog--symbian
- 乔布斯在2005年斯坦福大学毕业典礼上的演讲:我生命中的三个故事
- Android 移动动画- TranslateAnimation
- 通天塔
- (译)Cocos2d_for_iPhone_1_Game_Development_Cookbook:1.12使用层来交换调色板
- Blender 3D渲染工具