title author date CreateTime categories
Xamarin Forms 进度条控件
lindexi
2018-2-13 17:23:3 +0800
2018-2-13 17:23:3 +0800

本文翻译:http://xamlnative.com/2016/04/14/xamarin-forms-a-simple-circular-progress-control/ 里面都是胡说的,如果看不懂可以联系邮箱
源代码:https://github.com/billreiss/xamlnative/tree/master/XamarinForms/CircularProgress
最近作者需要做一个简单的圆形的等待控件在一个Xamarin Forms应用,效果可以看

看起来很容易做,不知道怎么微软就没有弄个这么好看,微软没有,我们来直接做,看起来这个很简单

原来的进度条是一个线,没有UWP那个ring,我要做一个,可以使用本地控制、自定义渲染器渲染、使用组件里面弄很多我之前做的、到Nuget找,这些都觉得不是我要的。

看到他们没有,我就很高兴,我可以做一个很厉害的,自然这里我是原文的那个,写了Xaml的大神

我首先拿出一个本子,我应该弄矢量图形,在Xamarin原生还没有,我会为每个平台定制渲染,所以他不支持我不能使用,我想到使用图片,矢量图片,既然想要图片我如何让很多图片看起来是一个

我想到简单使用两图,实际对称两图是表示4图,不停覆盖的两个图片表示进度,两个图片颜色不同

图片可以在:https://github.com/billreiss/xamlnative/tree/master/XamarinForms/CircularProgress/CircularProgress/CircularProgress.Droid/Resources/drawable

两个保存格式Png图片,一个图表示0-50%,我们叫第一图“completed”,第二“pending”,颜色深的是第一,进度我们需要一个completed,两个pending,我们先放completed,然后在它上面放pending,在pending对面放pending,第一个图在代码叫“progress1”,第二“background1”,第二个覆盖第一个,第三个pending旋转180,总的一个蓝色圆,这是0%

25%:我们旋转pending第二个,可以让看到下面的图,这个我们覆盖原来的pending因为颜色一样,所以我们就可以看到25%

50%:我们需要改变,两个completed,一个pending,pending覆盖completed,但是只是覆盖一个,他们的层次:

  • completed
  • pending
  • completed

可以让pending覆盖右边的completed,超过50%让pending右旋

如果觉得上面说的还是不知道,可以看代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms; namespace CircularProgress
{
public class CircularProgressControl : Grid
{
View progress1;
View progress2;
View background1;
View background2;
public CircularProgressControl()
{
progress1 = CreateImage("progress_done");
background1 = CreateImage("progress_pending");
background2 = CreateImage("progress_pending");
progress2 = CreateImage("progress_done");
HandleProgressChanged(1, 0);
} private View CreateImage(string v1)
{
var img = new Image();
img.Source = ImageSource.FromFile(v1 + ".png");
this.Children.Add(img);
return img;
} public static BindableProperty ProgressProperty =
BindableProperty.Create("Progress", typeof(double), typeof(CircularProgressControl), 0d, propertyChanged: ProgressChanged); private static void ProgressChanged(BindableObject bindable, object oldValue, object newValue)
{
var c = bindable as CircularProgressControl;
c.HandleProgressChanged(Clamp((double)oldValue, 0, 1), Clamp((double)newValue, 0, 1));
} static double Clamp(double value, double min, double max)
{
if (value <= max && value >= min) return value;
else if (value > max) return max;
else return min;
} private void HandleProgressChanged(double oldValue, double p)
{
if (p < .5)
{
if (oldValue >= .5)
{
// this code is CPU intensive so only do it if we go from >=50% to <50%
background1.IsVisible = true;
progress2.IsVisible = false;
background2.Rotation = 180;
progress1.Rotation = 0;
}
double rotation = 360 * p;
background1.Rotation = rotation;
}
else
{
if (oldValue < .5)
{
// this code is CPU intensive so only do it if we go from <50% to >=50%
background1.IsVisible = false;
progress2.IsVisible = true;
progress1.Rotation = 180;
}
double rotation = 360 * p;
background2.Rotation = rotation;
}
} public double Progress
{
get { return (double)this.GetValue(ProgressProperty); }
set { SetValue(ProgressProperty, value); }
}
}
}

我们需要把图片放在不同平台的文件夹,ios放在Resources文件夹,Android放在 AndroidResource

我们把控件放MainPage.xaml

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="CircularProgress.MainPage"
xmlns:local="clr-namespace:CircularProgress" BackgroundColor="White">
<Grid>
<local:CircularProgressControl x:Name="progressControl" Progress="0" HorizontalOptions="Center" VerticalOptions="Center" WidthRequest="60" HeightRequest="60"/>
</Grid>
</ContentPage>

我们让time进度加0.1每0.02s

namespace CircularProgress
{
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
Xamarin.Forms.Device.StartTimer(TimeSpan.FromSeconds(.02), OnTimer);
} private bool OnTimer()
{
var progress = (progressControl.Progress + .1) ;
if (progress > 1) progress = 0;
progressControl.Progress = progress;
return true;
}
}
}

不使用自定义渲染,可以在各个平台没有使用厉害的技术覆盖两个图做出从0-100%,可以使用不同角度表示0.001

本文:http://blog.csdn.net/lindexi_gd

2018-2-13-Xamarin-Forms-进度条控件的更多相关文章

  1. Xamarin Forms 进度条控件

    本文翻译:http://xamlnative.com/2016/04/14/xamarin-forms-a-simple-circular-progress-control/ 里面都是胡说的,如果看不 ...

  2. Photoshop和WPF双剑配合,打造炫酷个性的进度条控件

    现在如果想打造一款专业的App,UI的设计和操作的简便性相当重要.UI设计可以借助Photoshop或者AI等设计工具,之前了解到WPF设计工具Expression Blend可以直接导入PSD文件或 ...

  3. [转载]ExtJs4 笔记(8) Ext.slider 滚轴控件、 Ext.ProgressBar 进度条控件、 Ext.Editor 编辑控件

    作者:李盼(Lipan)出处:[Lipan] (http://www.cnblogs.com/lipan/)版权声明:本文的版权归作者与博客园共有.转载时须注明本文的详细链接,否则作者将保留追究其法律 ...

  4. ExtJs4 笔记(8) Ext.slider 滚轴控件、 Ext.ProgressBar 进度条控件、 Ext.Editor 编辑控件

    本篇要登场的有三个控件,分别是滚轴控件.进度条控件和编辑控件. 一.滚轴控件 Ext.slider 1.滚轴控件的定义 下面我们定义三个具有代表意义滚轴控件,分别展示滚轴横向.纵向,以及单值.多值选择 ...

  5. 为OLED屏添加GUI支持6:进度条控件

    为OLED屏添加GUI支持6:进度条控件 本文博客链接:http://blog.csdn.net/jdh99,作者:jdh,转载请注明. 环境: 主机:WIN10 开发环境:MDK5.13 MCU:S ...

  6. 用 CALayer 定制下载进度条控件

    // // RPProgressView.h // CALayer定制下载进度条控件 // // Created by RinpeChen on 16/1/2. // Copyright © 2016 ...

  7. 【转】用emWin进度条控件做个表盘控件,效果不错

    @2018-08-09 用emWin进度条控件做个表盘控件,效果不错

  8. CProgressCtrl进度条控件实现进度滚动效果

    关于CProgressCtrl 控件的基本操作网上有很多资料,可我想实现进度条中进度滚动效果,即很多时候程序出现的等待或启动画面,如下图: 实现这个效果的函数为SetMarquee(_In_ BOOL ...

  9. HslControls组件库 工业控件库 曲线控件 时间控件 管道控件 温度计控件 阀门控件 传送带控件 进度条控件 电池控件 数码管控件等等

    本篇博客主要对 HslControls 组件做一个大概的总览介绍,更详细的内容可以参照页面里的子链接,还有github上的源代码,然后进行相关的学习,和使用. Prepare 先从nuget下载到组件 ...

  10. iOS:进度条控件的详细使用

    进度条控件:UIProcessView:UIView   功能:顾名思义,用来显示下载进度或者传输数据进度.   属性: @property(nonatomic) UIProgressViewStyl ...

随机推荐

  1. 用五种不同的布局方式实现“左右300px中间自适应”的效果

    float浮动 <section class="layout float"> <style media="screen"> .layou ...

  2. Minimum Depth of Binary Tree最短深度

    Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...

  3. 谷歌好样的,把 www 也干掉了

    谷歌好样的,把 www 也干掉了 继把 http 干掉后,这次 Chrome 76 连 https 和 www 都一起干掉了. 喜欢简洁,但这个功能演化过程可不简单. 最早觉得把 http 干掉很不方 ...

  4. python 嵌套列表

  5. @noi.ac - 490@ game

    目录 @description@ @solution@ @accepted code@ @details@ @description@ 小 Q 和小 T 正在玩一种双人游戏.m 张木牌从左往右排成一排 ...

  6. CSS的固定定位

    将元素放置在浏览器窗口的固定位置,拖拽窗口时元素位置不变. 类似语法: div{ position:fixed; top:0px; left:0px; right:0px; bottom:0px; }

  7. 【a503】圆排列问题

    Time Limit: 1 second Memory Limit: 32 MB [问题描述] 给定n个大小不等的圆c1,c2,...., cn,现要将这n个圆排列进一个矩形框中,且要求各圆与矩形框的 ...

  8. laravel validate 设置为中文(验证提示为中文)

    把 resources\lang 下en 的文件夹 复制在同一目录并改名为 zn 把zn 中的 validation.php文件修改为 https://laravel-china.org/articl ...

  9. libsvm的安装,数据格式,常见错误,grid.py参数选择,c-SVC过程,libsvm参数解释,svm训练数据,libsvm的使用详解,SVM核函数的选择

    直接conda install libsvm安装的不完整,缺几个.py文件. 第一种安装方法: 下载:http://www.csie.ntu.edu.tw/~cjlin/cgi-bin/libsvm. ...

  10. 脑残的设计--- 视图(view)里面包含order by

    2015/05/26 更新 今天又遇到了类似问题...尼玛无语了 编码规范啊 !!! 今天有个兄弟跟我说sql跑得太慢了,让我看看.sql如下: SELECT rownum row_num, pv.v ...