mirror of
https://github.com/bspeice/itcs4180
synced 2024-12-04 13:18:16 -05:00
Switch the PhotoActivity to the MainActivity
This commit is contained in:
parent
0e90b852c9
commit
c9941fd0e4
@ -1,32 +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/layout_async"
|
||||
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=".MainActivity" >
|
||||
|
||||
<GridView
|
||||
android:id="@+id/grid_async"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="0dp"
|
||||
android:layout_weight="1"
|
||||
android:columnWidth="90dp"
|
||||
android:numColumns="@integer/column_count" />
|
||||
|
||||
<Button
|
||||
android:id="@+id/btnThread"
|
||||
android:layout_width="wrap_content"
|
||||
android:id="@+id/btn_exit_async"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_alignLeft="@+id/btnAsync"
|
||||
android:layout_alignParentTop="true"
|
||||
android:layout_alignRight="@+id/btnAsync"
|
||||
android:layout_marginTop="100dp"
|
||||
android:onClick="onClick"
|
||||
android:text="Generate Image Thread" />
|
||||
android:layout_gravity="bottom"
|
||||
android:text="@string/exit_button_text" />
|
||||
|
||||
<Button
|
||||
android:id="@+id/btnAsync"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_below="@+id/btnThread"
|
||||
android:layout_centerHorizontal="true"
|
||||
android:layout_marginTop="25dp"
|
||||
android:onClick="onClick"
|
||||
android:text="Generate Image AsyncTask" />
|
||||
|
||||
</RelativeLayout>
|
||||
</LinearLayout>
|
||||
|
@ -1,28 +0,0 @@
|
||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:tools="http://schemas.android.com/tools"
|
||||
android:id="@+id/layout_async"
|
||||
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" >
|
||||
|
||||
<GridView
|
||||
android:id="@+id/grid_async"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="0dp"
|
||||
android:layout_weight="1"
|
||||
android:columnWidth="90dp"
|
||||
android:numColumns="@integer/column_count" />
|
||||
|
||||
<Button
|
||||
android:id="@+id/btn_exit_async"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_gravity="bottom"
|
||||
android:text="@string/exit_button_text" />
|
||||
|
||||
</LinearLayout>
|
@ -4,36 +4,179 @@ package com.example.hw4;
|
||||
* Bradlee Speice, Brandon Rodenmayer
|
||||
* ITIS 4180
|
||||
* In Class 3
|
||||
* MainActivity.java
|
||||
* PhotoActivity.java
|
||||
*/
|
||||
|
||||
import java.net.URL;
|
||||
import java.util.ArrayList;
|
||||
|
||||
import android.os.AsyncTask;
|
||||
import android.os.Bundle;
|
||||
import android.app.Activity;
|
||||
import android.content.Intent;
|
||||
import android.app.ProgressDialog;
|
||||
import android.content.Context;
|
||||
import android.graphics.Bitmap;
|
||||
import android.graphics.BitmapFactory;
|
||||
import android.view.LayoutInflater;
|
||||
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;
|
||||
import android.widget.TextView;
|
||||
|
||||
public class MainActivity extends Activity {
|
||||
|
||||
ProgressDialog progress;
|
||||
LinearLayout root;
|
||||
GridView photoGrid;
|
||||
int[] imageUrlIds = {R.string.uncc_main_image, R.string.football_main_image,
|
||||
R.string.ifest_main_image, R.string.commencement_main_image
|
||||
};
|
||||
int[] imageNames = {R.string.uncc, R.string.sports, R.string.ifest, R.string.commencement};
|
||||
ArrayList<Bitmap> bitmapList = new ArrayList<Bitmap>();
|
||||
ArrayList<String> bitmapNames = new ArrayList<String>();
|
||||
int downloadProgress;
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.activity_main);
|
||||
|
||||
root = (LinearLayout)findViewById(R.id.layout_async);
|
||||
photoGrid = (GridView)findViewById(R.id.grid_async);
|
||||
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.btn_exit_async);
|
||||
exit.setOnClickListener(new View.OnClickListener() {
|
||||
public void onClick(View v)
|
||||
{
|
||||
finish();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onCreateOptionsMenu(Menu menu) {
|
||||
// Inflate the menu; this adds items to the action bar if it is present.
|
||||
getMenuInflater().inflate(R.menu.main, menu);
|
||||
getMenuInflater().inflate(R.menu.photo, menu);
|
||||
return true;
|
||||
}
|
||||
|
||||
public void onClick(View v) {
|
||||
// One of our buttons has been clicked, let's start the activity and go!
|
||||
if (v.getId() == R.id.btnAsync)
|
||||
startActivity(new Intent(this, PhotoActivity.class));
|
||||
else
|
||||
startActivity(new Intent(this, PhotoThread.class));
|
||||
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)
|
||||
{
|
||||
//already a default picture included in grid_schema.xml, so no need to set a blank pic
|
||||
bitmapList.add(result);
|
||||
bitmapNames.add(getString(imageNames[downloadProgress]));
|
||||
|
||||
downloadProgress++;
|
||||
if(downloadProgress>=imageUrlIds.length)
|
||||
{
|
||||
progress.dismiss();
|
||||
//all images are loaded, so set them in the grid
|
||||
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)//no purpose. only to fill the requirement of needing the method.
|
||||
{
|
||||
return position;
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getItemId(int position)//no purpose. only to fill the requirement of needing the method.
|
||||
{
|
||||
return position;
|
||||
}
|
||||
|
||||
@Override
|
||||
public View getView(int position, View convertView, ViewGroup parent)
|
||||
{
|
||||
Holder holder = new Holder();
|
||||
View vi = convertView;
|
||||
|
||||
if(vi == null)
|
||||
{
|
||||
//create layout of what we want one grid section to look like
|
||||
vi = getLayoutInflater().inflate(R.layout.grid_schema, null);
|
||||
|
||||
holder.textView = (TextView)vi.findViewById(R.id.textView1);
|
||||
holder.imageView = (ImageView)vi.findViewById(R.id.imageView1);
|
||||
|
||||
vi.setTag(holder);//associate the views in the holder to the grid
|
||||
}
|
||||
else
|
||||
{
|
||||
holder = (Holder)(vi.getTag());
|
||||
}
|
||||
//set the views in the grid to what was loaded
|
||||
holder.textView.setText(getString(R.string.download_error));
|
||||
if(bitmapList.get(position)!=null)
|
||||
{
|
||||
holder.imageView.setImageBitmap(bitmapList.get(position));
|
||||
holder.textView.setText(bitmapNames.get(position));
|
||||
}
|
||||
|
||||
return vi;
|
||||
}
|
||||
}
|
||||
|
||||
//views included in one grid section
|
||||
static class Holder
|
||||
{
|
||||
TextView textView;
|
||||
ImageView imageView;
|
||||
}
|
||||
}
|
||||
|
@ -1,182 +0,0 @@
|
||||
package com.example.hw4;
|
||||
|
||||
/*
|
||||
* Bradlee Speice, Brandon Rodenmayer
|
||||
* ITIS 4180
|
||||
* In Class 3
|
||||
* PhotoActivity.java
|
||||
*/
|
||||
|
||||
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.LayoutInflater;
|
||||
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;
|
||||
import android.widget.TextView;
|
||||
|
||||
public class PhotoActivity extends Activity {
|
||||
|
||||
ProgressDialog progress;
|
||||
LinearLayout root;
|
||||
GridView photoGrid;
|
||||
int[] imageUrlIds = {R.string.uncc_main_image, R.string.football_main_image,
|
||||
R.string.ifest_main_image, R.string.commencement_main_image
|
||||
};
|
||||
int[] imageNames = {R.string.uncc, R.string.sports, R.string.ifest, R.string.commencement};
|
||||
ArrayList<Bitmap> bitmapList = new ArrayList<Bitmap>();
|
||||
ArrayList<String> bitmapNames = new ArrayList<String>();
|
||||
int downloadProgress;
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.activity_photo);
|
||||
|
||||
root = (LinearLayout)findViewById(R.id.layout_async);
|
||||
photoGrid = (GridView)findViewById(R.id.grid_async);
|
||||
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.btn_exit_async);
|
||||
exit.setOnClickListener(new View.OnClickListener() {
|
||||
public void onClick(View v)
|
||||
{
|
||||
finish();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onCreateOptionsMenu(Menu menu) {
|
||||
// Inflate the menu; this adds items to the action bar if it is present.
|
||||
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)
|
||||
{
|
||||
//already a default picture included in grid_schema.xml, so no need to set a blank pic
|
||||
bitmapList.add(result);
|
||||
bitmapNames.add(getString(imageNames[downloadProgress]));
|
||||
|
||||
downloadProgress++;
|
||||
if(downloadProgress>=imageUrlIds.length)
|
||||
{
|
||||
progress.dismiss();
|
||||
//all images are loaded, so set them in the grid
|
||||
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)//no purpose. only to fill the requirement of needing the method.
|
||||
{
|
||||
return position;
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getItemId(int position)//no purpose. only to fill the requirement of needing the method.
|
||||
{
|
||||
return position;
|
||||
}
|
||||
|
||||
@Override
|
||||
public View getView(int position, View convertView, ViewGroup parent)
|
||||
{
|
||||
Holder holder = new Holder();
|
||||
View vi = convertView;
|
||||
|
||||
if(vi == null)
|
||||
{
|
||||
//create layout of what we want one grid section to look like
|
||||
vi = getLayoutInflater().inflate(R.layout.grid_schema, null);
|
||||
|
||||
holder.textView = (TextView)vi.findViewById(R.id.textView1);
|
||||
holder.imageView = (ImageView)vi.findViewById(R.id.imageView1);
|
||||
|
||||
vi.setTag(holder);//associate the views in the holder to the grid
|
||||
}
|
||||
else
|
||||
{
|
||||
holder = (Holder)(vi.getTag());
|
||||
}
|
||||
//set the views in the grid to what was loaded
|
||||
holder.textView.setText(getString(R.string.download_error));
|
||||
if(bitmapList.get(position)!=null)
|
||||
{
|
||||
holder.imageView.setImageBitmap(bitmapList.get(position));
|
||||
holder.textView.setText(bitmapNames.get(position));
|
||||
}
|
||||
|
||||
return vi;
|
||||
}
|
||||
}
|
||||
|
||||
//views included in one grid section
|
||||
static class Holder
|
||||
{
|
||||
TextView textView;
|
||||
ImageView imageView;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user