Android / Google Chart / ImageView – Sample #2 EVOLUTION


This is a revision of the precedent article,this correction need because it was not possible to derive the size of the ImageView before the system designed it all view of the activity, here’s the correct code.

public class LoadingRemoteGraph extends AsyncTask<String, Void, Bitmap> {
	private final WeakReference<ImageView> imageViewReference;
	private Context context = null;

	ImageView ivT;
	int vHeight = 0;
	int vWidth = 0;
	String strUrlGraphx2;

	public LoadingRemoteGraph(Context contextT, final ImageView imageView) {
		context = contextT;
		imageViewReference = new WeakReference<ImageView>(imageView);

		ivT = imageViewReference.get();
		vHeight = ivT.getHeight();
		vWidth = ivT.getWidth();

		Log.v("THREAD-construct", "H:" + vHeight + " W:"
				+ vWidth);

	}

	@Override
	protected void onPreExecute() {
		if (prbLoadGraph == null) {
			prbLoadGraph = ProgressDialog.show(context, "",
					context.getString(R.string.str_generic_progressbar),
					true);
		}
	}

	@Override
	protected Bitmap doInBackground(String... params) {
		Bitmap bmGraphT = null;
		URL urlImg = null;

		Log.v("THREAD", "H:" + vHeight + " W:" + vWidth);

		String strUrl = params[0].replace("WIDTHxHEIGHT",
				String.valueOf(vWidth) + "x" + String.valueOf(vHeight));

		strUrlGraphx2 = new String(params[0].replace(
				"WIDTHxHEIGHT",
				String.valueOf(800) + "x"
						+ String.valueOf(800)));

		try {
			urlImg = new URL(strUrl);
		} catch (MalformedURLException e) {
			e.printStackTrace();
		}

		try {
			bmGraphT = BitmapFactory.decodeStream(urlImg.openStream());
		} catch (IOException e) {
			e.printStackTrace();
		}

		return bmGraphT;
	}

	@Override
	protected void onPostExecute(Bitmap bmT) {

		if (prbLoadGraph != null) {
			prbLoadGraph.dismiss();
		}

		if (isCancelled()) {
			bmT = null;
		}
		if (imageViewReference != null) {
			ImageView imageView = imageViewReference.get();
			if (imageView != null) {
				imageView.setImageBitmap(bmT);

				/*
				 * set click events on the imageview
				 */
				imageView.setOnClickListener(new OnClickListener() {
					public void onClick(View view) {
						Intent intent = new Intent(Intent.ACTION_VIEW, Uri
								.parse(strUrlGraphx2));
						intent.addCategory(Intent.CATEGORY_BROWSABLE);
						context.startActivity(intent);
					}
				});
			}

		}
	}
}

and then you can use this task in the onWindowFocusChanged method,like this:

public class ActGraph extends Activity{
	/** Called when the activity is first created. */

	.......

	private boolean needChart = true;
	private static String strGraphScoreOriginal = "http://chart.apis.google.com/chart?chxr=0,0,129&chxs=0,676767,11.5,0,_,676767&chxt=y&chbh=a&chs=WIDTHxHEIGHT&cht=bvg&chco=A2C180,3D7930&chds=29,129,29,129&chd=t:YOUR|MEDIA&chdl=Tuo|Media&chdlp=b";
	.......

	@Override
	public void onWindowFocusChanged(boolean hasFocus) {
		super.onWindowFocusChanged(hasFocus);
		if (needChart) {
			ImageView imvGraphResult = (ImageView) findViewById(R.id.actquizfinal_iv_graphresult);

			final String strUrl = strGraphScoreOriginal.replace(
					"YOUR|MEDIA", String.valueOf(QuizScore) + "|"
							+ String.valueOf(dataT[0]));

			LoadingRemoteGraph task = new LoadingRemoteGraph(
					ActGraphRemoteBase.this, imvGraphResult);
			task.execute(strUrl);

			needChart = false;
		}
	}

	@Override
	public void onPause() {
		super.onPause();

		needChart = true;
	}

	........

I still do not like it, is a little rough but I did not find a more elegant solution.8y3

Lascia un commento

Inserisci i tuoi dati qui sotto o clicca su un'icona per effettuare l'accesso:

Logo WordPress.com

Stai commentando usando il tuo account WordPress.com. Chiudi sessione / Modifica )

Foto Twitter

Stai commentando usando il tuo account Twitter. Chiudi sessione / Modifica )

Foto di Facebook

Stai commentando usando il tuo account Facebook. Chiudi sessione / Modifica )

Google+ photo

Stai commentando usando il tuo account Google+. Chiudi sessione / Modifica )

Connessione a %s...