移动TipToast

Signed-off-by: donghongyu <donghongyu@zhidaoauto.com>
This commit is contained in:
donghongyu
2021-11-08 18:52:15 +08:00
parent d6c5c468f4
commit 099fde5d46
48 changed files with 81 additions and 77 deletions

View File

@@ -1,81 +0,0 @@
package com.mogo.utils;
import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.content.res.XmlResourceParser;
import android.graphics.drawable.Drawable;
import android.util.DisplayMetrics;
public class ResourcesHelper {
public static Resources getResources( Context context ) {
return context.getResources();
}
public static int getColor( Context context, int rid ) {
return getResources( context ).getColor( rid );
}
public static ColorStateList getColorStateList( Context context, int rid ) {
return getResources( context ).getColorStateList( rid );
}
public static String getString( Context context, int rid ) {
return getResources( context ).getString( rid );
}
public static String getString( Context context, int rid, int param1, int param2 ) {
return getResources( context ).getString( rid, param1, param2 );
}
public static String getString( Context context, int rid, String str ) {
return getResources( context ).getString( rid, str );
}
public static String[] getStringArray( Context context, int rid ) {
return getResources( context ).getStringArray( rid );
}
public static Drawable getDrawable( Context context, int rid ) {
return getResources( context ).getDrawable( rid );
}
public static float getDimension( Context context, int rid ) {
return getResources( context ).getDimension( rid );
}
public static DisplayMetrics getDisplayMetrics( Context context ) {
return getResources( context ).getDisplayMetrics();
}
public static int getDisplayMetrics( Context context, int x ) {
return getResources( context ).getDimensionPixelSize( x );
}
public static int getDimensionPixelSize( Context context, int x ) {
return getResources( context ).getDimensionPixelSize( x );
}
public static int getInteger( Context context, int rid ) {
return getResources( context ).getInteger( rid );
}
public static XmlResourceParser getXml( Context context, int rid ) {
return getResources( context ).getXml( rid );
}
public static Configuration getConfiguration( Context context ) {
return getResources( context ).getConfiguration();
}
public static String getResNameById( Context context, int id ) {
try {
return context.getResources().getResourceName( id );
} catch ( Exception e ) {
e.printStackTrace();
return String.valueOf( id );
}
}
}

View File

@@ -1,77 +0,0 @@
package com.mogo.utils;
import android.graphics.drawable.Drawable;
/**
* TipToast弹出框的图片资源封装类
* 当前只支持添加一张图片,可添加到文字的 左{@link #TIP_DRAWABLE_GRAVITY_LEFT}, 上{@link #TIP_DRAWABLE_GRAVITY_TOP}, 右{@link #TIP_DRAWABLE_GRAVITY_RIGHT}, 下{@link #TIP_DRAWABLE_GRAVITY_BOTTOM}
*
* 当前仅支持添加左侧图片,所以{@link #gravity} 这个参数形同虚设
*
* @author tongchenfei
*/
public class TipDrawable {
public static final int TIP_DRAWABLE_GRAVITY_LEFT = 1;
public static final int TIP_DRAWABLE_GRAVITY_TOP = 2;
public static final int TIP_DRAWABLE_GRAVITY_RIGHT = 3;
public static final int TIP_DRAWABLE_GRAVITY_BOTTOM = 4;
private Drawable drawable;
private int gravity;
private int width;
private int height;
/**
* 默认图片居左,宽高使用{@link Drawable#getIntrinsicWidth()}和{@link Drawable#getIntrinsicHeight()}
* @param drawable 要展示的图片
*/
public TipDrawable(Drawable drawable) {
this(drawable, TIP_DRAWABLE_GRAVITY_LEFT,drawable.getIntrinsicWidth(),drawable.getIntrinsicHeight());
}
/**
* 默认图片居左
* @param drawable 要展示的图片
* @param width 要展示的图片宽度
* @param height 要展示的图片高度
*/
public TipDrawable(Drawable drawable, int width, int height) {
this(drawable, TIP_DRAWABLE_GRAVITY_LEFT,width,height);
}
/**
* 默认宽高使用{@link Drawable#getIntrinsicWidth()}和{@link Drawable#getIntrinsicHeight()}
* @param drawable 要展示的图片
* @param gravity 左{@link #TIP_DRAWABLE_GRAVITY_LEFT}, 上{@link #TIP_DRAWABLE_GRAVITY_TOP}, 右{@link #TIP_DRAWABLE_GRAVITY_RIGHT}, 下{@link #TIP_DRAWABLE_GRAVITY_BOTTOM}
*/
public TipDrawable(Drawable drawable, int gravity) {
this(drawable, gravity,drawable.getIntrinsicWidth(),drawable.getIntrinsicHeight());
}
/**
* 可自定义展示位置
* @param drawable 要展示的图片
* @param gravity 左{@link #TIP_DRAWABLE_GRAVITY_LEFT}, 上{@link #TIP_DRAWABLE_GRAVITY_TOP}, 右{@link #TIP_DRAWABLE_GRAVITY_RIGHT}, 下{@link #TIP_DRAWABLE_GRAVITY_BOTTOM}
*/
public TipDrawable(Drawable drawable, int gravity, int width, int height) {
this.drawable = drawable;
this.gravity = gravity;
this.width = width;
this.height = height;
}
public Drawable getDrawable() {
return drawable;
}
public int getGravity() {
return gravity;
}
public int getWidth() {
return width;
}
public int getHeight() {
return height;
}
}

View File

@@ -1,213 +0,0 @@
package com.mogo.utils;
/*
* 2016/1/1 by congtaowang
*
* @Version 1.0
*
* 增加了图片支持
*
* @Version 1.1
*/
import android.content.Context;
import android.os.Handler;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.widget.Toast;
import com.mogo.utils.logger.Logger;
/**
* Init TipToast in your Application
*/
public final class TipToast {
private static final String TAG = "TipToast";
private static Toast sToast = null;
private static final byte[] sSyncObject = new byte[0];
private static Handler sHandler = null;
private static Context sContext;
private static ToastViewGenerator sGenerator;
public static void init( Context context, ToastViewGenerator generator ) {
TipToast.sContext = context;
sHandler = new Handler( context.getMainLooper() );
sGenerator = generator;
}
public static void recycle() {
sContext = null;
sHandler = null;
if ( sToast != null ) {
sToast.cancel();
sToast = null;
}
sGenerator = null;
}
private static void tip( final String message, int duration ,TipDrawable tipDrawable) {
if ( !checkParams() ) {
return;
}
if ( TextUtils.isEmpty( message ) ) {
return;
}
new ToastThread(new StringToastRunnable(sContext, message, duration, tipDrawable)).start();
}
private static void tip( final int msgId, int duration,TipDrawable tipDrawable ) {
if ( !checkParams() ) {
return;
}
try {
if ( TextUtils.isEmpty( ResourcesHelper.getString( sContext, msgId ) ) ) {
return;
}
} catch ( Exception e ) {
return;
}
tip( ResourcesHelper.getString( sContext, msgId ), duration ,tipDrawable);
}
private static boolean checkParams() {
if ( sContext == null ) {
Logger.e( TAG, "context can't be null." );
return false;
}
if ( sHandler == null ) {
Logger.e( TAG, "sHandler can't be null." );
return false;
}
return true;
}
public static void tip( final String message ) {
tip( message, Toast.LENGTH_SHORT,null );
}
public static void tip( final int msgId ) {
tip( msgId, Toast.LENGTH_SHORT ,null);
}
public static void longTip( String message ) {
tip( message, Toast.LENGTH_LONG ,null);
}
public static void longTip( int msgId ) {
tip( msgId, Toast.LENGTH_LONG ,null);
}
public static void shortTip( String message ) {
tip( message, Toast.LENGTH_SHORT ,null);
}
public static void shortTip( int msgId ) {
tip( msgId, Toast.LENGTH_SHORT ,null);
}
// -===带图片的方法===-
public static void tip( final String message,TipDrawable tipDrawable ) {
tip( message, Toast.LENGTH_SHORT,tipDrawable );
}
public static void tip( final int msgId,TipDrawable tipDrawable ) {
tip( msgId, Toast.LENGTH_SHORT ,tipDrawable);
}
public static void longTip( String message,TipDrawable tipDrawable ) {
tip( message, Toast.LENGTH_LONG ,tipDrawable);
}
public static void longTip( int msgId ,TipDrawable tipDrawable) {
tip( msgId, Toast.LENGTH_LONG ,tipDrawable);
}
public static void shortTip( String message,TipDrawable tipDrawable ) {
tip( message, Toast.LENGTH_SHORT ,tipDrawable);
}
public static void shortTip( int msgId,TipDrawable tipDrawable ) {
tip( msgId, Toast.LENGTH_SHORT ,tipDrawable);
}
static class ToastThread extends Thread {
public ToastThread( Runnable runnable ) {
super( runnable );
}
}
static class StringToastRunnable implements Runnable {
Context context;
String msg;
int duration;
TipDrawable tipDrawable;
public StringToastRunnable( Context context, String msg, int duration,TipDrawable tipDrawable ) {
this.context = context;
this.msg = msg;
this.duration = duration;
this.tipDrawable = tipDrawable;
}
@Override
public void run() {
if ( sHandler == null ) {
return;
}
sHandler.post(() -> {
synchronized ( sSyncObject ) {
if ( context == null ) {
return;
}
if ( sToast != null ) {
sToast.cancel();
}
if ( sGenerator == null ) {
sToast = Toast.makeText( context, msg, duration );
} else {
sToast = new Toast( context );
final View view = sGenerator.make( context, msg, tipDrawable );
if ( view != null ) {
sToast.setView( view );
sToast.setGravity( sGenerator.gravity(), sGenerator.xOffset(), sGenerator.yOffset() );
sToast.setDuration( duration );
} else {
sToast = Toast.makeText( context, msg, duration );
}
}
if ( sToast != null ) {
sToast.show();
}
}
});
}
}
public interface ToastViewGenerator {
View make( Context context, String message,TipDrawable tipDrawable );
default int gravity() {
return Gravity.CENTER;
}
default int yOffset() {
return 0;
}
default int xOffset() {
return 0;
}
}
}