1. 修改TipToast背景为渐变
2. 增加左侧带图片的TipToast
This commit is contained in:
@@ -0,0 +1,77 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -4,6 +4,10 @@ package com.mogo.utils;
|
||||
* 2016/1/1 by congtaowang
|
||||
*
|
||||
* @Version 1.0
|
||||
*
|
||||
* 增加了图片支持
|
||||
*
|
||||
* @Version 1.1
|
||||
*/
|
||||
|
||||
import android.content.Context;
|
||||
@@ -45,17 +49,17 @@ public final class TipToast {
|
||||
sGenerator = null;
|
||||
}
|
||||
|
||||
private static void tip( final String message, int duration ) {
|
||||
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 ) ).start();
|
||||
new ToastThread(new StringToastRunnable(sContext, message, duration, tipDrawable)).start();
|
||||
}
|
||||
|
||||
private static void tip( final int msgId, int duration ) {
|
||||
private static void tip( final int msgId, int duration,TipDrawable tipDrawable ) {
|
||||
if ( !checkParams() ) {
|
||||
return;
|
||||
}
|
||||
@@ -66,7 +70,7 @@ public final class TipToast {
|
||||
} catch ( Exception e ) {
|
||||
return;
|
||||
}
|
||||
tip( ResourcesHelper.getString( sContext, msgId ), duration );
|
||||
tip( ResourcesHelper.getString( sContext, msgId ), duration ,tipDrawable);
|
||||
}
|
||||
|
||||
private static boolean checkParams() {
|
||||
@@ -82,27 +86,53 @@ public final class TipToast {
|
||||
}
|
||||
|
||||
public static void tip( final String message ) {
|
||||
tip( message, Toast.LENGTH_SHORT );
|
||||
tip( message, Toast.LENGTH_SHORT,null );
|
||||
}
|
||||
|
||||
public static void tip( final int msgId ) {
|
||||
tip( msgId, Toast.LENGTH_SHORT );
|
||||
tip( msgId, Toast.LENGTH_SHORT ,null);
|
||||
}
|
||||
|
||||
public static void longTip( String message ) {
|
||||
tip( message, Toast.LENGTH_LONG );
|
||||
tip( message, Toast.LENGTH_LONG ,null);
|
||||
}
|
||||
|
||||
public static void longTip( int msgId ) {
|
||||
tip( msgId, Toast.LENGTH_LONG );
|
||||
tip( msgId, Toast.LENGTH_LONG ,null);
|
||||
}
|
||||
|
||||
public static void shortTip( String message ) {
|
||||
tip( message, Toast.LENGTH_SHORT );
|
||||
tip( message, Toast.LENGTH_SHORT ,null);
|
||||
}
|
||||
|
||||
public static void shortTip( int msgId ) {
|
||||
tip( msgId, Toast.LENGTH_SHORT );
|
||||
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 {
|
||||
@@ -117,11 +147,13 @@ public final class TipToast {
|
||||
Context context;
|
||||
String msg;
|
||||
int duration;
|
||||
TipDrawable tipDrawable;
|
||||
|
||||
public StringToastRunnable( Context context, String msg, int duration ) {
|
||||
public StringToastRunnable( Context context, String msg, int duration,TipDrawable tipDrawable ) {
|
||||
this.context = context;
|
||||
this.msg = msg;
|
||||
this.duration = duration;
|
||||
this.tipDrawable = tipDrawable;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -131,44 +163,40 @@ public final class TipToast {
|
||||
return;
|
||||
}
|
||||
|
||||
sHandler.post( new Runnable() {
|
||||
sHandler.post(() -> {
|
||||
synchronized ( sSyncObject ) {
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
synchronized ( sSyncObject ) {
|
||||
if ( context == null ) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ( context == null ) {
|
||||
return;
|
||||
}
|
||||
if ( sToast != null ) {
|
||||
sToast.cancel();
|
||||
}
|
||||
|
||||
if ( sToast != null ) {
|
||||
sToast.cancel();
|
||||
}
|
||||
|
||||
if ( sGenerator == null ) {
|
||||
sToast = Toast.makeText( context, msg, duration );
|
||||
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 = new Toast( context );
|
||||
final View view = sGenerator.make( context, msg );
|
||||
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();
|
||||
sToast = Toast.makeText( context, msg, duration );
|
||||
}
|
||||
}
|
||||
if ( sToast != null ) {
|
||||
sToast.show();
|
||||
}
|
||||
}
|
||||
} );
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
public interface ToastViewGenerator {
|
||||
View make( Context context, String message );
|
||||
View make( Context context, String message,TipDrawable tipDrawable );
|
||||
|
||||
default int gravity() {
|
||||
return Gravity.CENTER;
|
||||
@@ -182,5 +210,4 @@ public final class TipToast {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user