Repository navigation
Expand file tree
/
Copy pathResultGridAdapter.cs
More file actions
181 lines (155 loc) · 6.41 KB
/
Copy pathResultGridAdapter.cs
File metadata and controls
181 lines (155 loc) · 6.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
using System;
using System.Collections.Generic;
using Android.Util;
using Android.Views;
using Android.Widget;
using Pawelkleczkowski.CGauge;
namespace BmwDeepObd
{
public class ResultGridAdapter : BaseAdapter<GridResultItem>
{
private const int GaugeBaseSize = 200;
private readonly List<GridResultItem> _items;
public List<GridResultItem> Items => _items;
private readonly Android.App.Activity _context;
public ResultGridAdapter(Android.App.Activity context)
{
_context = context;
_items = new List<GridResultItem>();
}
public override long GetItemId(int position)
{
return position;
}
public override GridResultItem this[int position] => _items[position];
public override int Count => _items.Count;
public override bool IsEnabled(int position)
{
return false;
}
public override View GetView(int position, View convertView, ViewGroup parent)
{
GridResultItem item = _items[position];
View view = convertView;
if (convertView == null || convertView.Id != item.ResourceId)
{
view = _context.LayoutInflater.Inflate(item.ResourceId, null);
view.Id = item.ResourceId;
}
int gaugePadding = 20 * item.GaugeSize / GaugeBaseSize;
int gaugeInnerSize = item.GaugeSize - (2 * gaugePadding);
CustomGauge customGauge = view.FindViewById<CustomGauge>(Resource.Id.custom_gauge);
if (customGauge != null)
{
try
{
ViewGroup.LayoutParams layoutParams = customGauge.LayoutParameters;
layoutParams.Width = gaugeInnerSize;
layoutParams.Height = gaugeInnerSize;
customGauge.LayoutParameters = layoutParams;
customGauge.SetPadding(gaugePadding, gaugePadding, gaugePadding, gaugePadding);
int strokeWidth = string.Compare(customGauge.StrokeCap, "BUTT", StringComparison.OrdinalIgnoreCase) == 0 ? 20 : 10;
customGauge.StrokeWidth = (float)strokeWidth * gaugeInnerSize / GaugeBaseSize;
customGauge.Init();
if (item.GaugeColor.HasValue)
{
Android.Graphics.Color gaugeColor = item.GaugeColor.Value;
customGauge.PointStartColor = gaugeColor;
customGauge.PointEndColor = gaugeColor;
}
int gaugeScale = customGauge.EndValue;
double range = item.MaxValue - item.MinValue;
int gaugeValue = 0;
if (Math.Abs(range) > 0.00001)
{
gaugeValue = (int)((item.Value - item.MinValue) / range * gaugeScale);
if (gaugeValue < 0)
{
gaugeValue = 0;
}
else if (gaugeValue > gaugeScale)
{
gaugeValue = gaugeScale;
}
}
customGauge.Value = gaugeValue;
}
catch (Exception)
{
// ignored
}
}
TextView textViewGaugeValue = view.FindViewById<TextView>(Resource.Id.gauge_value);
if (textViewGaugeValue != null)
{
try
{
RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams)textViewGaugeValue.LayoutParameters;
if (customGauge != null)
{
layoutParams.BottomMargin = 80 * gaugeInnerSize / GaugeBaseSize;
}
else
{
layoutParams.Height = 120 * gaugeInnerSize / GaugeBaseSize;
layoutParams.Width = gaugeInnerSize;
}
textViewGaugeValue.LayoutParameters = layoutParams;
textViewGaugeValue.SetTextSize(ComplexUnitType.Px, (float)30 * gaugeInnerSize / GaugeBaseSize);
textViewGaugeValue.Text = item.ValueText;
}
catch (Exception)
{
// ignored
}
}
TextView textViewGaugeName = view.FindViewById<TextView>(Resource.Id.gauge_name);
if (textViewGaugeName != null)
{
try
{
RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams)textViewGaugeName.LayoutParameters;
if (customGauge != null)
{
layoutParams.TopMargin = -30 * gaugeInnerSize / GaugeBaseSize;
}
else
{
layoutParams.Height = 80 * gaugeInnerSize / GaugeBaseSize;
layoutParams.Width = gaugeInnerSize;
}
textViewGaugeName.LayoutParameters = layoutParams;
textViewGaugeName.SetTextSize(ComplexUnitType.Px, (float)20 * gaugeInnerSize / GaugeBaseSize);
textViewGaugeName.Text = item.Name;
}
catch (Exception)
{
// ignored
}
}
return view;
}
}
public class GridResultItem
{
public GridResultItem(int resourceId, string name, string valueText, double minValue, double maxValue, double value, int gaugeSize, Android.Graphics.Color? gaugeColor)
{
ResourceId = resourceId;
Name = name;
ValueText = valueText;
MinValue = minValue;
MaxValue = maxValue;
Value = value;
GaugeSize = gaugeSize;
GaugeColor = gaugeColor;
}
public int ResourceId { get; }
public string Name { get; }
public string ValueText { get; }
public double MinValue { get; }
public double MaxValue { get; }
public double Value { get; set; }
public int GaugeSize { get; set; }
public Android.Graphics.Color? GaugeColor { get; set; }
}
}