From 28001df8c607f9e678afdc90445075c9cf5d269b Mon Sep 17 00:00:00 2001 From: Robert Kolner Date: Mon, 5 Dec 2011 21:12:26 +0100 Subject: [PATCH] Added images to event list. --- res/layout/event_list_row.xml | 12 +++++-- src/com/studentersamfundet/app/Event.java | 9 +++-- .../app/ui/EventListActivity.java | 35 +++++++++++++++++++ 3 files changed, 51 insertions(+), 5 deletions(-) diff --git a/res/layout/event_list_row.xml b/res/layout/event_list_row.xml index c553194..93ba490 100644 --- a/res/layout/event_list_row.xml +++ b/res/layout/event_list_row.xml @@ -6,21 +6,29 @@ android:paddingTop="3px" android:paddingBottom="3px"> + + diff --git a/src/com/studentersamfundet/app/Event.java b/src/com/studentersamfundet/app/Event.java index 891cb34..ea18064 100644 --- a/src/com/studentersamfundet/app/Event.java +++ b/src/com/studentersamfundet/app/Event.java @@ -81,13 +81,16 @@ public class Event implements Serializable { return Uri.parse("http://studentersamfundet.no/vis.php?ID=" + this.id); } - public Uri getImageUri() { + public Uri getImageUri(int width) { if (this.id <= 0) { return null; } - String imageUri = "http://studentersamfundet.no/imageResize.php?pic=bilder/program/{image}&maxwidth=480"; - return Uri.parse(imageUri.replaceAll("{image}", this.image)); + String imageUri = "http://studentersamfundet.no/imageResize.php?pic=bilder/program/{img}&maxwidth={width}"; + imageUri = imageUri.replace("{img}", this.image); + imageUri = imageUri.replace("{width}", Integer.toString(width)); + + return Uri.parse(imageUri); } public String getPriceString() { diff --git a/src/com/studentersamfundet/app/ui/EventListActivity.java b/src/com/studentersamfundet/app/ui/EventListActivity.java index 9159417..7e78e22 100644 --- a/src/com/studentersamfundet/app/ui/EventListActivity.java +++ b/src/com/studentersamfundet/app/ui/EventListActivity.java @@ -1,6 +1,10 @@ package com.studentersamfundet.app.ui; import java.io.IOException; +import java.io.InputStream; +import java.net.HttpURLConnection; +import java.net.MalformedURLException; +import java.net.URL; import java.text.SimpleDateFormat; import org.w3c.dom.NodeList; @@ -8,6 +12,9 @@ import org.w3c.dom.NodeList; import android.app.Dialog; import android.content.ActivityNotFoundException; import android.content.Intent; +import android.graphics.Bitmap; +import android.graphics.BitmapFactory; +import android.graphics.Matrix; import android.os.AsyncTask; import android.os.Bundle; import android.text.Html; @@ -20,6 +27,7 @@ import android.view.ViewGroup; import android.view.View.OnClickListener; import android.view.View.OnLongClickListener; import android.widget.ArrayAdapter; +import android.widget.ImageView; import android.widget.ListAdapter; import android.widget.ListView; import android.widget.ProgressBar; @@ -107,6 +115,33 @@ public class EventListActivity extends BaseDnsActivity { final Event e = getItem(position); + /* Set images: */ + ImageView imageView = (ImageView) row.findViewById(R.id.event_list_row_image); + try { + String urlStr = e.getImageUri(60).toString(); + URL ulrn = new URL(urlStr); + HttpURLConnection con = (HttpURLConnection)ulrn.openConnection(); + InputStream is = con.getInputStream(); + Bitmap bmp = BitmapFactory.decodeStream(is); + + if (null != bmp) { + int width = bmp.getWidth(); + int height = bmp.getHeight(); + + float scaleWidth = ((float)60) / width; + float scaleHeight = ((float)60) / height; + + Matrix matrix = new Matrix(); + matrix.postScale(scaleWidth, scaleHeight); + + Bitmap resized = Bitmap.createBitmap(bmp, 0, 0, width, height, matrix, false); + imageView.setImageBitmap(resized); + } + + } catch (MalformedURLException ex1){ /* TODO: Default image */ + } catch (IOException ex2) { /* TODO: Default image */ } + + /* Set text: */ TextView titleView = (TextView) row.findViewById(R.id.event_list_row_text); titleView.setText(Html.fromHtml(e.title));