旋转toast 自定义toast方向,支持多个方向的显示,自定义View
package com.example.canvasdemo;
import java.security.InvalidAlgorithmParameterException;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.animation.ValueAnimator.AnimatorUpdateListener;
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.FontMetrics;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
public class RotateToast extends View{
private static final int MVISIABLE = 0x0;
private Paint mPaint = null;
private int mW = 0;
private int mH = 0;
private String mContentText = "";
private int mFlag = 1;
private Type mOrientation = Type.ORITATION;
private Duration mDuration = Duration.SHORT;
private ObjectAnimator anim;
public RotateToast(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initData();
}
public RotateToast(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public RotateToast(Context context) {
this(context, null);
}
private void initData(){
mPaint = new Paint();
mPaint.setAntiAlias(true);
mPaint.setDither(true);
mPaint.setColor(Color.parseColor("#000000"));
mPaint.setTextSize(24);
mPaint.setStyle(Paint.Style.STROKE);
initAnim();
}
private void initAnim(){
int duration = mDuration.ordinal() == 0 ? 2000 : 3000;
anim = ObjectAnimator
.ofFloat(this, "liemng", 1.0F, 0.0F).setDuration(duration);
anim.addUpdateListener(new TimerAnimListener());
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
mW = w;
mH = h;
}
/**
* toast渐变效果
*/
public void startAnim(){
if(anim.isRunning()){
anim.cancel();
}
anim.start();
}
public void setText(String text) {
if(null == text){
new IllegalArgumentException("Invalid args && draw text no null");
return ;
}
this.mContentText = text;
}
public void setOrientation(Type type){
this.mOrientation = type;
}
public void setTextSize(int size){
if(size < 36){
new InvalidAlgorithmParameterException("Android not know < 12sp textSize");
}
mPaint.setTextSize(size);
}
public void setDuration(Duration mDuration){
this.mDuration = mDuration;
}
public void setVisiable(boolean isVisiable){
if(isVisiable){
mFlag |= MVISIABLE;
}else{
mFlag &= ~MVISIABLE;
}
}
@SuppressLint("NewApi")
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if((mFlag & MVISIABLE) != MVISIABLE) return;
canvas.save();
int ori = 0;
switch (mOrientation.ordinal()) {
case 1:
ori = 90;
break;
case 2:
ori = 180;
break;
case 3:
ori = 270;
break;
default:
ori = 0;
break;
}
int mRotatePonitX = mW/2;
int mRotatePonitY = mH/2;
canvas.rotate(ori, mRotatePonitX, mRotatePonitY);
int textW = (int) mPaint.measureText(mContentText);
canvas.drawText(mContentText, mRotatePonitX - textW/2, mRotatePonitY, mPaint);
//--绘制外边框
FontMetrics fontMetrics = mPaint.getFontMetrics();
//--这里可以增加deta值,使的文字和边框有一定的padding效果
int left = mRotatePonitX - textW/2;
int top = (int) (mRotatePonitY + fontMetrics.ascent);
int right = mRotatePonitX + textW/2;
int bottom = (int) (mRotatePonitY + fontMetrics.descent);
canvas.drawRoundRect(left, top, right, bottom, mRotatePonitX - textW/2, mRotatePonitY, mPaint);
canvas.restore();
}
public enum Type{
ORITATION,ORITATION_90,ORITATION_180,ORITATION_270;
}
public enum Duration{
SHORT,LONG;
}
private class TimerAnimListener implements AnimatorUpdateListener {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
// TODO Auto-generated method stub
Log.d("TAG","mDuration = " + animation.getCurrentPlayTime());
float mTotaltime = mDuration == Duration.SHORT ? 2000f : 3000f;
float currentPlayTime = animation.getCurrentPlayTime();
float alpha = ((mTotaltime - currentPlayTime)/mTotaltime * 255);
Log.d("TAG","alpha = " + ((mTotaltime - currentPlayTime)/mTotaltime * 255));
if(alpha <= 0)
alpha = 0;
mPaint.setAlpha((int)alpha);
invalidate();
}
}
}
实际效果,这里设置方向为90,所以效果如下:可能跟大家想要的效果不太一样,当然大家可以根据自己的需要增加相应的padding值。如文字和边框的间距

旋转toast 自定义toast方向,支持多个方向的显示,自定义View的更多相关文章
- 写了一个迷你toast提示插件,支持自定义提示文字和显示时间
写了一个迷你toast提示插件,支持自定义提示文字和显示时间,不想用其他第三方的ui插件,又想要toast等小效果来完善交互的同学可以试试, 代码中还贡献了一段css能力检测js工具函数,做项目的时候 ...
- 微信小程序自定义toast的实现
今天写微信小程序突然发现一个尴尬的问题,请求报错需要提示,就去小程序API里找,可悲的小程序的toast并不能满足我的需求,原生提供的方法调用如下 wx.showToast({ title: '成功' ...
- 023 Android 自定义Toast控件
1.Toast自定义控件工具类 package com.example.administrator.test62360safeguard.Utils; import android.content.C ...
- Android带图片的Toast(自定义Toast)
使用Android默认的Toast Toast简介: Toast是一个简单的消息显示框,能够短暂的出现在屏幕的某个位置,显示提示消息. 默认的位置是屏幕的下方正中,一般Toast的使用如下: Toas ...
- 自定义Toast和RatingBar的简单用例
Toast是一个包含用户点击消息.Toast类会帮助你创建和显示这些.Android中的Toast是一种简易的消息提示框. 当视图显示给用户,在应用程序中显示为浮动 向右划动五角星增加 单击按钮显示自 ...
- 自定义Toast解决快速点击时重复弹出,排队无止尽
解决办法:自定义MyToast类: public class MyToast { /** 之前显示的内容 */ private static String oldMsg ; /** Toast对象 * ...
- 朝花夕拾-android 自定义toast
在一个只有你而且还未知的世界中,不去探索未知,死守一处,你到底在守什么呢? 作为一个目前的android程序员,可能过去写着delphi的代码,可能未来回去搭建服务器.不管怎样,你现在是一名安卓程序员 ...
- Android 自定义Toast
自定义Toast 其实就是自定义布局文件 感觉利用Dialog或者PopupWindow做也差不多 上图上代码 public class MainActivity extends Activity { ...
- android135 360 来电去电归属地显示,自定义toast,
点击会开启服务. sivAddress.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) ...
- Android Toast 设置到屏幕中间,自定义Toast的实现方法,及其说明
http://blog.csdn.net/wangfayinn/article/details/8065763 Android Toast用于在手机屏幕上向用户显示一条信息,一段时间后信息会自动消失. ...
随机推荐
- mysql in 命令
SQL: select * from table where id IN (3,6,9,1,2,5,8,7); SQL: select * from table where id IN ($str); ...
- tomcat配置环境变量
先把jdk配置好,这里不在赘述. 一.配置Tomcat环境变量 1,新建变量名:CATALINA_BASE,变量值:C:\tomcat2,新建变量名:CATALINA_HOME,变 量值:C:\tom ...
- Beginning guide to Reactive Extension - Rx
http://msdn.microsoft.com/en-us/data/gg577611.aspx
- Eclipse for Mac 常用快捷键
为了提高开发效率,Eclipse 为我们提供了许多快捷键,它们能够帮助我们快速和方便的完成一些繁琐的操作.在这里只提供 Eclipse for Mac 的常用快捷键. Command + O:显示大纲 ...
- Sqoop_ 简单介绍
一.基本作用 概念: Sqoop被称为协作框架,是在Hadoop.2.X生态系统的辅助型框架,简单说,就是一个数据转换工具,类似的协作框架有文件收集库框架Flume,任务协调框架Oozie,大数据We ...
- windows 给ping加时间
@echo off set /p host=host Address: set logfile=Log_%host%.log echo Target Host = %host% >%logfil ...
- scala - Map基础
Map 构造Map 不可变: val map = Map("sa" -> 1, "s" -> 2)map("sa") = 3 / ...
- Java 路径
http://swiftlet.net/archives/713 Java中不存在标准的相对路径,各种相对路径取资源的方式都是基于某种规则转化为绝对路径.所以在Java中文件路径问题无非归结为一点:找 ...
- html5响应式设置<meta>
<meta name="viewport" content="width=device-width, initial-scale=1.0"> < ...
- 示例在同一台机器上使用RMAN克隆数据库
1.查看主库ZDJS并使用RMAM进行备份 [oracle@std ~]$ sqlplus '/as sysdba' SQL*Plus: Release - Production on Wed Jan ...