how to display the image in desired size at the desired x,y

All your problems with Audio, Video and Images.

how to display the image in desired size at the desired x,y

Postby lakshmanraob » Mon Apr 13, 2009 7:20 am

Hi all,
I am working on a photo album application. IN which i need to display the images of the gallery in small icons at the starting of the application. The same image when chosen by the user then it should display in the normal size.
I think this can be done. As i am novice in this android usage, i am not getting the way to do that. can anybody please give some suggestions to achieve that goal
Thanks in advance for your support

lakshman
lakshmanraob
Junior Developer
Junior Developer
 
Posts: 18
Joined: Mon Mar 23, 2009 8:11 pm

Top

Postby stifry » Mon Apr 13, 2009 4:38 pm

You can locate the image via the location of your ImageView in your layout. And to resize the bitmap, you can do something like this:

Code: Select all
public static Bitmap getBitmap(Context context,String photoUriPath) throws Exception {
      Uri photoUri = Uri.parse(photoUriPath);
      InputStream photoStream = context.getContentResolver().openInputStream(photoUri);
      BitmapFactory.Options options = new BitmapFactory.Options();
      options.inSampleSize=2;
      Bitmap photoBitmap = BitmapFactory.decodeStream(photoStream,null,options);
      int h = photoBitmap.getHeight();
      int w = photoBitmap.getWidth();
      if((w>h)&&(w>128)){
         double ratio = 128d/w;
         w=128;
         h=(int)(ratio*h);
      }
      else if((h>w)&&(h>128)){
         double ratio = 128d/h;
         h=128;
         w=(int)(ratio*w);
      }
       Bitmap scaled = Bitmap.createScaledBitmap(photoBitmap, w, h, true);
       photoBitmap.recycle();
       return scaled;
   }
Visit Trackaroo.com!
Trackmaster - Motorsports Lap Timer
Dynomaster - Performance Dyno
Image Image
User avatar
stifry
Developer
Developer
 
Posts: 46
Joined: Tue Feb 17, 2009 7:50 pm

Postby padde » Mon Apr 13, 2009 4:44 pm

Here is a small example. It loads all images in thumbnail size from res/drawable into a GridView.
When you click those thumbnails the biggest possible version (maintain aspect ratio of the image)
gets shown in an extra imageview. Clicking on the big version brings you back to the thumbnail grid.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. package de.gallery.demo;
  3.  
  4.  
  5.  
  6. import java.lang.reflect.Field;
  7.  
  8.  
  9.  
  10. import android.app.Activity;
  11.  
  12. import android.content.Context;
  13.  
  14. import android.os.Bundle;
  15.  
  16. import android.util.DisplayMetrics;
  17.  
  18. import android.view.View;
  19.  
  20. import android.view.ViewGroup;
  21.  
  22. import android.widget.BaseAdapter;
  23.  
  24. import android.widget.GridView;
  25.  
  26. import android.widget.ImageView;
  27.  
  28.  
  29.  
  30. public class MyGallery extends Activity {
  31.  
  32.     private final int ThumbWidth  = 60;
  33.  
  34.     private final int ThumbHeight = 60;
  35.  
  36.     private GridView mGrid;
  37.  
  38.     private int[] allDrawables;
  39.  
  40.     private ImageView large;
  41.  
  42.    
  43.  
  44.     public void onCreate(Bundle savedInstanceState) {
  45.  
  46.         super.onCreate(savedInstanceState);
  47.  
  48.         setContentView(R.layout.main);
  49.  
  50.        
  51.  
  52.         allDrawables = getAllResourceIDs(R.drawable.class);
  53.  
  54.        
  55.  
  56.         large = (ImageView) findViewById(R.id.large);
  57.  
  58.         large.setScaleType(ImageView.ScaleType.FIT_CENTER);
  59.  
  60.         large.setOnClickListener(new View.OnClickListener() {
  61.  
  62.             public void onClick(View v) {
  63.  
  64.                 switchView();
  65.  
  66.             }
  67.  
  68.         });
  69.  
  70.         large.setVisibility(View.GONE);
  71.  
  72.        
  73.  
  74.         DisplayMetrics dm = new DisplayMetrics();
  75.  
  76.         getWindowManager().getDefaultDisplay().getMetrics(dm);
  77.  
  78.        
  79.  
  80.         mGrid = (GridView) findViewById(R.id.grid);
  81.  
  82.         mGrid.setAdapter(new GridAdapter(this, ThumbWidth, ThumbHeight));
  83.  
  84.         mGrid.setNumColumns(dm.widthPixels/ThumbWidth);
  85.  
  86.         mGrid.setColumnWidth(ThumbWidth);
  87.  
  88.     }
  89.  
  90.    
  91.  
  92.     private int[] getAllResourceIDs(Class<?> aClass) {
  93.  
  94.         Field[] IDFields = aClass.getFields();
  95.  
  96.         int[] IDs = new int[IDFields.length];
  97.  
  98.         try {
  99.  
  100.             for(int i = 0; i < IDFields.length; i++)
  101.  
  102.                 IDs[i] = IDFields[i].getInt(null);
  103.  
  104.         } catch (Exception e) { }
  105.  
  106.         return IDs;
  107.  
  108.     }
  109.  
  110.    
  111.  
  112.     private void switchView() {
  113.  
  114.         if(mGrid.getVisibility() == View.VISIBLE) {
  115.  
  116.             mGrid.setVisibility(View.GONE);
  117.  
  118.             large.setVisibility(View.VISIBLE);
  119.  
  120.         } else {
  121.  
  122.             large.setVisibility(View.GONE);
  123.  
  124.             mGrid.setVisibility(View.VISIBLE);
  125.  
  126.         }
  127.  
  128.     }
  129.  
  130.    
  131.  
  132.     class GridAdapter extends BaseAdapter {
  133.  
  134.         private Context mContext;
  135.  
  136.         private int tw;
  137.  
  138.         private int th;
  139.  
  140.        
  141.  
  142.         public GridAdapter(Context c, int thumbw, int thumbh) {
  143.  
  144.             this.mContext = c;
  145.  
  146.             this.tw = thumbw;
  147.  
  148.             this.th = thumbh;
  149.  
  150.         }
  151.  
  152.        
  153.  
  154.         public int getCount() { return allDrawables.length; }
  155.  
  156.         public Object getItem(int position) { return allDrawables[position]; }
  157.  
  158.         public long getItemId(int position) { return position; }
  159.  
  160.        
  161.  
  162.         public View getView(int position, View convertView, ViewGroup parent) {
  163.  
  164.             ImageView iv;
  165.  
  166.             if (convertView == null) {
  167.  
  168.                 iv = new ImageView(mContext);
  169.  
  170.                 iv.setScaleType(ImageView.ScaleType.FIT_XY);
  171.  
  172.                 iv.setLayoutParams(new GridView.LayoutParams(tw, th));
  173.  
  174.                 iv.setId(position);
  175.  
  176.                 iv.setOnClickListener(new View.OnClickListener() {
  177.  
  178.                     public void onClick(View v) {
  179.  
  180.                         large.setImageResource(allDrawables[v.getId()]);
  181.  
  182.                         switchView();
  183.  
  184.                     }
  185.  
  186.                 });
  187.  
  188.             } else { iv = (ImageView) convertView; }
  189.  
  190.                 iv.setImageResource(allDrawables[position]);
  191.  
  192.             return iv;
  193.  
  194.         }
  195.  
  196.     }    
  197.  
  198. }
  199.  
  200.  
Parsed in 0.045 seconds, using GeSHi 1.0.8.4


here the layout i use
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1.  
  2. <?xml version="1.0" encoding="utf-8"?>
  3.  
  4. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  5.  
  6.    android:orientation="vertical"
  7.  
  8.    android:layout_width="fill_parent"
  9.  
  10.    android:layout_height="fill_parent">
  11.  
  12.         <GridView
  13.  
  14.             android:id="@+id/grid"
  15.  
  16.             android:layout_width="fill_parent"
  17.  
  18.             android:layout_height="fill_parent"
  19.  
  20.             android:horizontalSpacing="5px"
  21.  
  22.             android:verticalSpacing="5px"
  23.  
  24.             android:stretchMode="columnWidth"
  25.  
  26.         />    
  27.  
  28.        
  29.  
  30.         <ImageView
  31.  
  32.             android:id="@+id/large"
  33.  
  34.        android:layout_width="fill_parent"
  35.  
  36.        android:layout_height="fill_parent"
  37.  
  38.         />
  39.  
  40. </LinearLayout>
  41.  
  42.  
Parsed in 0.002 seconds, using GeSHi 1.0.8.4


Greets
Padde
padde
Master Developer
Master Developer
 
Posts: 443
Joined: Wed Apr 08, 2009 4:52 pm

Postby lakshmanraob » Tue Apr 14, 2009 10:27 am

hi padde,
Thank you very much for the code. I am really thank ful to you.
I had one doubt is this possible to overlap the Thumbnails and display them with different sizes and in different places.
please let me know how to do that
lakshmanraob
Junior Developer
Junior Developer
 
Posts: 18
Joined: Mon Mar 23, 2009 8:11 pm

Top

Return to Multimedia Problems

Who is online

Users browsing this forum: No registered users and 9 guests