自定义时间Toast(只弹一次)

2023-11-01

CToast类:

package com.pinkman.dota.util;


import com.pinkman.dota.R;

import android.content.Context;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.RoundRectShape;
import android.os.Handler;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class CToast {
	public static final int LENGTH_SHORT = 2000;
	public static final int LENGTH_LONG = 3500;
	private final Handler mHandler = new Handler();
	private int mDuration = LENGTH_SHORT;
	private int mGravity = Gravity.BOTTOM;
	private int mX, mY;
	private float mHorizontalMargin;
	private float mVerticalMargin;
	private View mView;
	private View mNextView;
	private WindowManager mWM;
	private final WindowManager.LayoutParams mParams = new WindowManager.LayoutParams();
	public static CToast mToast;
	//show only one Toast
	public static void showSingleToast(Context context,View layout){
		if (mToast != null) {
			return ;
		}
		mToast = new CToast(context);
		mToast.setView(layout);
		mToast.show();
	}
	public static CToast makeToast(Context context, int resid, int text_id, int durationMills) {
		return makeToast(context,resid,context.getString(text_id),durationMills);
	}
	public static CToast makeToast(Context context, int resid, CharSequence text, int durationMills) {
		if (mToast != null) {
			mToast.destroy();
		}
		mToast = new CToast(context);
		// 外部矩形弧度
		float[] outerR = new float[] { DimenUtils.dip2px(context, 10), DimenUtils.dip2px(context, 10), DimenUtils.dip2px(context, 10),
				DimenUtils.dip2px(context, 10), DimenUtils.dip2px(context, 10), DimenUtils.dip2px(context, 10), DimenUtils.dip2px(context, 10), DimenUtils.dip2px(context, 10) };
		RoundRectShape s = new RoundRectShape(outerR, null, null);
		ShapeDrawable shapedrawble = new ShapeDrawable(s);
		shapedrawble.getPaint().setColor(Color.parseColor("#56000000"));
		shapedrawble.getPaint().setStyle(Paint.Style.FILL);
		LinearLayout mLayout = new LinearLayout(context);
		mLayout.setBackground(shapedrawble);
		mLayout.setPadding(DimenUtils.dip2px(context, 10), 0, DimenUtils.dip2px(context, 10), 0);
		FrameLayout frameLayout = new FrameLayout(context);
		FrameLayout.LayoutParams lp_image = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT,
				FrameLayout.LayoutParams.WRAP_CONTENT);
		lp_image.gravity = Gravity.CENTER;
		ImageView circle = new ImageView(context);
		circle.setImageResource(R.drawable.circle);
		RotateAnimation animation = new RotateAnimation(0f, 360f, Animation.RELATIVE_TO_SELF, 0.5f,
				Animation.RELATIVE_TO_SELF, 0.5f);
		animation.setDuration(1000);
		animation.setRepeatCount(Animation.INFINITE);
		animation.setInterpolator(new LinearInterpolator());
		circle.startAnimation(animation);
		frameLayout.addView(circle, lp_image);
		ImageView image = new ImageView(context);
		image.setImageResource(resid);
		frameLayout.addView(image, lp_image);
		if (resid < 1) {
			frameLayout.setVisibility(View.GONE);
		}
		mLayout.addView(frameLayout);
		TextView tv = new TextView(context);
		tv.setTextColor(Color.parseColor("#fdfdfd"));
		tv.setTextSize(DimenUtils.sp2px(context, 25));
		tv.setText(text);
		tv.setGravity(Gravity.CENTER);
		LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, DimenUtils.sp2px(
				context, 65));
		mLayout.addView(tv, lp);
		mToast.mNextView = mLayout;
		mToast.mDuration = durationMills;
		return mToast;
	}
	public CToast(Context context) {
		init(context);
	}
	/**
	 * Set the view to show.
	 * @see #getView
	 */
	public void setView(View view) {
		mNextView = view;
	}
	/**
	 * Return the view.
	 * @see #setView
	 */
	public View getView() {
		return mNextView;
	}
	/**
	 * Set how long to show the view for.
	 * @see #LENGTH_SHORT
	 * @see #LENGTH_LONG
	 */
	public void setDuration(int duration) {
		mDuration = duration;
	}
	/**
	 * Return the duration.
	 * @see #setDuration
	 */
	public int getDuration() {
		return mDuration;
	}
	/**
	 * Set the margins of the view.
	 * @param horizontalMargin The horizontal margin, in percentage of the container width, between
	 *            the container's edges and the notification
	 * @param verticalMargin The vertical margin, in percentage of the container height, between the
	 *            container's edges and the notification
	 */
	public void setMargin(float horizontalMargin, float verticalMargin) {
		mHorizontalMargin = horizontalMargin;
		mVerticalMargin = verticalMargin;
	}
	/**
	 * Return the horizontal margin.
	 */
	public float getHorizontalMargin() {
		return mHorizontalMargin;
	}
	/**
	 * Return the vertical margin.
	 */
	public float getVerticalMargin() {
		return mVerticalMargin;
	}
	/**
	 * Set the location at which the notification should appear on the screen.
	 * @see android.view.Gravity
	 * @see #getGravity
	 */
	public void setGravity(int gravity, int xOffset, int yOffset) {
		mGravity = gravity;
		mX = xOffset;
		mY = yOffset;
	}
	/**
	 * Get the location at which the notification should appear on the screen.
	 * @see android.view.Gravity
	 * @see #getGravity
	 */
	public int getGravity() {
		return mGravity;
	}
	/**
	 * Return the X offset in pixels to apply to the gravity's location.
	 */
	public int getXOffset() {
		return mX;
	}
	/**
	 * Return the Y offset in pixels to apply to the gravity's location.
	 */
	public int getYOffset() {
		return mY;
	}
	/**
	 * schedule handleShow into the right thread
	 */
	public void show() {
		mHandler.post(mShow);
		if (mDuration > 0) {
			mHandler.postDelayed(mHide, mDuration);
		}
	}
	/**
	 * schedule handleHide into the right thread
	 */
	public void hide() {
		mHandler.post(mHide);
	}

	private final Runnable mShow = new Runnable() {
		public void run() {
			handleShow();
		}
	};
	private final Runnable mHide = new Runnable() {
		public void run() {
			handleHide();
		}
	};

	private void init(Context context) {
		final WindowManager.LayoutParams params = mParams;
		params.height = WindowManager.LayoutParams.WRAP_CONTENT;
		params.width = WindowManager.LayoutParams.WRAP_CONTENT;
		params.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE
				| WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
		params.format = PixelFormat.TRANSLUCENT;
		params.windowAnimations = android.R.style.Animation_Toast;
		params.type = WindowManager.LayoutParams.TYPE_TOAST;
		params.setTitle("Toast");
		mWM = (WindowManager) context.getApplicationContext().getSystemService(Context.WINDOW_SERVICE);
	}
	private void handleShow() {
		if (mView != mNextView) {
			// remove the old view if necessary
			handleHide();
			mView = mNextView;
			// mWM = WindowManagerImpl.getDefault();
			final int gravity = mGravity;
			mParams.gravity = gravity;
			if ((gravity & Gravity.HORIZONTAL_GRAVITY_MASK) == Gravity.FILL_HORIZONTAL) {
				mParams.horizontalWeight = 1.0f;
			}
			if ((gravity & Gravity.VERTICAL_GRAVITY_MASK) == Gravity.FILL_VERTICAL) {
				mParams.verticalWeight = 1.0f;
			}
			mParams.x = mX;
			mParams.y = mY;
			mParams.verticalMargin = mVerticalMargin;
			mParams.horizontalMargin = mHorizontalMargin;
			if (mView.getParent() != null) {
				mWM.removeView(mView);
			}
			mWM.addView(mView, mParams);
		}
	}
	private void handleHide() {
		if (mView != null) {
			if (mView.getParent() != null) {
				mWM.removeView(mView);
			}
			mView = null;
			mToast = null;
		}
	}
	private void destroy() {
		mHandler.removeCallbacks(mHide);
		mHandler.removeCallbacks(mShow);
		handleHide();
	}
}

DimenUtils:

package com.pinkman.dota.util;

import android.content.Context;
import android.graphics.Point;
import android.util.DisplayMetrics;

/**
 * @author pinkman 尺寸转换及获取屏幕宽高工具类
 */
public class DimenUtils {
	public static int sp2px(Context context, float spValue) {
		float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
		return (int) (spValue * fontScale + 0.5f);
	}
	public static int px2sp(Context context, float pxValue) {
		float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
		return (int) (pxValue / fontScale + 0.5f);
	}
	public static int dip2px(Context context, int dipValue) {
		final float scale = context.getResources().getDisplayMetrics().density;
		return (int) (dipValue * scale + 0.5f);
	}
	public static int px2dip(Context context, float pxValue) {
		final float scale = context.getResources().getDisplayMetrics().density;
		return (int) (pxValue / scale + 0.5f);
	}
	/**
	 * 获取屏幕宽度和高度,单位为px
	 * @param context
	 * @return
	 */
	public static Point getScreenMetrics(Context context) {
		DisplayMetrics dm = context.getResources().getDisplayMetrics();
		int w_screen = dm.widthPixels;
		int h_screen = dm.heightPixels;
		return new Point(w_screen, h_screen);
	}
	/**
	 * 获取屏幕长宽比
	 * @param context
	 * @return
	 */
	public static float getScreenRate(Context context) {
		Point P = getScreenMetrics(context);
		float H = P.y;
		float W = P.x;
		return (H / W);
	}
}













本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

自定义时间Toast(只弹一次) 的相关文章

  • PhoneGap/Cordova 应用程序通知

    我是 PhoneGap Cordova 的新手 我希望向我的应用程序添加一些通知 推送通知 因此当应用程序上发布新文章时 它会提醒用户 本地通知 在设定的时间间隔 日期和时间 我可以提示用户我的应用程序上的最新文章 我进行了大量搜索 但找不
  • Android:初始化本机 AudioRecord 对象时 AudioRecord 错误代码 -20

    Android 我想从麦克风读取缓冲区 以便我可以对其执行处理 以下是我的代码 int sampleRateInHz 8000 44100 22050 and 11025 int channelConfig AudioFormat CHAN
  • 当满足条件时,如何以编程方式更改 ImageButton src 目标?

    我有一个学校项目 我正在尝试开发一个手电筒应用程序 对于开 关 ImageButton 我想要 4 个自定义图像 如果手电筒关闭 turn on png 默认 turn on pressing png 按下状态 true 如果手电筒打开 t
  • 带有 Android 支持库 v7 的 Maven Android 插件

    我使用 maven android plugin 构建我的 android 应用程序 它依赖于 android 支持库 v4 和 v7 由于我没有找到如何从developer android com下载整个sdk 因此我无法使用maven
  • 如何通过我的活动在 Android 中设置铃声?

    我正在尝试找到一种方法来通过 Android 活动中的代码设置新的默认铃声 我已经将铃声下载到bytearray 最后 我设法将默认铃声设置为我下载的铃声 下面不包含下载代码 仅包含将其设置为默认铃声所需的代码 File k new Fil
  • 改造中的多个队列导致内存不足错误?

    我正在使用retrofit2 做我的项目 当我的呼叫失败时 我再次重复相同的呼叫 重复此 呼叫使我的应用程序强制关闭 当我查看日志时 我得到了错误日志 如下所示 我觉得这是由于同一呼叫的多次排队造成的 所以我在排队之前就这样做了 我打电话给
  • ADB TCPIP 连接问题

    我有两台 Galaxy S3 其中一个已扎根 另一个则未扎根 因此 当我尝试通过本地网络连接它们时 计算机可以看到已root的计算机 但是正常的就卡在tcpip这一步了 所以 我写 adb tcpip 5555 It says restar
  • 需要 Android webview window.open() 和 window.close() 的信息

    我正在开发一个安卓应用程序 这是我网站的 WebView 该网站包含一个弹出按钮 单击该按钮后 将打开一个新窗口并显示内容 该链接可以来自外部站点 然而 当我实现此操作时 新选项卡正在打开 之后它会弹出以打开浏览器 尽管在 Web 视图中打
  • React Native Expo StackNavigator 重叠通知栏

    我正在尝试为我的 React Native Expo 应用程序实现导航栏 这里有一个问题 dependencies expo 18 0 3 react 16 0 0 alpha 12 react native 0 45 1 react na
  • Android -room 持久库 - DAO 调用是异步的,因此如何获取回调?

    从我读到的Room 不允许您在主线程上发出数据库查询 因为可能会导致主线程延迟 所以想象一下我正在尝试更新 UI 主线程上的文本视图 其中一些数据我将如何得到回调 让我给你举个例子 想象一下 我想将我的业务模型数据存储到一个名为 事件 的对
  • 在新的 intel x86 android 模拟器中访问 google api

    我只是尝试在新的 x86 android 模拟器中运行我公司的应用程序 但是我们的应用程序依赖于 google 地图 API 而这在 google 随 android sdk 版本 17 提供的 x86 系统映像中不可用 我的直觉告诉我答案
  • 是否可以通过 Android 应用程序来录音?

    我是一名开发人员 希望创建一个 Android 应用程序来记录电话 这是出于我个人的需要 为了我自己的目的和记录而记录电话 是否有可能做到这一点 是否可以访问麦克风以及通过扬声器发出的声音 我对 Android 开发有点陌生 所以请耐心等待
  • 在android中,将相机预览流到视图上

    我想将 Android 相机的相机预览流式传输到视图上 目的是随后使用 onDraw 将各种内容添加到视图中 我不需要随时实际捕捉图像 它不必是最高质量或每秒最大数量的帧 有谁知道如何做到这一点 将其添加到您的 xml 中
  • 安卓。 CalendarView...一次仅显示一个月的日历

    我正在使用 CalendarView 其中我想一次仅查看一个月的日历并滚动查看下个月 但 CalendarView 一次显示所有月份 下面是我的代码
  • Android 中的库可以有自己的意图过滤器吗?

    我想开发一个可以包含在其他 Android 应用程序中的库来拦截某些类型的意图 是否可以 我创建了一个库和一个测试项目 两者都有自己的AndroidManifest xml文件 在库的清单中 我为操作 TEST 定义了一个意图过滤器 但是
  • 如何更改操作栏背景和文本颜色

    我正在使用本教程中的导航抽屉 http www androidhive info 2013 11 android sliding menu using navigation drawer http www androidhive info
  • Android-dispatchTouchEvent 给了我一个 StackOverflowError

    这里我有一个带有 setOnTouchListener 的 ViewFlipper 它工作得很好 然后我膨胀 ReLayNewsItem 然后将其添加到 ViewFlipper 现在我希望 WebView web 监听触摸事件并将它们传递给
  • 检查应用程序是否在 Android Market 上可用

    给定 Android 应用程序 ID 包名称 如何以编程方式检查该应用程序是否在 Android Market 上可用 例如 com rovio angrybirds 可用 而 com random app ibuilt 不可用 我计划从
  • 将对象从手机共享到 Android Wear

    我创建了一个应用程序 在此应用程序中 您拥有包含 2 个字符串 姓名和年龄 和一个位图 头像 的对象 所有内容都保存到 sqlite 数据库中 现在我希望可以在我的智能手表上访问这些对象 所以我想实现的是你可以去启动 启动应用程序并向左和向
  • Android GetPTLAFormat 上的 Phonegap 错误

    我们正在开发一个使用 jQuery 移动和电话间隙的应用程序 一切似乎都工作正常 但是当在连接的 Android 手机上运行应用程序时 我们在 Eclipse logcat 中看到大量类似这样的错误 0 GetPTLAFormat inva

随机推荐

  • 蓝桥杯2022真题:裁纸刀、修剪灌木、刷题统计、纸张尺寸、数位排序、考勤刷卡、卡片、小平方、李白打酒加强版

    目录 1 裁纸刀 2 修剪灌木 3 刷题统计 4 纸张尺寸 5 数位排序 6 考勤刷卡 7 卡片 8 小平方 9 李白打酒加强版 1 裁纸刀 题目无法截图 看题点击 https www lanqiao cn problems sort st
  • 机器学习:利用神经网络实现简单的数字识别

    本学期的机器学习课程 作业参考了吴恩达大佬的手写数字识别实验 以下是完成的代码 import numpy as np import scipy io as sio from scipy optimize import fmin cg imp
  • 剖析:顺序表的增删改查

    目录 前言 一 什么是顺序表 1 1顺序表的两种形式 1 2动态顺序表 二 顺序表增删改查的接口实现 2 1结构体空间表示顺序表 2 2顺序表的初始化 2 2malloc新增空间 2 3增加元素 2 4删除元素 2 5查找元素 2 6指定位
  • 代码质量如何管控与提升

    开发团队代码质量如何做到管控与提升 我相信很多公司都会面临这样的问题 开发团队大人员技术水平参差不齐 代码写的不够规范 代码扫描问题修改太过滞后 代码库管理每个团队都不一致 偶尔还会合并丢失一些代码 code review费人费时效率不高
  • 前端必用正则(js)不间断更新```

    手机号 1 3 d 4 5 6 9 5 0 3 5 9 6 5 7 7 0 8 8 1 3 5 8 9 1 8 9 d 8 大写字母 A Z 日期 如 2019 07 10 d 4 d 1 2 1 d 1 2 email地址 w w w w
  • 【剑指offer-第二版】部分题目与解答【C++版本】

    20180612 求职在即 剑指offer 作为大家都推荐的一本应试宝典 确实也有刷一刷的必要 很多题目都比较经典 也涵盖了大多数的算法和数据结构 把自己刷题的过程做一个总结 权当是一个笔记 当前还处在未完成状态 希望自己能坚持做完 我自己
  • 仓位管理 – 2.实战篇

    上一篇 说到了仓位管理的重要性 这一篇则说明我对仓位控制算法的设计 以及最终使用的算法 由于内容较多 本文中我尽量只说重点 概念 算法 就是将一定可变范围内的一组输入条件 轮换到确定的输出时 所使用到的逻辑换算关系 仓位控制算法 其输入就是
  • study

    学习的博客地址 1 对自己python有帮助过的博客 http blog csdn net anbo724 article category 831447 另外有hadoop 等其他分类
  • 机器学习--人脸自动补齐(11)

    随机树ExtraTreeRegressor 分列点随机选取 不考虑信息增益 减少过拟合 获取一个对象所属的类名称 model class name import numpy as np import pandas as pd import
  • 肖臻老师区块链公开课笔记

    前段时间 区块链大火 出现了很多种基于区块链技术的政务应用 之前通过零散的网页信息和讲座 自我感觉理解了block chain原理 当看到各种区块链技术广泛应用时 自己以技术理解 反而对之不屑 当然 也怀疑自己还没有理解了区块链 带着这种好
  • 二、Flink使用异步算子请求高德地图获取位置信息

    目录 Flink异步算子使用介绍 Flink使用异步算子请求高德地图获取位置信息代码实现 相关阅读 Flink使用异步算子 线程池查询MySQL 1 概述 1 Flink异步算子使用介绍 1 异步与同步概述 同步 向数据库发送一个请求然后一
  • 一步步学习SPD2010--第二章节--处理SP网站(6)---- 探索SP网站

    SP技术没有一个界面 你可以通过使用Web浏览器或者兼容程序如Office 应用程序 包括SPD 你可以选择适合你必须完成的任务的接口 然而 根据你选择的程序 你可能有SP网站的不同视图 如果你使用MS Word 你只看到了网站和内容的一小
  • 爆肝整理 JVM 十大模块知识点总结,不信你还不懂

    01 JVM 内存结构 Java 虚拟机的内存空间分为 5 个部分 程序计数器 Java 虚拟机栈 本地方法栈 堆 方法区 JDK 1 8 同 JDK 1 7 比 最大的差别就是 元数据区取代了永久代 元空间的本质和永久 代类似 都是对 J
  • 【ESP32开发】——RGB LED灯(灯珠)点亮

    一 引言 本章内容主要介绍如何使用ESP32开发板点亮板载的RGB灯 使用的是ESP32 S3和ESP32 C3开发板 调用第三方库实现 由于网络上没有关于点亮ESP32板载RGB灯的资料 特此记录 二 ESP32 C3与ESP32 S3
  • python怎么批量处理数据_python操作数据之批量添加数据

    import pymysql import random import time from datetime import datetime type dict 测试01 001 测试02 002 测试03 003 测试04 004 fid
  • bind详解

    bind与占位符 绑定普通函数 绑定成员函数 绑定函数对象 如果你还在使用bind1st bind2nd 那么恐怕已经out了 Boost提供了更强大的武器bind 用于函数对象的绑定 bind接受的第一个参数必须是可调用对象f 包括函数
  • CentOS7.2安装Weblogic12c出现的问题

    Weblogic12c安装到步骤 Prerequisite Checks 时 会进行操作系统版本的校验 即checking operating system certification 此处操作系统版本会校验不过去 如下图 解决方案 修改
  • 刷脸生物支付有唯一性和不可替代性

    刷脸支付的时代 特别是刷脸支付解决了扫码支付需要手机为载体的痛点 在移动支付中生物支付以其唯一性和不可替代性 正在逐步替代传统的金融支付工具 引领未来支付结算模式 移动支付衍生的刷脸支付其核心技术就是人工智能中人脸识别技术 刷脸支付自然也是
  • 0027算法笔记——【回溯法】回溯法与装载问题

    1 回溯法 1 描述 回溯法是一种选优搜索法 按选优条件向前搜索 以达到目标 但当探索到某一步时 发现原先选择并不优或达不到目标 就退回一步重新选择 这种走不通就退回再走的技术为回溯法 2 原理 回溯法在问题的解空间树中 按深度优先策略 从
  • 自定义时间Toast(只弹一次)

    CToast类 package com pinkman dota util import com pinkman dota R import android content Context import android graphics C