Android image "viewer" application

I am trying to create an Android application that will allow me to display full-screen images with the next and previous buttons on top to change them.

Can someone point me to some tutorials where I can find instructions on something like this?

If not, which method is better to use for receiving images in the application? I tried several ways to create object classes for images and create instances using painting in each, using Bitmap Factory to return the image, but this will not work.

I am starting an android and can really use the help material but cannot find anything useful that covers this issue.

+6
android image gallery
source share
2 answers

As a beginner, I myself worked with this, and it is very simple. Here is some code (maybe there is a better way, but I understand how to do this):

package com.imageviewexample; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.ImageView; public class ImageViewExample extends Activity implements OnClickListener { /** Called when the activity is first created. */ int image_index = 0; private static final int MAX_IMAGE_COUNT = 3; private int[] mImageIds = { R.raw.image1, R.raw.image2, R.raw.image3 }; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Button btnPrevious = (Button)findViewById(R.id.previous_btn); btnPrevious.setOnClickListener(this); Button btnNext = (Button)findViewById(R.id.next_btn); btnNext.setOnClickListener(this); showImage(); } private void showImage() { ImageView imgView = (ImageView) findViewById(R.id.myimage); imgView.setImageResource(mImageIds[image_index]); } public void onClick(View v) { switch (v.getId()) { case (R.id.previous_btn): image_index--; if (image_index == -1) { image_index = MAX_IMAGE_COUNT - 1; } showImage(); break; case (R.id.next_btn): image_index++; if (image_index == MAX_IMAGE_COUNT) { image_index = 0; } showImage(); break; } } } 

And this is main.xml :

 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <Button android:id="@+id/previous_btn" android:layout_width="124dip" android:layout_height="wrap_content" android:text="Previous" /> <Button android:id="@+id/next_btn" android:layout_width="124dip" android:layout_height="wrap_content" android:layout_toRightOf="@+id/previous_btn" android:text="Next" /> <ImageView android:id="@+id/myimage" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="@+id/previous_btn" /> </RelativeLayout> 
+10
source share

This is a solution for viewing images with a grid view of image resources from which you can select an image and display it as a single image. In this single image view, which is executed using PictureViewer.java, you can view snapshots of resources using the left and right buttons. You can also return to the grid view, which is executed using HelloGridViewActivity.java. ImageAdapter.java is used to define resources and grid presentation methods. Hope this helps:

HelloGridViewActivity.java:

 package com.example.hellogridview; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.GridView; public class HelloGridViewActivity extends Activity { private long prev=0,next= 0; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); final GridView gridview = (GridView) findViewById(R.id.gridview); gridview.setAdapter(new ImageAdapter(this)); gridview.setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView<?> parent, View v, int position, long id) { prev = ImageAdapter.getPrevItemId(position); next = ImageAdapter.getNextItemId(position); showImage(gridview.getAdapter().getItemId(position),position); } }); } private void showImage(long id, int pos){ Intent pictureViewer = new Intent(this, PictureViewer.class); pictureViewer.putExtra("pictureId",id ); pictureViewer.putExtra("picturePosition", pos); pictureViewer.putExtra("picturePrevId", prev); pictureViewer.putExtra("pictureNextId", next); startActivityForResult(pictureViewer,0); } } 

ImageAdapter.java:

 package com.example.hellogridview; import android.content.Context; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.GridView; import android.widget.ImageView; public class ImageAdapter extends BaseAdapter { private Context mContext; public ImageAdapter(Context c) { mContext = c; } public int getCount() { return mThumbIds.length; } public Object getItem(int position) { return null; } public long getItemId(int position) { return mThumbIds[position]; } public static long getPrevItemId(int position) { if(--position<0) position = 21; return mThumbIds[position]; } public static long getNextItemId(int position) { if(++position>21) position = 0; return mThumbIds[position]; } // create a new ImageView for each item referenced by the Adapter public View getView(int position, View convertView, ViewGroup parent) { ImageView imageView; if (convertView == null) { // if it not recycled, initialize some attributes imageView = new ImageView(mContext); imageView.setLayoutParams(new GridView.LayoutParams(85, 85)); imageView.setScaleType(ImageView.ScaleType.CENTER_CROP); imageView.setPadding(8, 8, 8, 8); } else { imageView = (ImageView) convertView; } imageView.setImageResource(mThumbIds[position]); return imageView; } // references to our images private static Integer[] mThumbIds = { R.drawable.sample_2, R.drawable.sample_3, R.drawable.sample_4, R.drawable.sample_5, R.drawable.sample_6, R.drawable.sample_7, R.drawable.sample_0, R.drawable.sample_1, R.drawable.sample_2, R.drawable.sample_3, R.drawable.sample_4, R.drawable.sample_5, R.drawable.sample_6, R.drawable.sample_7, R.drawable.sample_0, R.drawable.sample_1, R.drawable.sample_2, R.drawable.sample_3, R.drawable.sample_4, R.drawable.sample_5, R.drawable.sample_6, R.drawable.sample_7 }; } 

PictureViewer.java:

 package com.example.hellogridview; import android.app.Activity; import android.content.Intent; import android.graphics.drawable.Drawable; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.ImageView; public class PictureViewer extends Activity { long picItem,picPrevItem,picNextItem; int picPosition; private ImageView m_vwImage; public void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.picture_layout); //citanje podataka prosledjenih ovoj aktivnosti //namera koja je pokrenula ovu aktivnost Intent i = getIntent(); picItem = i.getLongExtra("pictureId", -1); picPrevItem = i.getLongExtra("picturePrevId", -1); picNextItem = i.getLongExtra("pictureNextId", -1); picPosition = i.getIntExtra("picturePosition", -1); m_vwImage = (ImageView) findViewById(R.id.imageview); //menjamo ulazne vrednosti da bi ih takve prosledili kao povratne Log.i("Nemanja", "picItem" + picItem); Drawable image = getResources().getDrawable((int)picItem); m_vwImage.setImageDrawable(image); Button startButton = (Button) findViewById(R.id.return_button); startButton.setOnClickListener(new View.OnClickListener(){ public void onClick(View view){ finish(); } }); Button leftButton = (Button) findViewById(R.id.left_button); leftButton.setOnClickListener(new View.OnClickListener(){ public void onClick(View view){ Drawable imagePrev = getResources().getDrawable((int)picPrevItem); m_vwImage.setImageDrawable(imagePrev); if(--picPosition<0) picPosition = 21; picPrevItem = (ImageAdapter.getPrevItemId(picPosition)); picNextItem = (ImageAdapter.getNextItemId(picPosition)); } }); Button rightButton = (Button) findViewById(R.id.right_button); rightButton.setOnClickListener(new View.OnClickListener(){ public void onClick(View view){ Drawable imageNext = getResources().getDrawable((int)picNextItem); m_vwImage.setImageDrawable(imageNext); if(++picPosition>21) picPosition = 0; picNextItem = (ImageAdapter.getNextItemId(picPosition)); picPrevItem = (ImageAdapter.getPrevItemId(picPosition)); } }); } 

}

HelloGridView greeting:

 <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.hellogridview" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" /> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" > <activity android:name=".HelloGridViewActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".PictureViewer" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.VIEW" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </activity> </application> </manifest> 
+8
source share

All Articles