自定义重写Toast

使用方法以及说明写在代码中的注释中
/**
 * 
 * @author liu
 *	本类调用方法new MyToast(context, "送积分:", "200", 20).showLonger();
 */
public class MyToast extends Toast {

    public MyToast(Context context, String message,String creit, int textsize) {
    	
        super(context);
        if (context == null) {
            return;
        }
        //设置外层容器
        RelativeLayout mainLayout = new RelativeLayout(context);
        ViewGroup.LayoutParams mainP = new ViewGroup.LayoutParams (ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        mainLayout.setLayoutParams(mainP);
        try {
        	//从assets文件夹中进行图片加载
			InputStream open = context.getResources().getAssets().open("toast.png");
			Drawable background=Drawable.createFromStream(open, "toast.png");
			mainLayout.setBackground(background);
		} catch (IOException e) {
			//找不到图片或者加载异常时设置背景色为黑色
			mainLayout.setBackgroundColor(Color.BLACK);
		}
        
        TextView content = new TextView(context);
        //PhoneUtil.getResolution(context)为获取屏幕分辨率的方法,根据屏幕的宽度计算toast显示的宽度
        int width=PhoneUtil.getResolution(context)[0]/2;
        int height=(width*2)/5;
        //设置宽和高
        RelativeLayout.LayoutParams contentP = new RelativeLayout.LayoutParams(width,height);
        content.setLayoutParams(contentP);

        //toast中显示的内容
        String str=message+creit;
        SpannableStringBuilder style=new SpannableStringBuilder(str); 
        //对于toast中的文字设置不同的颜色,message设置为白色,creit设置为红色
        style.setSpan(new ForegroundColorSpan(Color.WHITE),0,message.length()-1,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);  
//设置字体大小
        style.setSpan(new AbsoluteSizeSpan(60), message.length(), str.length(),Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        
        style.setSpan(new ForegroundColorSpan(Color.RED),message.length(),str.length(),Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);  
        
        content.setText(style);
        content.setTextSize(textsize);
        //设置字体居中
        content.setGravity(Gravity.CENTER);
        mainLayout.addView(content);

        //给字体设置动画效果
        TranslateAnimation alphaAnimation2 = new TranslateAnimation(-20f, 0, 0, 0);
        alphaAnimation2.setDuration(90);
        alphaAnimation2.setRepeatCount(9);
        alphaAnimation2.setRepeatMode(Animation.REVERSE);
        content.setAnimation(alphaAnimation2);

        alphaAnimation2.start();
        //Utils.convertToScreenPixels为根据分辨率及显示密度计算显示数值的方法
        setGravity(Gravity.BOTTOM, 0, Utils.convertToScreenPixels(70, 1));
        setDuration(Toast.LENGTH_LONG);//设置长显示
        setView(mainLayout);
    }
    
    public void showLonger() {
    	//把Toast.show添加到消息队列中并显示
        new Handler().postDelayed(new Runnable() {

            @Override
            public void run() {
                show();
            }
        //显示延迟时间
        }, 1000);
        
    }
}


  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

失落夏天

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值