Android-->Toast全屏和动画(模拟QQ样式)

这里写图片描述

如图, 底下是一个空布局, 参考我的博文: http://blog.csdn.net/angcyo/article/details/53967099
顶部就是一个Toast. 全屏,并且进入和退出都有自定义的动画.


正文:
系统并没有提供设置全屏和动画的方法.

但是Java有一个神器, 反射. 我们可以通过反射. 肆意修改成员变量.

//全屏和动画的设置方法
private static void initToast(Toast toast) {
  try {
      Field mTN = toast.getClass().getDeclaredField("mTN");
      mTN.setAccessible(true);
      Object mTNObj = mTN.get(toast);

      Field mParams = mTNObj.getClass().getDeclaredField("mParams");
      mParams.setAccessible(true);
      WindowManager.LayoutParams params = (WindowManager.LayoutParams) mParams.get(mTNObj);
      params.width = -1;//-1表示全屏, 你也可以设置任意宽度.
      params.height = -2;// (int) dpToPx(context, T_HEIGHT);
      params.windowAnimations = R.style.BaseToastAnimation;//设置动画, 需要是style类型
  } catch (Exception e) {
      e.printStackTrace();
  }
}

有了以上方法, 你用该就可以直接使用了.


下面提供一下我的封装:

public static void show(Context context, CharSequence charSequence) {
    View layout;
    if (toast == null) {
        toast = Toast.makeText(context, "", Toast.LENGTH_SHORT);
        initToast(toast);
        layout = LayoutInflater.from(context).inflate(R.layout.base_toast, null);//自定义的布局
        ((TextView) layout.findViewById(R.id.base_toast_text_view)).setText(charSequence);
        toast.setView(layout);
        toast.setGravity(Gravity.TOP, 0, 0);//从顶部开始显示
        toast.getView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);//设置Toast可以布局到系统状态栏的下面
    } else {
        layout = toast.getView();
    }

    TextView titleView = find(layout, R.id.base_toast_text_view);
    ImageView imageView = find(layout, R.id.base_toast_image_view);

    titleView.setText(charSequence);//设置文本信息
    toast.show();
}

是不是很简单?


布局阴影的方法:

在5.0以上的系统中可以通过下面的方法设置:

   android:elevation="10dp"

万能的方法:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- Bottom 2dp Shadow -->
    <item>
        <shape android:shape="rectangle">
            //阴影的渐变颜色
            <gradient
                android:angle="-90"
                android:centerColor="#000"
                android:endColor="#00000000"
                android:startColor="#000"
                android:type="linear"
                />
        </shape>
    </item>

    <!-- White Top color -->
    <item android:bottom="@dimen/base_toast_shadow_height">//阴影的偏移高度
        <shape android:shape="rectangle">
            <solid android:color="#FFFFFF"/>//布局的背景颜色
        </shape>
    </item>
</layer-list>

Toast动画

<!--Toast的动画-->
<style name="BaseToastAnimation">
    <item name="android:windowExitAnimation">@anim/base_tran_top_exit</item>
    <item name="android:windowEnterAnimation">@anim/base_tran_top_enter</item>
</style>

//base_tran_top_exit
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
     android:duration="300"
     android:fillAfter="true"
     android:interpolator="@anim/interpolator_slight_anticipate">

    <translate
        android:fromYDelta="0%"
        android:toYDelta="-100%"/>

</set>

//interpolator_slight_anticipate
<anticipateInterpolator xmlns:android="http://schemas.android.com/apk/res/android"
                        android:tension="1.0"/>


//base_tran_top_enter
<set xmlns:android="http://schemas.android.com/apk/res/android"
     android:duration="300"
     android:interpolator="@anim/interpolator_slight_overshoot">

    <translate
        android:fromYDelta="-100%"
        android:toYDelta="0%"/>

</set>

//interpolator_slight_overshoot
<overshootInterpolator xmlns:android="http://schemas.android.com/apk/res/android"
                       android:tension="1.0"/>

至此: 文章就结束了,如有疑问: QQ群:274306954 欢迎您的加入.

评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值