自定义Toast样式,友好用户体验。

    这次在github上看见一个不错的库:github地址  :

              https://github.com/yadav-rahul/TastyToast  非常感谢该库作者
  看效果:

我们来看看布局:

       

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/root_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#00000000"
    android:orientation="vertical"
    >
    <LinearLayout
        android:id="@+id/base_layout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="25dp"
        android:layout_marginLeft="30dp"
        android:layout_marginRight="30dp"
        android:layout_marginTop="25dp"
        android:background="@drawable/background_toast"
        android:orientation="horizontal">
        <LinearLayout
            android:id="@+id/errorViewLayout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center">
            <com.sdsmdg.tastytoast.ErrorToastView
                android:id="@+id/errorView"
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:layout_gravity="center_vertical|left"
                android:layout_margin="10px"
                android:gravity="center_vertical|left" />
        </LinearLayout>
        <TextView
            android:id="@+id/toastMessage"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center_vertical"
            android:padding="10dp"
            android:text="New Text" />
    </LinearLayout>
</LinearLayout>
有一个自定义View:
com.sdsmdg.tastytoast.ErrorToastView
再进入细看 (本次只拆解ErrorToastView类,相关其他自定义类请自行下载该库了解) 地址:
 https://github.com/yadav-rahul/TastyToast 

package com.sdsmdg.tastytoast;

import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.LinearInterpolator;

/**
 * Created by rahul on 22/7/16.
 */
public class ErrorToastView extends View {
    RectF rectF = new RectF();
    RectF leftEyeRectF = new RectF();
    RectF rightEyeRectF = new RectF();
    ValueAnimator valueAnimator;
    float mAnimatedValue = 0f;
    private Paint mPaint;
    private float mWidth = 0f;
    private float mEyeWidth = 0f;
    private float mPadding = 0f;
    private float endAngle = 0f;
    private boolean isJustVisible = false;
    private boolean isSad = false;

    public ErrorToastView(Context context) {
        super(context);
    }


    public ErrorToastView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public ErrorToastView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initPaint();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        mWidth = getMeasuredWidth();//获取视图的实际宽度
        mPadding = dip2px(10);
        mEyeWidth = dip2px(3);
    }

    private void initPaint() {
        mPaint = new Paint();
        mPaint.setAntiAlias(true);//抗锯齿
        mPaint.setStyle(Paint.Style.STROKE);//画笔的样式(镂空)
        mPaint.setColor(Color.parseColor("#d9534f"));//画笔的颜色
        mPaint.setStrokeWidth(dip2px(2));//画笔的宽度

    }

  public int dip2px(float dpValue) {
    final float scale = getContext().getResources().getDisplayMetrics().density;
    return (int) (dpValue * scale + 0.5f);
  }

以上是前半部分 没什么好说的 重点看以下部分:


最后我们再看属性动画:

 public void startAnim() {
      stopAnim();
//启动动画,
      startViewAnim(0f, 1f, 2000);
  }

  public void stopAnim() {
//如果动画不为空
      if (valueAnimator != null) {
   //清除动画
          clearAnimation();
          isSad = false;
          endAngle = 0f;//设置弧度的角度为0          isJustVisible = false;//设置为实心圆
          mAnimatedValue = 0f;//设置动画的进度值为0
   //结束动画
          valueAnimator.end();
      }
  }

  private ValueAnimator startViewAnim(float startF, final float endF, long time) {
      valueAnimator = ValueAnimator.ofFloat(startF, endF);//属性动画
      valueAnimator.setDuration(time);//持续时间
      valueAnimator.setInterpolator(new LinearInterpolator());//篡改器
      valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
   //动画监听
          @Override
          public void onAnimationUpdate(ValueAnimator valueAnimator) {
              mAnimatedValue = (float) valueAnimator.getAnimatedValue();//获取动属性画状态改变的值
              if (mAnimatedValue < 0.5) {/*这里不绘制两个小眼睛*/
                  isSad = false;
                  isJustVisible = false;
                  endAngle = 240 * (mAnimatedValue);//动态改变弧度的角度
                  isJustVisible = true;//动画启动就画两个小眼睛
              } else if (mAnimatedValue > 0.55 && mAnimatedValue < 0.7) {
                  endAngle = 120;//弧度的角度为120
                  isSad = false;
                  isJustVisible = true;//显示实心圆
              } else {/*绘制椭圆眼睛*/
                  endAngle = 120;
isSad = true; isJustVisible = false; } postInvalidate();//强制重绘 } }); //如果 动画没有启动运行就启动 if (!valueAnimator.isRunning()) { valueAnimator.start(); } return valueAnimator; }
最后调用:

Toast toast = new Toast(context);
View layout = LayoutInflater.from(context).inflate(R.layout.error_toast_layout, null, false);
TextView text = (TextView) layout.findViewById(R.id.toastMessage);
text.setText(msg);//装配数据
errorToastView = (ErrorToastView) layout.findViewById(R.id.errorView);
errorToastView.startAnim();//启动动画
text.setBackgroundResource(R.drawable.error_toast);//设置文本背景样式
text.setTextColor(Color.parseColor("#FFFFFF"));//设置字体颜色
toast.setView(layout);//设置toastView
toast.setDuration(length);//显示时长
toast.show();//显示


  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值