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

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

DisplayMetrics outMetrics = new DisplayMetrics();
wm.getDefaultDisplay().getMetrics(outMetrics);
int screenWidth = outMetrics.widthPixels;

 

1.下面就直接上代码了,注释也懒得写啦 哈哈.

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.测试调用,布局文件:

<?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.测试调用,调用代码:

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;
        }
    }
}

 

 

转载于:https://my.oschina.net/august1996/blog/657811

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值