1. 修改TipToast背景为渐变

2. 增加左侧带图片的TipToast
This commit is contained in:
tongchenfei
2020-09-01 16:46:59 +08:00
parent 5673ec457f
commit 6e6c305230
12 changed files with 274 additions and 61 deletions

2
.idea/misc.xml generated
View File

@@ -4,7 +4,7 @@
<asm skipDebug="false" skipFrames="false" skipCode="false" expandFrames="false" />
<groovy codeStyle="LEGACY" />
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" project-jdk-name="JDK" project-jdk-type="JavaSDK">
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" project-jdk-name="1.8" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/build/classes" />
</component>
</project>

View File

@@ -4,6 +4,8 @@ import android.app.Application;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.alibaba.android.arouter.launcher.ARouter;
@@ -17,6 +19,7 @@ import com.mogo.commons.network.ParamsUtil;
import com.mogo.commons.network.X509TrustManagerImpl;
import com.mogo.commons.storage.SpStorage;
import com.mogo.utils.ThreadPoolService;
import com.mogo.utils.TipDrawable;
import com.mogo.utils.TipToast;
import com.mogo.utils.network.NetConfig;
@@ -65,13 +68,26 @@ public class AbsMogoApplication extends Application {
ARouter.openLog();
}
ARouter.init( sApp );
TipToast.init( this, ( ( context, message ) -> {
TipToast.init( this, ( ( context, message, tipDrawable ) -> {
if ( TextUtils.isEmpty( message ) ) {
return null;
}
View contentView = LayoutInflater.from( context ).inflate( R.layout.module_commons_layout_toast, null );
TextView txt = contentView.findViewById( R.id.module_commons_toast_msg );
txt.setText( message );
View contentView;
if(tipDrawable==null) {
contentView = LayoutInflater.from(context).inflate(R.layout.module_commons_layout_toast, null);
TextView txt = contentView.findViewById(R.id.module_commons_toast_msg);
txt.setText(message);
}else{
// 有图片,使用带图片的布局,当前只实现了左侧图片
contentView = LayoutInflater.from(context).inflate(R.layout.module_commons_layout_toast_with_left_drawable, null);
TextView txt = contentView.findViewById(R.id.module_commons_toast_msg);
ImageView img = contentView.findViewById(R.id.module_commons_toast_left_drawable);
img.setImageDrawable(tipDrawable.getDrawable());
ViewGroup.LayoutParams params = img.getLayoutParams();
params.width = tipDrawable.getWidth();
params.height = tipDrawable.getHeight();
txt.setText(message);
}
return contentView;
} ) );
}

View File

@@ -2,7 +2,7 @@
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:radius="90dp" />
<gradient
android:angle="270"
android:endColor="#F2161616"
android:startColor="#F2161616" />
android:angle="180"
android:endColor="#FF3F4057"
android:startColor="#FF5E6079" />
</shape>

View File

@@ -1,27 +1,28 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/module_commons_toast_bkg"
android:orientation="vertical">
android:paddingTop="@dimen/module_commons_toast_marginTop"
android:paddingBottom="@dimen/module_commons_toast_marginBottom"
android:paddingLeft="@dimen/module_commons_toast_marginLeft"
android:paddingRight="@dimen/module_commons_toast_marginRight">
<TextView
android:id="@+id/module_commons_toast_msg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="@dimen/module_commons_toast_marginLeft"
android:layout_marginTop="@dimen/module_commons_toast_marginTop"
android:layout_marginRight="@dimen/module_commons_toast_marginRight"
android:layout_marginBottom="@dimen/module_commons_toast_marginBottom"
android:ellipsize="end"
android:gravity="center"
android:drawablePadding="@dimen/module_commons_toast_space_between_icon_and_msg"
android:maxWidth="@dimen/module_commons_toast_maxWidth"
android:maxLength="20"
android:maxLines="1"
android:minWidth="@dimen/module_commons_toast_minWidth"
android:textColor="#FFFF"
android:textSize="@dimen/module_commons_toast_textSize"
tools:text="测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试" />
</LinearLayout>
tools:text="测试测试测试" />
<!-- android:minWidth="@dimen/module_commons_toast_minWidth"-->
</FrameLayout>

View File

@@ -0,0 +1,39 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/module_commons_toast_bkg"
android:orientation="horizontal"
android:paddingLeft="@dimen/module_commons_toast_with_left_drawable_marginLeft"
android:paddingTop="@dimen/module_commons_toast_with_left_drawable_marginTop"
android:paddingRight="@dimen/module_commons_toast_marginRight"
android:paddingBottom="@dimen/module_commons_toast_with_left_drawable_marginBottom">
<ImageView
android:id="@+id/module_commons_toast_left_drawable"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" />
<Space
android:layout_width="@dimen/module_commons_toast_space_between_icon_and_msg"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/module_commons_toast_msg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:drawablePadding="@dimen/module_commons_toast_space_between_icon_and_msg"
android:ellipsize="end"
android:gravity="center"
android:maxWidth="@dimen/module_commons_toast_maxWidth"
android:maxLength="20"
android:maxLines="1"
android:textColor="#FFFF"
android:textSize="@dimen/module_commons_toast_textSize"
tools:text="测试测试测试" />
<!-- android:minWidth="@dimen/module_commons_toast_minWidth"-->
</LinearLayout>

View File

@@ -1,13 +1,18 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="module_commons_toast_marginLeft">50px</dimen>
<dimen name="module_commons_toast_marginRight">50px</dimen>
<dimen name="module_commons_toast_marginLeft">63px</dimen>
<dimen name="module_commons_toast_with_left_drawable_marginLeft">30px</dimen>
<dimen name="module_commons_toast_marginRight">63px</dimen>
<dimen name="module_commons_toast_marginTop">32px</dimen>
<dimen name="module_commons_toast_marginBottom">32px</dimen>
<dimen name="module_commons_toast_with_left_drawable_marginTop">19px</dimen>
<dimen name="module_commons_toast_with_left_drawable_marginBottom">19px</dimen>
<dimen name="module_commons_toast_textSize">40px</dimen>
<dimen name="module_commons_toast_minWidth">698px</dimen>
<dimen name="module_commons_toast_maxWidth">900px</dimen>
<dimen name="module_commons_toast_y_offset">130px</dimen>
<dimen name="module_commons_toast_icon_width">94px</dimen>
<dimen name="module_commons_toast_space_between_icon_and_msg">21px</dimen>
</resources>

View File

@@ -1,11 +1,16 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="module_commons_toast_marginLeft">30px</dimen>
<dimen name="module_commons_toast_marginRight">30px</dimen>
<dimen name="module_commons_toast_marginLeft">33px</dimen>
<dimen name="module_commons_toast_with_left_drawable_marginLeft">17px</dimen>
<dimen name="module_commons_toast_marginRight">33px</dimen>
<dimen name="module_commons_toast_marginTop">16px</dimen>
<dimen name="module_commons_toast_marginBottom">16px</dimen>
<dimen name="module_commons_toast_with_left_drawable_marginTop">10px</dimen>
<dimen name="module_commons_toast_with_left_drawable_marginBottom">10px</dimen>
<dimen name="module_commons_toast_textSize">22px</dimen>
<dimen name="module_commons_toast_minWidth">371px</dimen>
<dimen name="module_commons_toast_maxWidth">500px</dimen>
<dimen name="module_commons_toast_y_offset">72px</dimen>
<dimen name="module_commons_toast_icon_width">50px</dimen>
<dimen name="module_commons_toast_space_between_icon_and_msg">10px</dimen>
</resources>

View File

@@ -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;
}
}

View File

@@ -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;
}
}
}

View File

@@ -55,6 +55,8 @@ import com.mogo.service.statusmanager.StatusDescriptor;
import com.mogo.service.windowview.IMogoTopViewStatusListener;
import com.mogo.utils.LaunchUtils;
import com.mogo.utils.ResourcesHelper;
import com.mogo.utils.TipDrawable;
import com.mogo.utils.TipToast;
import com.mogo.utils.UiThreadHandler;
import com.mogo.utils.glide.GlideApp;
import com.mogo.utils.logger.Logger;
@@ -595,6 +597,19 @@ public class EntranceFragment extends MvpFragment<EntranceView, EntrancePresente
tv.setText("entrance add");
mApis.getEntranceButtonController().addBottomLayerView(tv, 50, 50);
});
findViewById(R.id.btnShowTextTip).setOnClickListener(v-> TipToast.tip("分享成功"));
findViewById(R.id.btnShowDrawableTip).setOnClickListener(v->{
TipDrawable drawable =
new TipDrawable(getResources().getDrawable(R.drawable.model_ext_default_user_head), 150, 150);
TipToast.tip("分享成功",drawable);
});
findViewById(R.id.btnShowDrawableTipNoSize).setOnClickListener(v->{
TipDrawable drawable =
new TipDrawable(getResources().getDrawable(R.drawable.model_ext_default_user_head));
TipToast.tip("分享成功",drawable);
});
}
@Override

View File

@@ -174,6 +174,7 @@
android:visibility="gone" />
</LinearLayout>
<Button
android:id="@+id/btnDebugCtrlTopView"
android:layout_width="wrap_content"
@@ -210,11 +211,37 @@
app:layout_constraintBottom_toTopOf="@id/btnDebugCtrlNaviView"
app:layout_constraintLeft_toLeftOf="parent" />
<Button
android:id="@+id/btnShowTextTip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="显示文字tip"
android:textSize="20sp"
app:layout_constraintBottom_toTopOf="@id/btnDebugAddBottomLayerView"
app:layout_constraintLeft_toLeftOf="parent" />
<Button
android:id="@+id/btnShowDrawableTip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="显示图文tip"
android:textSize="20sp"
app:layout_constraintBottom_toTopOf="@id/btnShowTextTip"
app:layout_constraintLeft_toLeftOf="parent" />
<Button
android:id="@+id/btnShowDrawableTipNoSize"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="显示图文tip没有大小"
android:textSize="20sp"
app:layout_constraintBottom_toTopOf="@id/btnShowDrawableTip"
app:layout_constraintLeft_toLeftOf="parent" />
<androidx.constraintlayout.widget.Group
android:id="@+id/groupTopViewDebug"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"
app:constraint_referenced_ids="btnDebugCtrlNaviView,btnDebugCtrlSubView,btnDebugCtrlTopView,btnDebugAddBottomLayerView" />
app:constraint_referenced_ids="btnShowDrawableTipNoSize,btnShowDrawableTip,btnShowTextTip,btnDebugCtrlNaviView,btnDebugCtrlSubView,btnDebugCtrlTopView,btnDebugAddBottomLayerView" />
</androidx.constraintlayout.widget.ConstraintLayout>

View File

@@ -94,6 +94,7 @@ import com.mogo.service.module.MogoAction;
import com.mogo.service.statusmanager.IMogoStatusManager;
import com.mogo.service.windowview.IMogoTopViewStatusListener;
import com.mogo.utils.NetworkUtils;
import com.mogo.utils.TipDrawable;
import com.mogo.utils.TipToast;
import com.mogo.utils.WorkThreadHandler;
import com.mogo.utils.logger.Logger;