旋转toast 自定义toast方向,支持多个方向的显示,自定义View

 

  1 package com.example.canvasdemo;
  2 
  3 import java.security.InvalidAlgorithmParameterException;
  4 
  5 import android.animation.ObjectAnimator;
  6 import android.animation.ValueAnimator;
  7 import android.animation.ValueAnimator.AnimatorUpdateListener;
  8 import android.annotation.SuppressLint;
  9 import android.content.Context;
 10 import android.graphics.Canvas;
 11 import android.graphics.Color;
 12 import android.graphics.Paint;
 13 import android.graphics.Paint.FontMetrics;
 14 import android.util.AttributeSet;
 15 import android.util.Log;
 16 import android.view.View;
 17 
 18 public class RotateToast extends View{
 19     
 20     private static final int MVISIABLE = 0x0;
 21 
 22     private Paint mPaint = null;
 23     
 24     private int mW = 0;
 25     private int mH = 0;
 26     
 27     private String mContentText = "";
 28     
 29     private int mFlag = 1;
 30     
 31     private Type mOrientation = Type.ORITATION;
 32     
 33     private Duration mDuration = Duration.SHORT;
 34 
 35     private ObjectAnimator anim;
 36 
 37     public RotateToast(Context context, AttributeSet attrs, int defStyleAttr) {
 38         super(context, attrs, defStyleAttr);
 39         initData();
 40     }
 41 
 42     public RotateToast(Context context, AttributeSet attrs) {
 43         this(context, attrs, 0);
 44     }
 45 
 46     public RotateToast(Context context) {
 47         this(context, null);
 48     }
 49     
 50     private void initData(){
 51         mPaint = new Paint();
 52         mPaint.setAntiAlias(true);
 53         mPaint.setDither(true);
 54         mPaint.setColor(Color.parseColor("#000000"));
 55         mPaint.setTextSize(24);
 56         mPaint.setStyle(Paint.Style.STROKE);
 57         
 58         initAnim();
 59     }
 60     
 61     private void initAnim(){
 62         int duration = mDuration.ordinal() == 0 ? 2000 : 3000;
 63         anim = ObjectAnimator  
 64                 .ofFloat(this, "liemng", 1.0F,  0.0F).setDuration(duration);
 65         anim.addUpdateListener(new TimerAnimListener());
 66     }
 67     
 68     @Override
 69     protected void onSizeChanged(int w, int h, int oldw, int oldh) {
 70         super.onSizeChanged(w, h, oldw, oldh);
 71         mW = w;
 72         mH = h;
 73     }
 74     
 75     /**
 76      * toast渐变效果
 77      */
 78     public void startAnim(){
 79         if(anim.isRunning()){
 80             anim.cancel();
 81         }
 82         anim.start();
 83     }
 84     
 85     
 86     public void setText(String text) {
 87         if(null == text){
 88             new IllegalArgumentException("Invalid args && draw text no null");
 89             return ;
 90         }
 91         this.mContentText = text;
 92     }
 93     
 94     public void setOrientation(Type type){
 95         this.mOrientation = type;
 96     }
 97     
 98     public void setTextSize(int size){
 99         if(size < 36){
100             new InvalidAlgorithmParameterException("Android not know < 12sp textSize");
101         }
102         mPaint.setTextSize(size);
103     }
104     
105     public void setDuration(Duration mDuration){
106         this.mDuration = mDuration;
107     }
108     
109     public void setVisiable(boolean isVisiable){
110         if(isVisiable){
111             mFlag |= MVISIABLE;
112         }else{
113             mFlag &= ~MVISIABLE;
114         }
115     }
116     
117     @SuppressLint("NewApi")
118     @Override
119     protected void onDraw(Canvas canvas) {
120         super.onDraw(canvas);
121         if((mFlag & MVISIABLE) != MVISIABLE) return;
122         
123         canvas.save();
124         int ori = 0;
125         switch (mOrientation.ordinal()) {
126         case 1:
127             ori = 90;    
128             break;
129         case 2:
130             ori = 180;
131             break;
132         case 3:
133             ori = 270;
134             break;
135         default:
136             ori = 0;
137             break;
138         }
139         int mRotatePonitX = mW/2;
140         int mRotatePonitY = mH/2;
141         
142         canvas.rotate(ori, mRotatePonitX, mRotatePonitY);
143         
144         int textW = (int) mPaint.measureText(mContentText);
145         
146         canvas.drawText(mContentText, mRotatePonitX - textW/2, mRotatePonitY, mPaint);
147         
148         //--绘制外边框
149         FontMetrics fontMetrics = mPaint.getFontMetrics();
150         
151         int left = mRotatePonitX - textW/2;
152         int top = (int) (mRotatePonitY + fontMetrics.ascent);
153         int right = mRotatePonitX + textW/2;
154         int bottom = (int) (mRotatePonitY + fontMetrics.descent);
155         
156         canvas.drawRoundRect(left, top, right, bottom, mRotatePonitX - textW/2, mRotatePonitY, mPaint);
157         
158         canvas.restore();
159     }
160     
161     public enum Type{
162         ORITATION,ORITATION_90,ORITATION_180,ORITATION_270;
163     }
164     
165     public enum Duration{
166         SHORT,LONG;
167     }
168     
169     private class TimerAnimListener implements AnimatorUpdateListener {
170 
171         @Override
172         public void onAnimationUpdate(ValueAnimator animation) {
173             // TODO Auto-generated method stub
174             Log.d("TAG","mDuration = " + animation.getCurrentPlayTime());
175             float mTotaltime = mDuration == Duration.SHORT ? 2000f : 3000f;
176             float currentPlayTime = animation.getCurrentPlayTime();
177             float alpha = ((mTotaltime - currentPlayTime)/mTotaltime * 255);
178             Log.d("TAG","alpha = " + ((mTotaltime - currentPlayTime)/mTotaltime * 255));
179             if(alpha <= 0)
180                 alpha = 0;
181             mPaint.setAlpha((int)alpha);
182             invalidate();
183         }
184         
185     }
186 }

 

实际效果,这里设置方向为90,所以效果如下:可能跟大家想要的效果不太一样,当然大家可以根据自己的需要增加相应的padding值。如文字和边框的间距

 

posted @ 2015-11-03 21:39  冒泡的章鱼  阅读(443)  评论(0编辑  收藏  举报