mirror of
https://github.com/bspeice/itcs4180
synced 2024-12-04 13:18:16 -05:00
Added gallery function
This commit is contained in:
parent
8a1acefb21
commit
5615978ba0
@ -10,9 +10,18 @@
|
||||
<GridView
|
||||
android:id="@+id/gallery"
|
||||
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:numColumns="4" >
|
||||
</GridView>
|
||||
android:layout_gravity="bottom"
|
||||
android:text="@string/exit_button_text" />
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
|
@ -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<Bitmap> 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<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
|
||||
@ -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<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)
|
||||
{
|
||||
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[] 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<String> bitmapNames = new ArrayList<String>();
|
||||
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);
|
||||
}
|
||||
});
|
||||
|
Loading…
Reference in New Issue
Block a user