zoukankan      html  css  js  c++  java
  • Android自定义组件系列【17】——教你如何高仿微信录音Toast

    一、Toast介绍

    平时我们在Android开发中会经常用到一个叫Toast的东西,官方解释如下

    A toast is a view containing a quick little message for the user. The toast class helps you create and show those.
    When the view is shown to the user, appears as a floating view over the application. It will never receive focus. The user will probably be in the middle of typing something else. The idea is to be as unobtrusive as possible, while still showing the user the information you want them to see. Two examples are the volume control, and the brief message saying that your settings have been saved.

    Toast最基本的用法很简单,不用说大家都会(这里切记要调用show()去显示)

    public static void showToast(Context context){
        Toast.makeText(context, "欢迎关注水寒的CSDN博客", Toast.LENGTH_LONG).show();
    }

    这里写图片描述

    二、自定义Toast

    上面的Toast一般显示在手机偏下的一个位置上,有的时候我们需要将这个Toast的位置或者内容进行修改,比如让显示在屏幕中间,让内容里面有图片,这样的修改也比较容易,Toast有一个和ActionBar类似的方法setView(),这个方法就是提供给我们自定义Toast的。

    这里写图片描述

    public static void showToast(Context context){
        LayoutInflater inflater = LayoutInflater.from(context);
        View toastView = inflater.inflate(R.layout.toast_test_custome, null);
        Toast toast = new Toast(context); 
        toast.setDuration(3000);
        toast.setView(toastView);                //设置自定义view
        toast.setGravity(Gravity.CENTER, 0, 0);  //控制显示到屏幕中间
        toast.show();                            //注意:一定要调用才能显示
    }

    布局文件如下

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/custom_toast_bg"
        android:padding="15dip"
        android:gravity="center_horizontal"
        android:orientation="vertical" >
    
        <ImageView 
            android:layout_width="80dip"
            android:layout_height="80dip"
            android:scaleType="centerCrop"
            android:src="@drawable/shuihan"/>
    
        <TextView 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="12dip"
            android:textSize="16sp"
            android:text="欢迎访问水寒的CSDN博客"
            android:textColor="#ffffff"/>
    </LinearLayout>

    三、为什么要改变Toast的显示时长

    上面的自定义非常简单,但是我们要改变Toast的显示时间就比较麻烦了,因为toast的setDuration方法只能填写两个值2000ms或者3000ms
    这里写图片描述
    不信你可以给setDuration设置一个10000它最长只显示3s,网上有各种延长Toast的方法,基本上都是通过定时器来延长显示的。
    那么我们接下来就要思考一个问题了,我们为什么要延迟Toast的时间?为什么不用Dialog或者PopupWindow来替代?
    其实原因很简单,Toast显示是不获取焦点的,所以Toast一般是用来提示用户的,而不影响用户的操作。我们可以用一个很简单的例子证明一下:

    findViewById(R.id.test_toast_button).setOnTouchListener(new OnTouchListener() {
        @Override
        public boolean onTouch(View view, MotionEvent event) {
            switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                //showToast();
                showDialog();
                Log.d("shuihan", "----down----");
                break;
            case MotionEvent.ACTION_MOVE:
                Log.d("shuihan", "----move----");
                break;
            case MotionEvent.ACTION_UP:
                Log.d("shuihan", "----up----");
                break;
            case MotionEvent.ACTION_CANCEL:
                Log.d("shuihan", "----cancel----");
                break;
            default:
                break;
            }
            return true;
        }
    });

    我们分别去在ACTION_DOWN的时候去显示一下toast和dialog你再对照输出日志看一下,就会明白。
    我们在微信里面录音的时候会有一个Toast大家应该都见过,如下:
    这里写图片描述
    这个Toast的一个很重要的特点就是只要你按着不放它就不消失,也就是说它的显示时间可以长于3s

    四、实现显示时间长于3s的Toast

    (本文出自水寒的CSDN博客:http://blog.csdn.net/dawanganban
    我们采用的方式也是使用定时器来实现重复显示一个toast从而延长它的时间,我们先来看一个androd里面的CountDownTimer的用法

     new CountDownTimer(30000, 1000) {
    
         public void onTick(long millisUntilFinished) {
             mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
         }
    
         public void onFinish() {
             mTextField.setText("done!");
         }
      }.start();

    这是官方文档上的一个例子,一个30s的倒计时,每隔1s调用一次onTick方法,该方法返回一个倒计时剩余时间,ok,接下来我们来看看toast怎么让他显示30s

        /**
         * 显示Toast
         * @param toast
         * @param duration
         */
        public static void showToast(final Toast toast, long duration, final OnToastStatus toastStatu) {
            final long tickTime = 200;
            mCountDownTimer = new CountDownTimer(duration, tickTime) {
                @Override
                public void onTick(long millisUntilFinished) {
                    if(millisUntilFinished <= 200){
                        showToast("最长可录制一分钟");
                    }else{
                        toast.show();
                    }
                }
    
                @Override
                public void onFinish() {
                    toast.cancel();
                    if(toastStatu != null){
                        toastStatu.toastStop();
                    }
                }
            }.start();
        }
    
        public interface OnToastStatus{
            public void toastStop();
        }
    
        /**
         * 停止显示Toast
         */
        public static void stopToast(Toast toast){
            if(toast != null){
                toast.cancel();
            }
            if(mCountDownTimer != null){
                mCountDownTimer.cancel();
            }
        }
  • 相关阅读:
    innotop 安装和使用
    Waiting for table flush 的原因及处理方法
    input 在 chrome 下 , 自动填充后,默认样式清除
    小米WiFi放大器出现黄灯闪烁,无法使用处理方法
    Error writing file '/tmp/MLLGyECY' (Errcode: 28
    org.apache.activemq.transport.InactivityIOException: Channel was inactive for too (>30000) long: tcp://192.168.120.22:61616
    Apache-Tomcat-Ajp漏洞(CVE-2020-1938)漏洞复现 和处理
    Spring boot CommandLineRunner接口使用例子
    interface21
    interface21
  • 原文地址:https://www.cnblogs.com/lanzhi/p/6468482.html
Copyright © 2011-2022 走看看