核心代码:
package com.huatec.myapplication;
import android.content.Context;
import android.graphics.Bitmap;
import android.support.annotation.ColorInt;
import android.support.annotation.DrawableRes;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
/**
* 自定义吐司
*/
public final class MyToast {
public final Toast toast;//Toast对象
public final View view;//Toast的UI效果
public final ImageView icon;//图标
public final TextView message;//内容
public MyToast(Context context) {
toast = new Toast(context);
view = LayoutInflater.from(context).inflate(R.layout.toast_layout, null);
icon = view.findViewById(R.id.toast_icon);
message = view.findViewById(R.id.toast_message);
}
/**
* 显示
*/
public void show() {
this.toast.show();
}
public static class Builder {
private Bitmap icon;//图标图片
// private int iconID = R.mipmap.ic_launcher;//图标资源ID
private int iconID;//
private String message;//内容
private int backgroundColor = 0x56000000;//背景颜色
private Context mContext;//上下文
private int duration = Toast.LENGTH_SHORT;//设置时间
private MyToast mine;
private int gravity = Gravity.NO_GRAVITY;//设置位置
private int offsetX = 0;//设置偏移度X
private int offsetY = 0;//设置偏移度Y
private boolean isShowIcon;//是否显示图标
public Builder(Context context) {
this.mContext = context;
}
/**
* 设置ICON
*
* @param bitmap
* @return
*/
public Builder setIcon(Bitmap bitmap) {
this.icon = bitmap;
return this;
}
public Builder setIcon(@DrawableRes int resId) {
this.iconID = resId;
return this;
}
public Builder showIcon(boolean showIcon) {
this.isShowIcon = showIcon;
return this;
}
/**
* 设置内容
*/
public Builder setMessage(String hintMessage) {
this.message = hintMessage;
return this;
}
/**
* 设置吐司时长
*/
public Builder setDuration(int type) {
this.duration = type;
return this;
}
/**
* 设置背景
*/
public Builder setBackgroundColor(@ColorInt int color) {
this.backgroundColor = color;
return this;
}
/**
* 设置位置
*/
public Builder setGravity(int gravity) {
this.gravity = gravity;
return this;
}
/**
* 偏移量
*/
public Builder setOffsetX(int x) {
this.offsetX = x;
return this;
}
public Builder setOffsetY(int y) {
this.offsetY = y;
return this;
}
/**
* 创建MyToast对象
*/
public MyToast build() {
if (null == mine) {
mine = new MyToast(mContext);//创建对象
}
if (isShowIcon) {
//隐藏图标
mine.icon.setVisibility(View.VISIBLE);
if (null != icon) {//判断是否显示图标
mine.icon.setImageBitmap(icon);//设置图片
} else if (iconID != 0) {
//设置图片
mine.icon.setBackgroundResource(iconID);
}
}
if (!message.isEmpty()) {//判断内容是否为空
mine.message.setText(message);
} else {
mine.message.setText("");
}
//设置背景
mine.view.setBackground(new BackgroundDrawable(backgroundColor, mContext));
//mine.message.setTextColor(Color.BLACK);//设置字体颜色
mine.toast.setDuration(duration);//设置时长
mine.toast.setView(mine.view);//添加自定义效果
mine.toast.setGravity(gravity, offsetX, offsetY);//设置偏移量
return mine;
}
}
}
toast_layout.xml布局文件:
BackgroundDrawable.java背景:
package com.huatec.myapplication;
import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.ColorFilter;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.support.annotation.ColorInt;
import android.util.TypedValue;
public class BackgroundDrawable extends Drawable{
private Paint paint;
private Context mContext;
public BackgroundDrawable(@ColorInt int color,Context context) {
mContext = context.getApplicationContext();
paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setColor(color);
paint.setDither(true);
paint.setStyle(Paint.Style.FILL);
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
@Override
public void draw(Canvas canvas) {
int width = canvas.getWidth();
int height = canvas.getHeight();
canvas.drawRoundRect(0,0,width,height,dp2px(20),dp2px(20),paint);
}
@Override
public void setAlpha(int alpha) {
paint.setAlpha(alpha);
}
@Override
public void setColorFilter(ColorFilter colorFilter) {
paint.setColorFilter(colorFilter);
}
@Override
public int getOpacity() {
return PixelFormat.TRANSLUCENT;
}
private int dp2px(int values){
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,values,
mContext.getResources().getDisplayMetrics());
}
}
使用Toast:
package com.huatec.myapplication;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
private Button hintToast1;
private Button hintToast2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
hintToast1 = findViewById(R.id.hint_toast);
hintToast2 = findViewById(R.id.hint_toast2);
//按钮1
hintToast1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new MyToast.Builder(MainActivity.this)
.setMessage("你好吗?")//设置提示文字
.setBackgroundColor(0x88ff4587)//设置背景颜色
.setGravity(Gravity.CENTER)//设置吐司位置
.build()//创建吐司
.show();
}
});
//按钮2
hintToast2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new MyToast.Builder(MainActivity.this)
.setMessage("你好吗?")
.setBackgroundColor(0x88ff4587)
.setGravity(Gravity.CENTER)
.setIcon(R.mipmap.ic_launcher)//设置图标
.showIcon(true)//是否显示图标
.build()
.show();
}
});
}
}
效果图:
源码地址:https://github.com/280357392/CustomToast