• Android打印机效果的闪屏


    效果图片

    class

    创建一个PrinterTextView

    package com.lyq.stein;
    //我的包的名字,这里要改成自己的包名
    
    
    import android.content.Context;
    import android.util.AttributeSet;
    import android.widget.TextView;
    
    import java.util.Timer;
    import java.util.TimerTask;
    
    /**
     * Created by junweiliu on 16/12/29.
     */
    public class PrinterTextView extends TextView {
        /**
         * TAG
         */
        private static final String TAG = "PrinterTextView";
        /**
         * 默认打字字符
         */
        private final String DEFAULT_INTERVAL_CHAR = "_";
        /**
         * 默认打字间隔时间
         */
        private final int DEFAULT_TIME_DELAY = 80;
        /**
         * 计时器
         */
        private Timer mTimer;
        /**
         * 需要打字的文字
         */
        private String mPrintStr;
        /**
         * 间隔时间
         */
        private int intervalTime = DEFAULT_TIME_DELAY;
        /**
         * 间隔时间
         */
        private String intervalChar = DEFAULT_INTERVAL_CHAR;
        /**
         * 打字进度
         */
        private int printProgress = 0;
    
    
        public PrinterTextView(Context context) {
            super(context);
        }
    
        public PrinterTextView(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
    
        public PrinterTextView(Context context, AttributeSet attrs, int defStyleAttr) {
            super(context, attrs, defStyleAttr);
        }
    
    
        /**
         * 设置要打字的文字
         *
         * @param str
         */
        public void setPrintText(String str) {
            setPrintText(str, DEFAULT_TIME_DELAY);
        }
    
        /**
         * 设置需要打字的文字及打字间隔
         *
         * @param str  打字文字
         * @param time 打字间隔(ms)
         */
        public void setPrintText(String str, int time) {
            setPrintText(str, time, DEFAULT_INTERVAL_CHAR);
        }
    
        /**
         * 设置需要打字的文字,打字间隔,间隔符号
         *
         * @param str          打字文字
         * @param time         打字间隔(ms)
         * @param intervalChar 间隔符号("_")
         */
        public void setPrintText(String str, int time, String intervalChar) {
            if (strIsEmpty(str) || 0 == time || strIsEmpty(intervalChar)) {
                return;
            }
            this.mPrintStr = str;
            this.intervalTime = time;
            this.intervalChar = intervalChar;
        }
    
        /**
         * 开始打字
         */
        public void startPrint() {
            // 判空处理
            if (strIsEmpty(mPrintStr)) {
                if (!strIsEmpty(getText().toString())) {
                    this.mPrintStr = getText().toString();
                } else {
                    return;
                }
            }
            // 重置相关信息
            setText("");
            stopPrint();
            printProgress = 0;
            mTimer = new Timer();
            mTimer.schedule(new PrinterTimeTask(), intervalTime, intervalTime);
        }
    
        /**
         * 停止打字
         */
        public void stopPrint() {
            if (null != mTimer) {
                mTimer.cancel();
                mTimer = null;
            }
        }
    
        /**
         * 判断str是否为空
         *
         * @param str
         * @return
         */
        private boolean strIsEmpty(String str) {
            if (null != str && !"".equals(str)) {
                return false;
            } else {
                return true;
            }
        }
    
        /**
         * 打字计时器任务
         */
        class PrinterTimeTask extends TimerTask {
    
            @Override
            public void run() {
                // 需要刷新页面,必须在UI线程,使用post方法
                post(new Runnable() {
                    @Override
                    public void run() {
                        // 如果未显示完,继续显示
                        if (printProgress < mPrintStr.length()) {
                            printProgress++;
                            // (printProgress & 1) == 1 等价于printProgress%2!=0
                            setText(mPrintStr.substring(0, printProgress) + ((printProgress & 1) == 1 ? intervalChar : ""));
                        } else {
                            // 如果完成打字,显示完整文字
                            setText(mPrintStr);
                            stopPrint();
                        }
                    }
                });
            }
        }
    }
    
    
    

    java

    创建一个MainActivity

    package com.lyq.stein;//包名
    
    import android.content.Intent;
    import android.os.Bundle;
    import android.os.Handler;
    import android.support.v7.app.AppCompatActivity;
    
    import com.lyq.stein.R;//这是我的包名的地址
    
    public class MainActivity extends AppCompatActivity {
    
    
        private PrinterTextView mPrinterTextView;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            initView();
        }
    
        private void initView(){
    
            mPrinterTextView = (PrinterTextView) findViewById(R.id.pt_my);
    
            try{
                startPrint();
            }catch (Exception e){
                finish();
            }
    
        }
    
        /**
         * 开始打印
         *
         * @param view
         */
        public void startPrint() {
            mPrinterTextView.setPrintText("This is Stein
    Welcome to my blog", 100, "|");
            mPrinterTextView.startPrint();
        }
    }
    
    

    修改setPrintText里的文字即可改变打印机的文字,100是打字的速度,没100ms打印1个字符

    xml

    在activity_main里配置

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout 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"
        tools:context=".SplashActivity"
        android:gravity="center"
        android:orientation="vertical">
    
        <!-- 此处是clss的位置 -->
        <com.lyq.stein.PrinterTextView
            android:id="@+id/pt_my"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:paddingLeft="16dp"
            android:paddingTop="16dp"
            android:paddingRight="16dp"
            android:textColor="@color/cyan"
            android:textSize="30sp" />
    
    </LinearLayout>
    
    

    在colors里配置

    <color name="cyan">#00FFFF</color> <!--青色 -->
    

    最后,在manifests里加入

    <activity android:name=".MainActivity"></activity>
    
  • 相关阅读:
    设置display:flex后 flex布局设置单个子元素靠右
    wordpress独立站菜单导航设置教程
    javascript对象数组内元素排序
    好用的在线客服系统Go语言源码GOFLY ( 开源代码+安装教程)
    解决golang报错:imports github.com/gosqldriver/mysql from implicitly required module;
    多语言在线客服系统源码自动识别中英环境私有化部署完美支持跨境电商网站
    网页在线客服代码侧边悬浮在线客服/QQ/微信/电话代码
    在线客服系统源码开发实战总结:H5 Notifications浏览器桌面通知
    在线客服系统源码开发实战总结:gin框架模板渲染html页面
    wordpress网站主题安装教程
  • 原文地址:https://www.cnblogs.com/Emcikem/p/11342139.html
Copyright © 2020-2023  润新知