これは、このスレッドを読んでいる人にとっては非常に便利な例です。私は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>
チャートは作業確認されているこれは、私が今起こっている現在のホット混乱です。プレースホルダの現在のサイズを取得して一致するように円グラフを作成するので、コードの一部がうまくいきます。私はちょうどこの混乱の残りを整理するのに少し助けを必要とし、それは単に私のスキルセットを超えています。