diff --git a/HW4/res/layout/activity_gallery.xml b/HW4/res/layout/activity_gallery.xml index 8a30688..9de29db 100644 --- a/HW4/res/layout/activity_gallery.xml +++ b/HW4/res/layout/activity_gallery.xml @@ -10,9 +10,18 @@ + + - + android:layout_gravity="bottom" + android:text="@string/exit_button_text" /> diff --git a/HW4/src/com/example/hw4/GalleryActivity.java b/HW4/src/com/example/hw4/GalleryActivity.java index cd53810..a21b7ea 100644 --- a/HW4/src/com/example/hw4/GalleryActivity.java +++ b/HW4/src/com/example/hw4/GalleryActivity.java @@ -7,19 +7,45 @@ package com.example.hw4; * GalleryActivity.java */ +import java.net.URL; +import java.util.ArrayList; + +import com.example.hw4.MainActivity.Holder; + import android.app.Activity; +import android.app.ProgressDialog; +import android.content.Context; +import android.content.Intent; +import android.graphics.Bitmap; +import android.graphics.BitmapFactory; +import android.os.AsyncTask; import android.os.Bundle; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuItem; import android.view.View; +import android.view.ViewGroup; +import android.view.View.OnClickListener; +import android.widget.AdapterView; +import android.widget.BaseAdapter; +import android.widget.Button; import android.widget.GridView; +import android.widget.ImageView; +import android.widget.TextView; import android.widget.Toast; +import android.widget.AdapterView.OnItemClickListener; public class GalleryActivity extends Activity { GridView gallery; - int thumbsId; + ImageView imageView; + Button exitButton; + String[] thumbUrls; + ArrayList thumbs; + ImageAdapter imageAdapter; + Intent imageViewerIntent; + ProgressDialog progress; + int photosId; @Override protected void onCreate(Bundle savedInstanceState) @@ -27,8 +53,29 @@ public class GalleryActivity extends Activity super.onCreate(savedInstanceState); setContentView(R.layout.activity_gallery); - thumbsId = getIntent().getExtras().getInt("thumbsId"); - Toast.makeText(getApplicationContext(), thumbsId, Toast.LENGTH_LONG).show(); + imageViewerIntent = new Intent(this, ImageViewerActivity.class); + thumbUrls = getResources().getStringArray(getIntent().getExtras().getInt("thumbsId")); + photosId = getIntent().getExtras().getInt("photosId"); + gallery = (GridView)findViewById(R.id.gallery); + exitButton = (Button)findViewById(R.id.buttonExit); + thumbs = new ArrayList(); + + progress = new ProgressDialog(this); + progress.setMessage("Loading..."); + progress.setCancelable(false); + progress.show(); + + for(String url: thumbUrls) + { + new DownloadThumbs().execute(url); + } + + exitButton.setOnClickListener(new OnClickListener(){ + @Override + public void onClick(View v) { + finish(); + } + }); } @Override @@ -39,15 +86,105 @@ public class GalleryActivity extends Activity return true; } - @Override - public boolean onOptionsItemSelected(MenuItem item) { - // Handle action bar item clicks here. The action bar will - // automatically handle clicks on the Home/Up button, so long - // as you specify a parent activity in AndroidManifest.xml. - int id = item.getItemId(); - if (id == R.id.action_settings) { - return true; + private class DownloadThumbs extends AsyncTask + { + + @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) + { + thumbs.add(result); + } + else + { + thumbs.add(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher)); + } + + if(thumbs.size() >= thumbUrls.length) + { + imageAdapter = new ImageAdapter(gallery.getContext()); + gallery.setAdapter(imageAdapter); + + gallery.setOnItemClickListener(new OnItemClickListener() + { + @Override + public void onItemClick(AdapterView> parent, View view, + int position, long id) + { + imageViewerIntent.putExtra("urls", (int)photosId); + imageViewerIntent.putExtra("index", (int)position); + startActivity(imageViewerIntent); + } + }); + progress.dismiss(); + } + } + } + + public class ImageAdapter extends BaseAdapter + { + private Context context; + + public ImageAdapter(Context context) + { + this.context = context; + } + + @Override + public int getCount() + { + return thumbs.size(); + } + + @Override + public Object getItem(int position)//no purpose. + { + 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) + { + ImageView imageView; + View vi = convertView; + + if(vi == null) + { + imageView = new ImageView(getBaseContext()); + //imageView.setLayoutParams(new GridView.LayoutParams(50,50)); + imageView.setPadding(5, 5, 5, 5); + } + else + { + imageView = (ImageView)vi; + } + + imageView.setImageBitmap(thumbs.get(position)); + return imageView; } - return super.onOptionsItemSelected(item); } } diff --git a/HW4/src/com/example/hw4/MainActivity.java b/HW4/src/com/example/hw4/MainActivity.java index 66badc7..2d0ed7c 100644 --- a/HW4/src/com/example/hw4/MainActivity.java +++ b/HW4/src/com/example/hw4/MainActivity.java @@ -41,6 +41,7 @@ public class MainActivity extends Activity { }; int[] imageNames = {R.string.label_uncc, R.string.label_sports, R.string.label_ifest, R.string.label_commencement}; int[] thumbNames = {R.array.uncc_thumbs, R.array.football_thumbs, R.array.ifest_thumbs, R.array.commencement_thumbs}; + int[] photoNames = {R.array.uncc_photos, R.array.football_photos, R.array.ifest_photos, R.array.commencement_photos}; ArrayList bitmapList = new ArrayList(); ArrayList bitmapNames = new ArrayList(); int downloadProgress; @@ -125,8 +126,9 @@ public class MainActivity extends Activity { public void onItemClick(AdapterView> parent, View view, int position, long id) { - //Send intent with R.array.label - galleryIntent.putExtra("thumbsId", (Integer)(imageAdapter.getItem(position))); + //Send intent with thumbs and photos id + galleryIntent.putExtra("thumbsId", thumbNames[position]); + galleryIntent.putExtra("photosId", photoNames[position]); startActivity(galleryIntent); } });