转:u_Toast
unit u_Toast;
interface
uses
System.SysUtils, System.Types, System.Classes, FMX.Graphics,FMX.Surfaces
{$IFDEF ANDROID}
, FMX.Helpers.Android, Androidapi.JNI.app, Androidapi.JNIBridge,
Androidapi.JNI.JavaTypes, Androidapi.Helpers, Androidapi.JNI.Widget,
Androidapi.JNI.GraphicsContentViewText
{$ENDIF}
;
type
/// <summary>
/// Toast显示时间
/// </summary>
TToastLength = record
const
ShortToast = 0;
LongToast = 1;
end;
/// <summary>
/// Toast显示位置
/// </summary>
TGravity = record
const
AXIS_CLIP = 8;
AXIS_PULL_AFTER = 4;
AXIS_PULL_BEFORE = 2;
AXIS_SPECIFIED = 1;
AXIS_X_SHIFT = 0;
AXIS_Y_SHIFT = 4;
BOTTOM = 80;
CENTER = 17;
CENTER_HORIZONTAL = 1;
CENTER_VERTICAL = 16;
CLIP_HORIZONTAL = 8;
CLIP_VERTICAL = 128;
DISPLAY_CLIP_HORIZONTAL = 16777216;
DISPLAY_CLIP_VERTICAL = 268435456;
&END = 8388613;
FILL = 119;
FILL_HORIZONTAL = 7;
FILL_VERTICAL = 112;
HORIZONTAL_GRAVITY_MASK = 7;
LEFT = 3;
NO_GRAVITY = 0;
RELATIVE_HORIZONTAL_GRAVITY_MASK = 8388615;
RELATIVE_LAYOUT_DIRECTION = 8388608;
RIGHT = 5;
START = 8388611;
TOP = 48;
VERTICAL_GRAVITY_MASK = 112;
end;
/// <summary>
/// 显示安卓原生Toast
/// </summary>
/// <param name="msg">需要显示的内容</param>
/// <param name="duration">显示时间长短</param>
procedure Toast(const msg: string;
duration: Integer = TToastLength.ShortToast); overload;
/// <summary>
/// 显示安卓原生Toast
/// </summary>
/// <param name="msg">需要显示的内容</param>
/// <param name="duration">显示时间长短</param>
/// <param name="gravity">显示的位置</param>
/// <param name="xOffset,yOffset">坐标设置</param>
procedure Toast(const msg: string; duration: Integer; gravity: Integer;
xOffset: Integer; yOffset: Integer); overload;
/// <summary>
/// 显示安卓原生Toast 有图片的
/// </summary>
/// <param name="msg">需要显示的内容</param>
/// <param name="duration">显示时间长短</param>
/// <param name="gravity">显示的位置</param>
/// <param name="xOffset,yOffset">坐标设置</param>
/// <param name="vBitmap">需要显示的图片</param>
procedure Toast(const msg: string; duration: Integer; gravity: Integer;
xOffset: Integer; yOffset: Integer; vBitmap: TBitmap); overload;
implementation
{$IFDEF ANDROID}
function BitmapToJBitmap(Bmp: TBitmap): JBitmap;
var
mBitmap: JBitmap;
Surface: TBitmapSurface;
begin
Surface := TBitmapSurface.Create;
Surface.Assign(Bmp);
mBitmap := TJBitmap.JavaClass.createBitmap(Bmp.Width, Bmp.Height,
TJBitmap_Config.JavaClass.ARGB_8888);
if SurfaceToJBitmap(Surface, mBitmap) then
Result := mBitmap;
end;
{$ENDIF}
procedure Toast(const msg: string; duration: Integer = TToastLength.ShortToast);
begin
{$IFDEF ANDROID}
CallInUiThread(
procedure
begin
TJToast.JavaClass.makeText(TAndroidHelper.Activity,
StrToJCharSequence(msg), duration).show;
end);
{$ENDIF}
end;
procedure Toast(const msg: string; duration: Integer; gravity: Integer;
xOffset: Integer; yOffset: Integer);
begin
Toast(msg, duration, gravity, xOffset, yOffset, nil);
end;
procedure Toast(const msg: string; duration: Integer; gravity: Integer;
xOffset: Integer; yOffset: Integer; vBitmap: TBitmap);
begin
{$IFDEF ANDROID}
CallInUiThread(
procedure
var
vToast: JToast;
vImageView: JImageView;
vLinearLayout: JLinearLayout;
begin
vToast := TJToast.JavaClass.makeText(TAndroidHelper.Activity,
StrToJCharSequence(msg), duration);
vToast.setGravity(gravity, xOffset, yOffset);
if Assigned(vBitmap) then
begin
// 创建图片显示
vImageView := TjImageView.JavaClass.init(TAndroidHelper.Context);
vImageView.setImageBitmap(BitmapToJBitmap(vBitmap));
vLinearLayout := TJLinearLayout.Wrap(vToast.getView);
vLinearLayout.addView(vImageView, 0);
end;
vToast.show;
end);
{$ENDIF}
end;
end.
————————————————
版权声明:本文为CSDN博主「tanqth」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/tanqth/java/article/details/104728239