Delphi XE10写的转换图像文件大小,用于生成安卓和IOS的小图标和启动画面

记不清是从哪年开始学Delphi的了,可能是从Delphi 1.0开始用的,一直用到D7,然后工作变动,不再写经常代码,但一直保持关注,最近XE10的出现,让我对Delphi移动开发产生了兴趣。学习时发现移动开发竟然需要准备20多个不同分辨率的图片,用来适应不同分辨率的设备,年纪大了,越来越懒了,干脆写点代码,做一个大图然后再缩小吧,折腾了两个多小时,总算用FMX写了出来。VCL时代是想用API就用API,在移动平台下的API可不是随便调用的,FMX下的类、属性、方法与VCL都不一样,啃HELP文件许久才找到办法。

最主要的区别就是VCL时代用Timage+Canvas,FMX时代是TBitmap+canvas。

代码贴上:

form:
object Form1: TForm1
  Left = 0
  Top = 0
  Caption = 'Resize Image'
  ClientHeight = 713
  ClientWidth = 624
  FormFactor.Width = 320
  FormFactor.Height = 480
  FormFactor.Devices = [Desktop]
  DesignerMasterStyle = 0
  object ImageViewer1: TImageViewer
    Anchors = [akLeft, akTop, akRight, akBottom]
    ClipParent = True
    Position.X = 8.000000000000000000
    Position.Y = 152.000000000000000000
    Size.Width = 601.000000000000000000
    Size.Height = 513.000000000000000000
    Size.PlatformDefault = False
    TabOrder = 0
    Viewport.Width = 597.000000000000000000
    Viewport.Height = 509.000000000000000000
  end
  object Label1: TLabel
    Position.X = 8.000000000000000000
    Position.Y = 8.000000000000000000
    Text = 'ICON Maxsize:180'
  end
  object Label2: TLabel
    Position.X = 120.000000000000000000
    Position.Y = 8.000000000000000000
    Size.Width = 169.000000000000000000
    Size.Height = 17.000000000000000000
    Size.PlatformDefault = False
    Text = 'Splash Image MAX:1242x2208'
  end
  object Label3: TLabel
    Position.X = 8.000000000000000000
    Position.Y = 48.000000000000000000
    Size.Width = 113.000000000000000000
    Size.Height = 17.000000000000000000
    Size.PlatformDefault = False
    Text = 'Source image File:'
  end
  object EditSource: TEdit
    Touch.InteractiveGestures = [LongTap, DoubleTap]
    Anchors = [akLeft, akTop, akRight]
    TabOrder = 5
    Position.X = 112.000000000000000000
    Position.Y = 40.000000000000000000
    Size.Width = 409.000000000000000000
    Size.Height = 32.000000000000000000
    Size.PlatformDefault = False
  end
  object ButtonOpen: TButton
    Anchors = [akTop, akRight]
    Position.X = 528.000000000000000000
    Position.Y = 40.000000000000000000
    TabOrder = 6
    Text = 'Open'
    OnClick = ButtonOpenClick
  end
  object RadioButtonIcon: TRadioButton
    GroupName = '1'
    IsChecked = True
    Position.X = 8.000000000000000000
    Position.Y = 24.000000000000000000
    Size.Width = 57.000000000000000000
    Size.Height = 19.000000000000000000
    Size.PlatformDefault = False
    TabOrder = 7
    Text = 'ICON'
  end
  object RadioButtonSplash: TRadioButton
    GroupName = '1'
    Position.X = 120.000000000000000000
    Position.Y = 24.000000000000000000
    TabOrder = 8
    Text = 'Splash Image'
  end
  object Label4: TLabel
    Position.X = 8.000000000000000000
    Position.Y = 72.000000000000000000
    Size.Width = 97.000000000000000000
    Size.Height = 17.000000000000000000
    Size.PlatformDefault = False
    Text = 'Output directory:'
  end
  object EditOutput: TEdit
    Touch.InteractiveGestures = [LongTap, DoubleTap]
    Anchors = [akLeft, akTop, akRight]
    TabOrder = 10
    Position.X = 112.000000000000000000
    Position.Y = 72.000000000000000000
    Size.Width = 409.000000000000000000
    Size.Height = 32.000000000000000000
    Size.PlatformDefault = False
  end
  object ButtonOutPut: TButton
    Anchors = [akTop, akRight]
    Position.X = 528.000000000000000000
    Position.Y = 72.000000000000000000
    TabOrder = 12
    Text = 'Out Dir'
    OnClick = ButtonOutPutClick
  end
  object Button3: TButton
    Position.X = 392.000000000000000000
    Position.Y = 104.000000000000000000
    Size.Width = 216.000000000000000000
    Size.Height = 25.000000000000000000
    Size.PlatformDefault = False
    TabOrder = 13
    Text = 'Convert'
    OnClick = Button3Click
  end
  object LabelFileInfo: TLabel
    Anchors = [akLeft, akTop, akRight]
    Position.X = 8.000000000000000000
    Position.Y = 136.000000000000000000
    Size.Width = 577.000000000000000000
    Size.Height = 17.000000000000000000
    Size.PlatformDefault = False
    Text = 'File:'
  end
  object OpenDialog1: TOpenDialog
    DefaultExt = '*.png'
    Filter = 'PNG FILE|*.png|BMP FILE|*.bmp|JPG FILE|*.jpg'
    Left = 32
    Top = 184
  end
  object SaveDialog1: TSaveDialog
    Left = 32
    Top = 248
  end
  object Label5: TLabel
    Anchors = [akTop, akRight, akBottom]
    Position.X = 8.000000000000000000
    Position.Y = 664.000000000000000000
    Size.Width = 625.000000000000000000
    Size.Height = 57.000000000000000000
    Size.PlatformDefault = False
    Text = 
      'Delphi XE10 Artwork full  resolution support. ICON:36,48,72,96,1' +
      '44, 57,60,87,114,120,180,29,48,58,80. Spalsh:426x320,470x320,640' +
      'x480,960x720,320x480,640x960,640x1136,750x1334,1242x2208,2208x12' +
      '42.'
  end
end


Pas:
unit main;


interface


uses
  System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
  FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, FMX.Layouts,
  FMX.ExtCtrls, FMX.StdCtrls, FMX.Edit, FMX.Controls.Presentation;


type
  TForm1 = class(TForm)
    ImageViewer1: TImageViewer;
    Label1: TLabel;
    Label2: TLabel;
    OpenDialog1: TOpenDialog;
    Label3: TLabel;
    EditSource: TEdit;
    ButtonOpen: TButton;
    RadioButtonIcon: TRadioButton;
    RadioButtonSplash: TRadioButton;
    Label4: TLabel;
    EditOutput: TEdit;
    SaveDialog1: TSaveDialog;
    ButtonOutPut: TButton;
    Button3: TButton;
    LabelFileInfo: TLabel;
    Label5: TLabel;
    procedure ButtonOpenClick(Sender: TObject);
    procedure ButtonOutPutClick(Sender: TObject);
    procedure Button3Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;
const
  iconSize:array[1..15] of integer =(36,48,72,96,144, 57,60,87,114,120,180,29,48,58,80);
  imageSize:array[1..10,1..2] of integer = ((426,320),(470,320),(640,480),(960,720),(320,480),(640,960),(640,1136),(750,1334),(1242,2208),(2208,1242));
var
  Form1: TForm1;


implementation


{$R *.fmx}
{$R *.iPhone47in.fmx IOS}
{$R *.LgXhdpiPh.fmx ANDROID}


procedure TForm1.ButtonOpenClick(Sender: TObject);
begin
  if opendialog1.Execute then
  begin
    ImageViewer1.Bitmap.LoadFromFile(OpenDialog1.FileName);
    EditSource.Text := OpenDialog1.FileName;
    LabelFileInfo.Text :='File: '+EditSource.Text
    +' Size(width x height): '
    +inttostr(ImageViewer1.Bitmap.Image.Width)+' x '+inttostr( ImageViewer1.Bitmap.Image.Height);
  end;
end;


procedure TForm1.ButtonOutPutClick(Sender: TObject);
begin
  if Length(OpenDialog1.FileName)>0 then
  begin
    SaveDialog1.FileName := OpenDialog1.FileName;
    if SaveDialog1.execute then
    begin
      EditOutPut.Text := ExtractFilePath(SaveDialog1.FileName);
    end;
  end;


end;


procedure TForm1.Button3Click(Sender: TObject);
var img:TBitmap;
    srcRect: TRectF;
    desRect: TRectF;
    ias,i:integer;
    fPath:String;
    fExt:string;
    fName:String;
begin
  srcRect:=Trectf.Create(0,0,ImageViewer1.Bitmap.Width,ImageViewer1.Bitmap.Height);
  if Length(editSource.Text)<1 then
  begin
    ButtonOpenClick(sender);
  end else
  begin
    if Length(editOutPut.Text)<1 then
    begin
      ButtonOutputClick(sender);
    end
    else
    begin
      fPath := ExtractFilePath(editOutput.Text);
      fExt := ExtractFileExt(editSource.Text);
      fName := ExtractFileName(editSource.Text);
      img := TBitmap.Create;
      if radiobuttonIcon.IsChecked then
      begin
        ias:=high(iconsize);
        for i := 1 to ias do
        begin
          desRect := Trectf.Create(0,0, iconsize[i], iconsize[i]);
          img.Width :=iconsize[i];
          img.Height :=iconsize[i];
          img.Canvas.BeginScene;
          img.Canvas.DrawBitmap(ImageViewer1.Bitmap,srcRect,DesRect,100);
          img.Canvas.EndScene;
          img.SaveToFile(fPath+fName+'.'+inttostr(iconsize[i])+fExt);
          desRect.Empty;
         end;
         Showmessage('RIGHT!');
       end
       else
       begin
         ias:=high(imagesize);
        for i := 1 to ias do
        begin
          desRect := Trectf.Create(0,0, imagesize[i][1], imagesize[i][2]);
          img.Width :=imagesize[i][1];
          img.Height :=imagesize[i][2];
          img.Canvas.BeginScene;
          img.Canvas.DrawBitmap(ImageViewer1.Bitmap,srcRect,DesRect,100);
          img.Canvas.EndScene;
          img.SaveToFile(fPath+fName+'.'+inttostr(imagesize[i][1])+'x'+inttostr(imagesize[i][2])+fExt);
          desRect.Empty;
         end;
         Showmessage('RIGHT!');
       end;
     img.Destroy;
    end;
  end;
end;


end.

运行界面:

Delphi XE10写的转换图像文件大小,用于生成安卓和IOS的小图标和启动画面_第1张图片


第一个XE10小程序就这样完工。

你可能感兴趣的:(Delphi)