Android Toast使用的簡單小結(jié)(推薦)
老規(guī)矩,先上效果圖吧
主要實現(xiàn)了幾種常用的方式:
1.最基本的Toast
系統(tǒng)自帶Toast采用的是隊列的方式, 等當前Toast消失后, 下一個Toast才能顯示出來;原因是Toast的管理是在隊列中,點擊一次,就會產(chǎn)生一個新的Toast,要等這個隊列中的Toast處理完,這個顯示Toast的任務才算結(jié)束。so~ 我們可以把Toast改成單例模式,沒有Toast再新建它,這樣也就解決了連續(xù)點擊Toast,一直在顯示的問題。
2.自定義位置的Toast
3.自定義布局(帶圖片)的Toast
4.自定義帶動畫效果的Toast控件
OK,下面上代碼
代碼實現(xiàn):
先上Activity的代碼
public class ToastActivity extends BaseTitleActivity { @BindView(R.id.btn_basic_toast) Button basicToast; @BindView(R.id.btn_basic_toast2) Button basicToast2; @BindView(R.id.btn_custom_location) Button customLocation; @BindView(R.id.btn_custom_picture) Button customPicture; @BindView(R.id.btn_custom_smile) Button customSmile; @BindView(R.id.btn_custom_smile2) Button customSmile2; // private static CustomToast customToastView; public static void newInstance(Context context){ Intent intent = new Intent(context, ToastActivity.class); context.startActivity(intent); } @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); } @Override public int getResourcesId() { return R.layout.activity_toast; } @Override public void initView() { } @Override public void initData() { } @Override public int getTitleText() { return R.string.play_toast; } @OnClick( {R.id.btn_basic_toast, R.id.btn_custom_location, R.id.btn_custom_picture, R.id.btn_custom_smile, R.id.btn_custom_smile2, R.id.btn_basic_toast2} ) public void onViewClick(View v){ switch (v.getId()){ /* 最基本的Toast,解決了原生Toast不能快速更新的問題 */ case R.id.btn_basic_toast: ToastUtils.showToast(this, "這是最基本的Toast"); break; case R.id.btn_basic_toast2: ToastUtils.showToast(this, "===已更新==="); break; /* 自定義位置的Toast * 相對于Gravity.LEFT位置, x方向上的偏移量, y方向上的偏移量 */ case R.id.btn_custom_location: Toast toast = Toast.makeText(ToastActivity.this, "自定義位置的Toast", Toast.LENGTH_SHORT); toast.setGravity(Gravity.LEFT,0, 0); toast.show(); break; /* 帶圖片的Toast,自定義布局 * 參考 Toast.makeText() 方法 */ case R.id.btn_custom_picture: Toast result = new Toast(this); View toastView = LayoutInflater.from(this).inflate(R.layout.toast_custom, null); ImageView img = (ImageView) toastView.findViewById(R.id.iv_img); TextView msg = (TextView) toastView.findViewById(R.id.tv_msg); img.setImageResource(R.mipmap.jyfr_icon_mpossh3x); msg.setText(R.string.picture_toast); result.setView(toastView); result.setGravity(Gravity.BOTTOM, 0 , 0); result.setDuration(Toast.LENGTH_SHORT); result.show(); break; /* 自定義Toast控件,帶個動畫效果 * 解決了原生Toast不能快速更新的問題 * 但是并沒有擺脫原生Toast顯示方法的調(diào)用 */ case R.id.btn_custom_smile: ToastUtils.showToast(this, "在看我", true); break; case R.id.btn_custom_smile2: ToastUtils.showToast(this, "==還在看我==", true); break; default: break; } } }
對應的布局代碼較為簡單,就不貼了
下面是第四種效果實現(xiàn)的布局
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="10dp" android:background="@drawable/shape_background_toast"> <ImageView android:id="@+id/iv_img" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:scaleType="center" android:visibility="visible"/> <com.example.xuetaotao.helloworld.widget.CustomToast android:id="@+id/smileView" android:layout_width="50dp" android:layout_height="50dp" android:layout_margin="10dp" android:layout_gravity="center" /> <TextView android:id="@+id/tv_msg" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:text="" android:textSize="18sp" android:textColor="@color/common_blue"/> </LinearLayout>
接著是自定義控件部分的代碼
public class CustomToast extends View { /** * 初始化一些變量 * 實現(xiàn)3個構(gòu)造函數(shù) * 初始化畫筆參數(shù)和矩形參數(shù) * 設置畫筆的參數(shù)及矩形的參數(shù) * 重寫onMeasure:onMeasure()方法中主要負責測量,決定控件本身或其子控件所占的寬高 * 重寫onDraw:onDraw()方法負責繪制,即如果我們希望得到的效果在Android原生控件中沒有現(xiàn)成的支持,那么我們就需要自己繪制我們的自定義控件的顯示效果。 * 自定義View中的動畫效果實現(xiàn) */ private Toast toast; private Context context; RectF rectF = new RectF(); //矩形,設置Toast布局時使用 ValueAnimator valueAnimator; //屬性動畫 private Paint paint; //自定義View的畫筆 float mAnimatedValue = 0f; private float mWidth = 0f; //view的寬 private float mPadding = 0f; //view的內(nèi)邊距 private float endAngle = 0f; //圓弧結(jié)束的度數(shù) private float mEyeWidth = 0f; //笑臉的眼睛半徑 private boolean isSmileLeft = false; private boolean isSmileRight = false; public CustomToast(Context context){ super(context); this.context = context; } public CustomToast(Context context, AttributeSet attrs){ super(context, attrs); this.context = context; } public CustomToast(Context context, AttributeSet attrs, int defStyleAttr){ super(context, attrs, defStyleAttr); this.context = context; } private void initPaint(){ paint = new Paint(); paint.setAntiAlias(true); //抗鋸齒 paint.setStyle(Paint.Style.STROKE); //畫筆的樣式:空心 paint.setColor(Color.parseColor("#5cb85c")); //繪制的顏色 paint.setStrokeWidth(dip2px(2)); //設置筆刷的粗細 } private void initRect(){ rectF = new RectF(mPadding, mPadding, mWidth-mPadding, mWidth-mPadding); } //dip轉(zhuǎn)px。為了支持多分辨率手機 public int dip2px(float dpValue){ final float scale = getContext().getResources().getDisplayMetrics().density; return (int) (dpValue * scale + 0.5f); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); initPaint(); initRect(); mWidth = getMeasuredWidth(); //view的寬度 mPadding = dip2px(10); mEyeWidth = dip2px(3); } //每次觸摸了自定義View/ViewGroup時都會觸發(fā)onDraw()方法 @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); paint.setStyle(Paint.Style.STROKE); canvas.drawArc(rectF, 180, endAngle, false, paint ); //畫微笑圓弧 paint.setStyle(Paint.Style.FILL); //畫筆的樣式:實心 if (isSmileLeft){ canvas.drawCircle(mPadding+mEyeWidth+mEyeWidth/2, mWidth/3, mEyeWidth, paint); //繪制圓圈 } if (isSmileRight){ canvas.drawCircle(mWidth-mPadding-mEyeWidth-mEyeWidth/2, mWidth/3, mEyeWidth, paint); } } //開啟動畫 public void startAnimator(boolean playAnimate){ if (playAnimate){ stopAnimator(); startViewAnim(0f, 1f, 2000); } } //停止動畫 public void stopAnimator(){ if (valueAnimator != null){ clearAnimation(); isSmileLeft = false; isSmileRight = false; mAnimatedValue = 0f; valueAnimator.end(); } } /** * 開始動畫 * @param start 起始值 * @param end 結(jié)束值 * @param time 動畫的時間 * @return */ public ValueAnimator startViewAnim(float start, float end, long time){ valueAnimator = ValueAnimator.ofFloat(start, end); //設置 ValueAnimator 的起始值和結(jié)束值 valueAnimator.setDuration(time); //設置動畫時間 valueAnimator.setInterpolator(new LinearInterpolator()); //設置補間器,控制動畫的變化速率 valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { //設置監(jiān)聽器。監(jiān)聽動畫值的變化,做出相應方式 @Override public void onAnimationUpdate(ValueAnimator animation) { mAnimatedValue = (float) valueAnimator.getAnimatedValue(); if (mAnimatedValue < 0.5){ isSmileLeft = false; isSmileRight = false; endAngle = -360 * (mAnimatedValue); } else if (mAnimatedValue > 0.55 && mAnimatedValue < 0.7){ endAngle = -180; isSmileLeft = true; isSmileRight = false; } else{ endAngle = -180; isSmileLeft = true; isSmileRight = true; } postInvalidate(); //重繪 } }); if (!valueAnimator.isRunning()){ valueAnimator.start(); } return valueAnimator; } /** * 本質(zhì)上還是依賴Android原生Toast的顯示方法來進行顯示, * 只是引入了自定義的布局,添加了自定義動畫 */ public void show(String message, boolean playAnimate){ /* 解決多次點擊Toast一直提示不消失問題 */ if (toast == null){ toast = new Toast(context); } View customToastView = LayoutInflater.from(context).inflate(R.layout.toast_custom, null); TextView msg2 = (TextView) customToastView.findViewById(R.id.tv_msg); msg2.setText(message); msg2.setBackgroundResource(R.drawable.shape_text_toast); msg2.setTextColor(Color.parseColor("#ffffff")); ImageView img2 = (ImageView) customToastView.findViewById(R.id.iv_img); img2.setImageResource(R.mipmap.jyfr_icon_mpossh3x); // img2.setVisibility(View.GONE); CustomToast customToast = (CustomToast) customToastView.findViewById(R.id.smileView); customToast.startAnimator(playAnimate); toast.setView(customToastView); toast.setGravity(Gravity.BOTTOM, 0 , 0); toast.setDuration(Toast.LENGTH_SHORT); toast.show(); } }
最后把效果一和四共用到的工具類貼上來,主要是為了解決連續(xù)點擊Toast,一直在顯示的問題。補充一點:效果四仍然是基于系統(tǒng)原生Toast的顯示方法來顯示的,所以那個連續(xù)點擊一直顯示的問題還是存在的,后面再試試不用這種方式吧。
public class ToastUtils { private static Toast toast; private static CustomToast customToast; /** * 自定義CustomToast的顯示 * @param context 上下文 * @param message 提示內(nèi)容 * @param playAnimate 是否顯示動畫 true,顯示 false,不顯示 */ public static void showToast(Context context, String message, boolean playAnimate){ if (customToast == null){ customToast = new CustomToast(context); } customToast.show(message, playAnimate); } /** * Android原生Toast的顯示,主要解決點多少就提示多少次的問題 */ public static void showToast(Context context, String content){ if (toast == null){ toast = Toast.makeText(context, content, Toast.LENGTH_SHORT); } else { toast.setText(content); } toast.show(); } }
OK,完成,新手入門學習報到~
到此這篇關(guān)于Android Toast使用的簡單小結(jié)的文章就介紹到這了,更多相關(guān)Android Toast使用內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
android 9.0 launcher3 去掉抽屜式顯示所有 app(代碼詳解)
本文通過實例代碼給大家介紹了android 9.0 Launcher3 去掉抽屜式,顯示所有 app,本文給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2019-11-11android開發(fā)修改狀態(tài)欄背景色和圖標顏色的示例
本篇文章主要介紹了android開發(fā)修改狀態(tài)欄背景色和圖標顏色的示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-01-01Android實現(xiàn)仿微信tab高亮icon粘著手的滑動效果
這篇文章主要為大家詳細介紹了Android實現(xiàn)仿微信tab高亮icon粘著手的滑動效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-08-08Android自定義view實現(xiàn)多色進度條GradientProgressView的繪制
我們常使用shape實現(xiàn)漸變色,但是shape的極限卻只有三色,如果有超過三種顏色的View的要求,那么我們就不得不去自定義View來實現(xiàn)這個需求,所以下面我們就來看看如何自定義view實現(xiàn)多色進度條的繪制吧2023-08-08