对于阅读此帖子的人来说,这应该是一个非常有用的例子。我正在尝试创建一个异步任务,它将占用一些现有的位置,在ImageView中保存图片,并用完全相同的大小,但不同的图片替换它们。
具体来说,我想显示一些静态图片,然后使用来自远程服务器的实时数据更新它们,还可以将它们缩放到当前显示的大小。
从主要活动中,我们只需使用我们想要更新的ImageView调用它:
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
drawPieCharts((ImageView) findViewById(R.id.chart1),(ImageView) findViewById(R.id.chart2),(ImageView) findViewById(R.id.chart3));
}
接下来,我们需要联系服务器,使用我们的图表获取JSON对象,然后更新图像。我担心我对Java和Android的了解不够充分。这是我现在正在进行的热门话题:
public class drawPieCharts(ImageView... imageViews){
int[] COLORS={0xff00ff00,0xff00ee00,0xff00dd00,0xff00cc00,0xff000000};//alpha r g b
int[] slices={300,400,100,200,500};
@Override
protected void onPreExecute(){
//String charts="{\"charts\":[{\"chart\":1, \"colors\":[0,1,2,3,4], \"slices\":[300,400,100,200,500]},{\"chart\":2, \"colors\":[0,1,2,3,4], \"slices\":[200,100,400,200,500]},{\"chart\": 3, \"colors\":[0,1,2,3,4], \"slices\":[200,100,400,200,200]}]}";
try{
JSONObject obj = new JSONObject(charts);
JSONArray jsonArray = obj.optJSONArray("charts");
for(int i=0; i < jsonArray.length(); i++){
JSONObject chartData = jsonArray.getJSONObject(i);
JSONObject chart = chartData.optJSONObject("chart");
JSONArray colors = chartData.optJSONArray("colors");
JSONArray slices = chartData.optJSONArray("slices");
new DrawPieChart(chartViews[chart]);
}
}catch(JSONException e){e.printStackTrace();}
String jsonReply = "";
try {
URL url = new URL(url_to_server);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
InputStream stream = new BufferedInputStream(urlConnection.getInputStream());
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(stream));
StringBuilder builder = new StringBuilder();
String inputString;
while ((inputString = bufferedReader.readLine()) != null) {
builder.append(jsonReply);
}
JSONObject charts = new JSONObject(builder.toString());
JSONObject chart = charts.getJSONObject("chart");
urlConnection.disconnect();
} catch (IOException | JSONException e) {
e.printStackTrace();
}
ImageView imageView = null;
@Override
protected doInBackground() {
this.imageView = imageViews[0];
return updateChart();
}
@Override
protected void onPostExecute(Bitmap result) {
for(
imageView.setImageBitmap(result);
}
private Bitmap updateChart() {
Bitmap bmp = Bitmap.createBitmap(imageView.getDrawable().getIntrinsicHeight(),imageView.getDrawable().getIntrinsicHeight(), Bitmap.Config.ARGB_8888);//w h config
Canvas canvas = new Canvas(bmp);
RectF box = new RectF(2, 2,bmp.getWidth()-2 , bmp.getHeight()-2);
//get value for 100%
int sum = 0;
for (int slice : slices) {
sum += slice;
}
//initalize painter
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(1f);
paint.setStyle(Paint.Style.FILL_AND_STROKE);
float start = 0;
//draw slices
for(int i =0; i < slices.length; i++){
paint.setColor(COLORS[i]);
float angle;
angle = ((360.0f / sum) * slices[i]);
canvas.drawArc(box, start, angle, true, paint);
start += angle;
}
return bmp;
}
}
此处还包括布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<ImageView
android:id="@+id/chart1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="centerInside"
android:adjustViewBounds="true"
android:layout_weight="1"
android:src="@drawable/chart_place_holder"
/>
<ImageView
android:id="@+id/chart2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="centerInside"
android:adjustViewBounds="true"
android:layout_weight="1"
android:src="@drawable/chart_place_holder"
/>
<ImageView
android:id="@+id/chart3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="centerInside"
android:adjustViewBounds="true"
android:layout_weight="1"
android:src="@drawable/chart_place_holder"
/>
</LinearLayout>
<TextView
android:id="@+id/textView"
android:text="data" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_centerInParent="true"
android:textSize="28sp"/>
</LinearLayout>
图表已确认有效。它将获取占位符的当前大小并创建匹配的饼图,以便部分代码可以正常使用。我只是需要一些帮助,让剩下的这个混乱组织起来,这只是超出我的技能组合。
答案 0 :(得分:0)
我能够自己解决这个问题。这是经过完整测试的代码。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/RootView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bg_image">
<ScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="10dp"
android:fillViewport="false">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:background="#cc000000"
android:orientation="horizontal"
>
<!-- chart title -->
<TextView
android:text="CHART ONE"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="centerInside"
android:adjustViewBounds="true"
android:gravity="center"
android:textSize="16sp"
android:layout_weight="1"
/>
<TextView
android:text="CHART TWO"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="centerInside"
android:adjustViewBounds="true"
android:gravity="center"
android:textSize="16sp"
android:layout_weight="1"
/>
<TextView
android:text="CHART THREE"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="centerInside"
android:adjustViewBounds="true"
android:gravity="center"
android:textSize="16sp"
android:layout_weight="1"
/>
</LinearLayout>
<!-- charts -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="#AA000000"
android:gravity="center"
>
<ImageView
android:id="@+id/chart_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="centerInside"
android:adjustViewBounds="true"
android:gravity="center"
android:layout_weight="1"
android:src="@drawable/default_piechart"
/>
<ImageView
android:id="@+id/chart_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="centerInside"
android:adjustViewBounds="true"
android:gravity="center"
android:layout_weight="1"
android:src="@drawable/default_piechart"
/>
<ImageView
android:id="@+id/chart_3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="centerInside"
android:adjustViewBounds="true"
android:gravity="center"
android:layout_weight="1"
android:src="@drawable/default_piechart"
/>
</LinearLayout>
<!-- chart values -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:background="#AA000000"
android:orientation="horizontal"
>
<TextView
android:id="@+id/chart_value_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="centerInside"
android:adjustViewBounds="true"
android:gravity="center"
android:text="-"
android:textSize="16sp"
android:layout_weight="1"
/>
<TextView
android:id="@+id/chart_value_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="centerInside"
android:adjustViewBounds="true"
android:gravity="center"
android:text="-"
android:textSize="16sp"
android:layout_weight="1"
/>
<TextView
android:id="@+id/chart_value_3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="centerInside"
android:adjustViewBounds="true"
android:gravity="center"
android:text="-"
android:textSize="16sp"
android:layout_weight="1"
/>
</LinearLayout>
</LinearLayout>
</ScrollView>
</RelativeLayout>
要显示的内容:
ImageView [] chart_image = new ImageView[3];
TextView [] chart_text = new TextView[3];
chart_image[0] = (ImageView) findViewById(R.id.chart_1);
chart_image[1] = (ImageView) findViewById(R.id.chart_2);
chart_image[2] = (ImageView) findViewById(R.id.chart_3);
chart_text[0] = (TextView) findViewById(R.id.chart_value_1);
chart_text[1] = (TextView) findViewById(R.id.chart_value_2);
chart_text[2] = (TextView) findViewById(R.id.chart_value_3);
String chart_data= "\"charts\": [{ \"value\": \"Chart 1 Value\", \"colors\": [5, 6, 7, 8, 1], \"slices\": [938, 2927, 2271, 557, 5507] }, { \"value\": \"Chart 2 Value\", \"colors\": [4, 8, 1, 1], \"slices\": [5492, 41701, 1507] }, { \"value\": 9999, \"colors\": [8, 1], \"slices\": [7000, 3000] }] ";
new DrawPieCharts(chart_image, chart_text, chart_data);
现在可以从主活动中调用它:
{{1}}