Added Async functionality and grid display. Still need to fix sizes and

display text  under pictures.
master
tokugawa 2014-03-09 17:55:22 -04:00
parent d41468fc6f
commit 61c9379b05
5 changed files with 159 additions and 2 deletions

View File

@ -12,3 +12,4 @@
# Project target.
target=android-19
android.library.reference.1=../../../workspace/gridlayout_v7

View File

@ -1,11 +1,28 @@
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".PhotoActivity" >
</RelativeLayout>
<GridView
android:id="@+id/GridView1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:numColumns="@integer/COLUMN_COUNT"
android:columnWidth="90dp" />
<Button
android:id="@+id/ExitButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:text="@string/Exit" />
</LinearLayout>

View File

@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<integer name="ROW_COUNT">2</integer>
<integer name="COLUMN_COUNT">2</integer>
</resources>

View File

@ -10,5 +10,6 @@
<string name="football_main_image">http://farm9.staticflickr.com/8441/7882624916_5f62cb318f_z.jpg</string>
<string name="title_activity_photo">UNC Charlotte Photos</string>
<string name="title_activity_photo_thread">UNC Charlotte Photos</string>
<string name="Exit">Exit</string>
</resources>

View File

@ -1,15 +1,63 @@
package edu.uncc.itcs4180.hw4;
import java.net.URL;
import java.util.ArrayList;
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
public class PhotoActivity extends Activity {
ProgressDialog progress;
LinearLayout root;
GridView photoGrid;
public int[] imageUrlIds = {R.string.uncc_main_image, R.string.football_main_image,
R.string.ifest_main_image, R.string.commencement_main_image
};
ArrayList<Bitmap> bitmapList = new ArrayList<Bitmap>();
int downloadProgress;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_photo);
root = (LinearLayout)findViewById(R.id.LinearLayout1);
photoGrid = (GridView)findViewById(R.id.GridView1);
downloadProgress = 0;
//set the progress dialog
progress = new ProgressDialog(this);
progress.setMessage("Loading...");
progress.setCancelable(false);
progress.show();
for(int x : imageUrlIds)//download images
{
new DownloadPhoto().execute(getString(x));
}
//create exit button
Button exit = (Button)findViewById(R.id.ExitButton);
exit.setOnClickListener(new View.OnClickListener() {
public void onClick(View v)
{
finish();
}
});
}
@Override
@ -18,5 +66,90 @@ public class PhotoActivity extends Activity {
getMenuInflater().inflate(R.menu.photo, menu);
return true;
}
private class DownloadPhoto extends AsyncTask<String, Void, Bitmap>
{
@Override
protected Bitmap doInBackground(String... url)
{
Bitmap image = null;
try
{
URL imageUrl = new URL(url[0]);
image = BitmapFactory.decodeStream(imageUrl.openStream());
}
catch(Exception e)
{
e.printStackTrace();
}
return image;
}
@Override
protected void onPostExecute(Bitmap result)
{
if (result == null)//set failed images to a default image
{
//set blank image
}
bitmapList.add(result);
downloadProgress++;
if(downloadProgress>=imageUrlIds.length)
{
progress.dismiss();
photoGrid.setAdapter(new ImageAdapter(photoGrid.getContext()));
}
}
}
public class ImageAdapter extends BaseAdapter
{
private Context context;
public ImageAdapter(Context context)
{
this.context = context;
}
@Override
public int getCount()
{
return imageUrlIds.length;
}
@Override
public Object getItem(int position)
{
return position;
}
@Override
public long getItemId(int position)
{
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
ImageView newImageView = new ImageView(getBaseContext());
if(convertView == null)
{
newImageView.setLayoutParams(new GridView.LayoutParams(
140, 140));
newImageView.setPadding(5, 5, 5, 5);
}
else
{
newImageView = (ImageView) convertView;
}
newImageView.setImageBitmap(bitmapList.get(position));
return newImageView;
}
}
}