您的位置:首页 > 移动开发 > Android开发

Android进阶学习-一般动画(使用Animation封装特效工具类2)

2016-04-13 00:00 1006 查看
下面就开始封装一个View显示与隐藏的工具类吧,其实也没什么 跟上一篇文章相比,就是多了一个获取屏幕尺寸的方法.

[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;
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息