Creating Image Gallery - Android app (in tamil)

689 views

Published on

Dr. Ramkumar Lakshminarayanan article published in Tamil Computer Magazine. The content is about creating Android Application - Image Gallery

Published in: Mobile
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
689
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
59
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Creating Image Gallery - Android app (in tamil)

  1. 1. ݇†ó£Œ®™ ðìƒè¬÷ ¬èò£À õ ðô º¬øèœ àœ÷ù. ÜõŸ Áœ å¼ º¬ø «èôK (Gallery) ðò¡ð´ˆ¶ õ‹. ºîL™ «èôK â¡ø Šó£ªü‚†®¬ù à¼õ£‚è «õ‡´‹. Þ‰î Šó£ªü‚†®¡ «ï£‚è‹ å¼ Cô ðìƒè¬÷ õK¬êò£è F¬óJ™ 裇Hˆ¶, ãî£õ¶ ðìˆF¬ù AO‚ ªêŒî¾ì¡ F¬óJ™ îèõL¬ù 裇HŠð«îò£°‹. ÜŠO«èû¡ â‚OŠv ªè£‡´ à¼õ£‚°A«ø£‹. â‚OŠvC™ à¼õ£‚èŠð´‹ ÜŠO«èê¡ è¬÷ å¼ å˜‚v«ðC™ (Work Space) «êIˆ¶ ¬õ‚°‹. â¡Â¬ìò ÜŠO«èê¡èœ âù¶ èEQJ™ C:usersmeeraneelaworkspace â¡ø ç«ð£™ìK™ «êI‚èŠð´A¡øù. (ðì‹-1). à¼õ£‚èŠð´‹ Šó£ªü‚†´èœ ç«ð£™ìó£è «êI‚èŠð´‹. Þƒ° Gallery â¡ø ç«ð£™ì˜ Þ¼Šðî¬ù è£íô£‹. Gallery/res â¡ø ç«ð£™ìK¬ù à¼õ£‚A Sample.jpg, Sample2.jpg,sample3.jpg,sample4. jpg,sample5.jpg,sample6.jpg ñŸÁ‹ Sample <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> <Gallery android:id="@+id/gallery" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </LinearLayout> ðì‹-1 ðì‹-2 Gó™-1 ðìƒè¬÷ ¬èò£À‹ «èôK!
  2. 2. Ý¡†ó£Œ´ -2 Þ󣋰ñ£˜ ªô†²Iï£ó£òí¡ 15 7.jpg â¡Á ªðò¼¬ìò ðìƒè¬÷ «êIˆ F´ƒèœ. (ðì‹-2) Ü´ˆîî£è «èôK ÝŠªü‚†®¬ù main. xml -™ ðò¡ð´ˆî «õ‡´‹. main.xml-Ÿ è£ù¶ Gó™ -1-™ àœ÷¶ «ð£¡Á Þ¼‚°‹. Ü´ˆîî£è Šó£ªü‚† à¼õ£‚èŠð†ì ¾ì¡ «î£¡P»œ÷ Gallery Activity.java ç¬ðL™ àœ÷ OnCreate ªñˆî®™ Gó™-2™ àœ÷¶ «ð£¡Á Gó™è¬÷ Þ¬í‚è «õ‡ ´‹. ºîL™ main.xml-™ àœ÷ «ôܾ†®¬ù Content View â¡Á ܬñ‚A«ø£‹. Ü´ˆîî£è «èôK ÝŠªü‚†®¬ù find viewById(R.id.gallery) ðò¡ð´ˆF «ôܾ†´ ì¡ Þ¬íˆFì «õ‡´‹. «èôKJ™ ðò¡ 𴈶õîŸè£è BaseAdapter å¡P¬ù ༠õ£‚è «ð£A«ø£‹. Üî¡ ªðò˜ Image Adapter â¡ð‹. Ü´ˆîî£è «èôKJ™ SetOnItemClick Listener ðò¡ð´ˆF AO‚ ªêŒõîù£™ îèõ™ F¬óJ™ «î£¡Áñ£Á ܬñˆ¶œ «÷£‹. îèõ™ F¬óJ™ 裇H‚è Toast ðò¡ ð´ˆîŠð†´œ÷¶. Ü´ˆîî£è «èôKJ™ ðò¡ð´ˆîŠð´‹ v¬ìL¬ù (Style) °PŠH´õîŸè£è attrs. xml â¡ø Xml ç¬ðL¬ù ðò¡ð´ˆî «ð£A «ø£‹. attrs.xml-Ÿè£ù Gó™ -3™ ªè£´‚ Gallery gallery = (Gallery)findViewById(R.id.gallery); gallery.setAdapter(new ImageAdapter(this)); gallery.setOnItemClickListener(new onItemClickListener(){ @Override public void onItemClick(AdapterView<?> parent, View v, int position, long id) { // TODO Auto-generated method stub Toast.makeText(GalleryActivity.this,""+ position, Toast.LENGTH_SHORT).show(); }} ); <?xml version="1.0" encoding="utf- 8"?> <resources> <declare-styleable name="HelloGallery"> <attr name="android:galleryItemBackground "/> </declare-styleable> </resources> èŠð†´œ÷¶. <attr> âLªñ¡† Íô‹ v¬ìL¬ù android.gallery ItemBack ground â¡Á °PŠH†´œ«÷£‹. Ü´ˆîî£è ImageAdapter.java â¡ø ç¬ðL¬ù à¼õ£‚è «õ‡´‹. à¼õ£‚èŠ ð´‹«ð£¶ «ðv Ü죊ìK¬ù (Base Adapter) Extend ªêŒò «õ‡´‹. (Gó™-4) Array- J¬ù ªè£‡´ drawable Folder- ™ ެ특 ãŸð´ˆî «õ‡´‹. Ü´ˆîî£è Class Constructor ðò¡ð´ˆîŠð†´œ÷¶. Gó™-2 Gó™-3 ðì‹-3 ðì‹-4
  3. 3. package ram.com.gallery; import android.content.Context; import android.content.res.TypedArray; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.Gallery; import android.widget.ImageView; public class ImageAdapter extends BaseAdapter { int mGalleryItemBackground; private Context mContext; private Integer[] mImageIds ={ R.drawable.sample1, R.drawable.sample2, R.drawable.sample3, R.drawable.sample4, R.drawable.sample5, R.drawable.sample6, R.drawable.sample7 }; public ImageAdapter(Context c){ mContext = c; TypedArray attr = mContext.obtainStyledAttributes(R.styleab le.HelloGallery); mGalleryItemBackground = attr.getResourceId(R.styleable.HelloGaller y_android_galleryItemBackground, 0); attr.recycle(); } @Override public int getCount() { // TODO Auto-generated method stub return mImageIds.length; } @Override public Object getItem(int position) { // TODO Auto-generated method stub return mImageIds[position]; } @Override public long getItemId(int position) { // TODO Auto-generated method stub return mImageIds[position]; } @Override public View getView(int position, View convertView, ViewGroup parent) { // TODO Auto-generated method stub ImageView imageView = new ImageView(mContext); imageView.setImageResource(mImageIds [position]); imageView.setLayoutParams(new Gallery.LayoutParams(150,100)); imageView.setScaleType(ImageView.Scal eType.FIT_XY); imageView.setBackgroundResource(mGall eryItemBackground); return imageView; } } Gó™-4 Þƒ° Þ«ñx Ü죊켂 è£ù Context õ¬óòÁ‚èŠ ð†´œ÷¶. Ü´ˆîî£è style able resource â¡ðî¬ù attr â¡ø çd™´ (Field) Íô‹ °PŠHìŠ ð†´œ÷¶. Constructor- ¡ º®M™ Typed Array -J¬ù ®¬õv e‡´‹ ðò¡ð´ˆîô£‹ â¡ðîŸè£è recycle() ðò¡ð´ˆîŠð† ´œ÷¶. Þ«ñT¬ù «èôK»ì¡ ެ특 ãŸð´ˆ¶õ getCount(),getItem() ñŸÁ‹ getIte mId() ªñˆî†èœ ðò¡ ð´ˆîŠð†´œ÷¶. getview ªñˆî† Íô‹ ContextMember Íô‹ Þ«ñx MÎ à¼õ£‚èŠ ð†´œ÷¶. Þ«ñx MÎMŸ° drawble- L™ àœ÷ ðìƒèÀì¡ Þ¬íŠ¹ ãŸð´ˆîŠð†´œ ÷¶. imageView.setImageResourc e(mImageIds [position]; Þ«ñx MÎM¡ c÷ ñŸÁ‹ ÜèôˆF¬ù Gallery.Layout params Íô‹ °PŠH†´œ «÷£‹.
  4. 4. ðì‹-6ðì‹-5 - ªî£ì¼‹ i m a g e V i e w . S e t L a y o u t Params (new Gallery. Layout Params(150,100); Þî¬ù «ð£¡«ø, SetScaleType ñŸÁ‹ SetBack groundResource ðò¡ð´ˆîŠ ð†´œ÷¶. ÜŠO«èê¬ù ªêò™ð´ˆ¶õ ºîL™ Project-Build Project â¡ð î¬ù AO‚ ªêŒò «õ‡´‹. H¬öèœ ã«î‹ Þ¼‰î£™ Þ‰î ªêò™ Íô‹ ÜP‰¶ ªè£œ÷ô£‹. Ü´ˆîî£è «èôK â¡ø Šó£ªü‚†®¬ù «î˜¾ ªêŒ¶ Run As>Android Appli cation â¡Á AO‚ ªêŒò «õ‡´‹. (ðì‹-3) Run As> Android Application â¡ðî¬ù «î˜¾ ªêŒî¾ì¡ ðì‹-4™ àœ÷¶ «ð£¡Á «î£¡Á‹. «î¬õò£ù ⺠«ô†ìK¬ù«ò£ (Emulator) Ü™ô¶ ®¬õC¬ù«ò£ «î˜‰ ªî´ˆ¶ ªè£œ÷ô£‹. «èôK ÜŠO«èê¡ ðì‹-5™ àœ÷¶ «ð£¡Á F¬óJ™ «î£¡Á‹ ðìˆF¬ù ®¬õvªî£´F¬óJ™ªî£† 죙 ðìˆF¡ ⇠F¬óJ™ «î£¡Á‹. (ðì‹-6)

×