旋转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

上一篇:【bzoj2440】【bzoj3994】莫比乌斯反演学习


下一篇:Java里的File I/O