xamarin.form image

Image的重要属性Aspect,可以设置3个值,AspectFill,AspectFit,Fill 其中AspectFill代表原比例放大并填充至整个Image,AspectFit为等比例缩放放到Image中可能其中Image会有空白的地方,Fill为填满整个Image但是图片可能会失贞

Image所使用图片的来源在Android端放在Resources下面的drawable,drawable-hdpi,drawable-xhdpi,drawable-xxhdpi,drawable-xxxhdpi下,其中drawable是图片最小的那个,如果你的图片越来越大,依次放在后面的文件夹中,把图片复制到对应的文件夹中,并且设置便宜方式为AndroidResource

IOS端只有Resources文件夹,如果图片有多个大小的版本,可以使用demo.jpg和[email protected][email protected]来表示,通过后面增加@2x来表示更大的图片,然后编译方式更改为BundleResource就可以使用了

如果你是在form中使用可以使用

如果使用代码创建可以使用Image.Source = ImageSource.FromFile("xxx.jpg");

这种方式需要Android和IOS都要放置一份图片,PCL可以解决这个问题

EmbeddedResource的编译方式,然后代码中写Image.Source = ImageSource.FromResource("命名空间.文件夹.文件名.文件格式");记住嵌入的资源一定要加命名空间,否则无法加载

使用URI方式加载网络图片

image.Source = new UriImageSource()加载网络资源并启用超时响应
            {
                Uri = new Uri("http://image.mamicode.com/info/201811/20181125190339071494.png"),
                CacheValidity= new TimeSpan(5,0,0,0),
                CachingEnabled = true
            };

  使用CircleImage显示圆形图片,在Android和IOS中将矩形图片显示为圆形不太容易,可以直接使用nuget包中的Xam.Plugins.Forms.ImageCircle来完成原型图片的显示,在form的项目中添加Xam.Plugins.Forms.ImageCircle,在Android的MainActivity.cs的OnCreate方法中加入ImageCircleRenderer.Init();  然后在IOS的AppDelegate.cs文件中的FinishedLaunching方法中加入ImageCircleRenderer.Init();  

然后在form的xaml文件中声明命名空间xmlns:custom="clr-namespace:ImageCircle.Forms.Plugin.Abstractions;assembly=ImageCircle.Forms.Plugin.Abstractions" 

然后创建图片控件<custom:CircleImage Source="BG.png" Aspect="AspectFill" />

猜你喜欢

转载自www.cnblogs.com/jiecaoge/p/10032440.html