/
ListViewActivity.java
131 lines (111 loc) · 4.84 KB
/
ListViewActivity.java
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
/*
* Copyright 2015 AndroidPlot.com
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.androidplot.demos;
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import androidx.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import com.androidplot.Plot;
import com.androidplot.ui.SeriesBundle;
import com.androidplot.util.PixelUtils;
import com.androidplot.xy.CatmullRomInterpolator;
import com.androidplot.xy.LineAndPointFormatter;
import com.androidplot.xy.SimpleXYSeries;
import com.androidplot.xy.XYPlot;
import com.androidplot.xy.XYSeries;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public class ListViewActivity extends Activity {
private static final int NUM_PLOTS = 10;
private static final int NUM_POINTS_PER_SERIES = 10;
private static final int NUM_SERIES_PER_PLOT = 5;
private ListView lv;
private List<List<SeriesBundle<XYSeries, LineAndPointFormatter>>> seriesData = new ArrayList<>(NUM_PLOTS);
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listview_example);
PixelUtils.init(this);
generateData();
lv = (ListView) findViewById(R.id.listView1);
lv.setAdapter(new MyViewAdapter(getApplicationContext(), R.layout.listview_example_item, null));
}
protected void generateData() {
Random generator = new Random();
for(int i = 0; i < NUM_PLOTS; i++) {
List<SeriesBundle<XYSeries, LineAndPointFormatter>> seriesList
= new ArrayList<>(NUM_SERIES_PER_PLOT);
for (int k = 0; k < NUM_SERIES_PER_PLOT; k++) {
ArrayList<Number> nums = new ArrayList<>();
for (int j = 0; j < NUM_POINTS_PER_SERIES; j++) {
nums.add(generator.nextFloat());
}
double rl = Math.random();
double gl = Math.random();
double bl = Math.random();
double rp = Math.random();
double gp = Math.random();
double bp = Math.random();
LineAndPointFormatter lpf = new LineAndPointFormatter(
Color.rgb(Double.valueOf(rl * 255).intValue(),
Double.valueOf(gl * 255).intValue(), Double.valueOf(bl * 255).intValue()),
Color.rgb(Double.valueOf(rp * 255).intValue(),
Double.valueOf(gp * 255).intValue(), Double.valueOf(bp * 255).intValue()),
null, null);
// for fun, configure interpolation on the formatter:
lpf.setInterpolationParams(
new CatmullRomInterpolator.Params(20, CatmullRomInterpolator.Type.Centripetal));
seriesList.add(new SeriesBundle<XYSeries, LineAndPointFormatter>(
new SimpleXYSeries(nums, SimpleXYSeries.ArrayFormat.Y_VALS_ONLY, "S" + k),
lpf));
}
seriesData.add(seriesList);
}
}
class MyViewAdapter extends ArrayAdapter<View> {
public MyViewAdapter(Context context, int resId, List<View> views) {
super(context, resId, views);
}
@Override
public int getCount() {
return NUM_PLOTS;
}
@NonNull
@Override
public View getView(int pos, View convertView, @NonNull ViewGroup parent) {
LayoutInflater inf = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = convertView;
if (v == null) {
v = inf.inflate(R.layout.listview_example_item, parent, false);
}
Plot p = (XYPlot) v.findViewById(R.id.xyplot);
p.clear();
p.getTitle().setText("plot" + pos);
List<SeriesBundle<XYSeries, LineAndPointFormatter>> thisSeriesList = seriesData.get(pos);
for(SeriesBundle<XYSeries, LineAndPointFormatter> sf : thisSeriesList) {
p.addSeries(sf.getSeries(), sf.getFormatter());
}
p.redraw();
return v;
}
}
}