mirror of
				https://github.com/bspeice/itcs4180
				synced 2025-11-04 02:10:32 -05:00 
			
		
		
		
	Added gallery function
This commit is contained in:
		@ -10,9 +10,18 @@
 | 
				
			|||||||
    <GridView
 | 
					    <GridView
 | 
				
			||||||
        android:id="@+id/gallery"
 | 
					        android:id="@+id/gallery"
 | 
				
			||||||
        android:layout_width="match_parent"
 | 
					        android:layout_width="match_parent"
 | 
				
			||||||
 | 
					        android:layout_height="0dp"
 | 
				
			||||||
 | 
					        android:layout_margin="5dp"
 | 
				
			||||||
 | 
					        android:layout_weight="1"
 | 
				
			||||||
 | 
					        android:columnWidth="50dp"
 | 
				
			||||||
 | 
					        android:numColumns="4" />
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    <Button
 | 
				
			||||||
 | 
					        android:id="@+id/buttonExit"
 | 
				
			||||||
 | 
					        android:layout_width="fill_parent"
 | 
				
			||||||
        android:layout_height="wrap_content"
 | 
					        android:layout_height="wrap_content"
 | 
				
			||||||
        android:numColumns="4" >
 | 
					        android:layout_gravity="bottom"
 | 
				
			||||||
    </GridView>
 | 
					        android:text="@string/exit_button_text" />
 | 
				
			||||||
 | 
					
 | 
				
			||||||
</LinearLayout>
 | 
					</LinearLayout>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
@ -7,19 +7,45 @@ package com.example.hw4;
 | 
				
			|||||||
 * GalleryActivity.java
 | 
					 * GalleryActivity.java
 | 
				
			||||||
 */
 | 
					 */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import java.net.URL;
 | 
				
			||||||
 | 
					import java.util.ArrayList;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import com.example.hw4.MainActivity.Holder;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import android.app.Activity;
 | 
					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.os.Bundle;
 | 
				
			||||||
import android.view.LayoutInflater;
 | 
					import android.view.LayoutInflater;
 | 
				
			||||||
import android.view.Menu;
 | 
					import android.view.Menu;
 | 
				
			||||||
import android.view.MenuItem;
 | 
					import android.view.MenuItem;
 | 
				
			||||||
import android.view.View;
 | 
					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.GridView;
 | 
				
			||||||
 | 
					import android.widget.ImageView;
 | 
				
			||||||
 | 
					import android.widget.TextView;
 | 
				
			||||||
import android.widget.Toast;
 | 
					import android.widget.Toast;
 | 
				
			||||||
 | 
					import android.widget.AdapterView.OnItemClickListener;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
public class GalleryActivity extends Activity 
 | 
					public class GalleryActivity extends Activity 
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
	GridView gallery;
 | 
						GridView gallery;
 | 
				
			||||||
	int thumbsId;
 | 
						ImageView imageView;
 | 
				
			||||||
 | 
						Button exitButton;
 | 
				
			||||||
 | 
						String[] thumbUrls;
 | 
				
			||||||
 | 
						ArrayList<Bitmap> thumbs;
 | 
				
			||||||
 | 
						ImageAdapter imageAdapter;
 | 
				
			||||||
 | 
						Intent imageViewerIntent;
 | 
				
			||||||
 | 
						ProgressDialog progress;
 | 
				
			||||||
 | 
						int photosId;
 | 
				
			||||||
	
 | 
						
 | 
				
			||||||
	@Override
 | 
						@Override
 | 
				
			||||||
	protected void onCreate(Bundle savedInstanceState) 
 | 
						protected void onCreate(Bundle savedInstanceState) 
 | 
				
			||||||
@ -27,8 +53,29 @@ public class GalleryActivity extends Activity
 | 
				
			|||||||
		super.onCreate(savedInstanceState);
 | 
							super.onCreate(savedInstanceState);
 | 
				
			||||||
		setContentView(R.layout.activity_gallery);
 | 
							setContentView(R.layout.activity_gallery);
 | 
				
			||||||
		
 | 
							
 | 
				
			||||||
		thumbsId = getIntent().getExtras().getInt("thumbsId");
 | 
							imageViewerIntent = new Intent(this, ImageViewerActivity.class);
 | 
				
			||||||
		Toast.makeText(getApplicationContext(), thumbsId, Toast.LENGTH_LONG).show();
 | 
							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<Bitmap>();
 | 
				
			||||||
 | 
							
 | 
				
			||||||
 | 
							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
 | 
						@Override
 | 
				
			||||||
@ -39,15 +86,105 @@ public class GalleryActivity extends Activity
 | 
				
			|||||||
		return true;
 | 
							return true;
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						private class DownloadThumbs extends AsyncTask<String, Void, Bitmap>
 | 
				
			||||||
 | 
						{
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		@Override
 | 
							@Override
 | 
				
			||||||
	public boolean onOptionsItemSelected(MenuItem item) {
 | 
							protected Bitmap doInBackground(String... url) 
 | 
				
			||||||
		// Handle action bar item clicks here. The action bar will
 | 
							{
 | 
				
			||||||
		// automatically handle clicks on the Home/Up button, so long
 | 
								Bitmap image = null;
 | 
				
			||||||
		// as you specify a parent activity in AndroidManifest.xml.
 | 
								
 | 
				
			||||||
		int id = item.getItemId();
 | 
								try
 | 
				
			||||||
		if (id == R.id.action_settings) {
 | 
								{
 | 
				
			||||||
			return true;
 | 
									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);
 | 
					 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
@ -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[] 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[] 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<Bitmap> bitmapList = new ArrayList<Bitmap>();
 | 
						ArrayList<Bitmap> bitmapList = new ArrayList<Bitmap>();
 | 
				
			||||||
	ArrayList<String> bitmapNames = new ArrayList<String>();
 | 
						ArrayList<String> bitmapNames = new ArrayList<String>();
 | 
				
			||||||
	int downloadProgress;
 | 
						int downloadProgress;
 | 
				
			||||||
@ -125,8 +126,9 @@ public class MainActivity extends Activity {
 | 
				
			|||||||
					public void onItemClick(AdapterView<?> parent, View view,
 | 
										public void onItemClick(AdapterView<?> parent, View view,
 | 
				
			||||||
							int position, long id) 
 | 
												int position, long id) 
 | 
				
			||||||
					{
 | 
										{
 | 
				
			||||||
						//Send intent with R.array.label
 | 
											//Send intent with thumbs and photos id
 | 
				
			||||||
						galleryIntent.putExtra("thumbsId", (Integer)(imageAdapter.getItem(position)));
 | 
											galleryIntent.putExtra("thumbsId", thumbNames[position]);
 | 
				
			||||||
 | 
											galleryIntent.putExtra("photosId", photoNames[position]);
 | 
				
			||||||
						startActivity(galleryIntent);
 | 
											startActivity(galleryIntent);
 | 
				
			||||||
					}
 | 
										}
 | 
				
			||||||
				});
 | 
									});
 | 
				
			||||||
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user