buat lah file berextensi .xml di folder layout <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/layout_followed_top" android:layout_width="fill_parent" android:layout_height="fill_parent" android:weightSum="1" android:orientation="vertical" > <GridView android:id="@+id/gridview" android:layout_width="match_parent" android:layout_height="match_parent" android:numColumns="auto_fit" android:verticalSpacing="10dp" android:horizontalSpacing="10dp" android:stretchMode="columnWidth" android:gravity="center" > </GridView> </LinearLayout>
Buat satu layout lagi di folder layout dengan nama singleimage.xml , untuk menampilkan gambar dalam bentuk satuan, lihat code :
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <ImageView android:id="@+id/singleimage" android:layout_width="wrap_content" android:layout_height="0dip" android:contentDescription="gambar tunggal" android:layout_gravity="center_horizontal|center_vertical" android:layout_weight="1" /> </LinearLayout>
Tahap berikutnya adalah kita akan membuat service sound , sehingga apabila gambar tsb di pencet akan mengeluarkan bunyi binatang sesuai dengan yang telah kita download, kita akan buat satu class dengan nama SoundBase.java, dengan memakai media player : lihat contoh code :
package com.alcytrite.suarabinatang; import android.app.Service; import android.content.Intent; import android.media.MediaPlayer; import android.os.Bundle; import android.os.IBinder; public class SoundBase extends Service { MediaPlayer media; public static int[] resource = { R.raw.kodok, R.raw.tokek, R.raw.burung, R.raw.macan }; @Override public IBinder onBind(Intent intent) { // TODO Auto-generated method stub return null; } @Override public void onCreate() { media = new MediaPlayer(); } @Override public int onStartCommand(Intent intent, int flags, int startId) { // TODO do something useful Bundle extras = intent.getExtras(); int position = (Integer) extras.get("position"); media = MediaPlayer.create(this, SoundBase.resource[position]); media.setLooping(false); media.start(); // Toast.makeText(this,position,Toast.LENGTH_LONG).show(); return Service.START_STICKY; } @Override public void onDestroy() { media.stop(); media.release(); } }
Untuk menampilkan Image yang telah kita download maka kita buat class ImageAdapter etends BaseAdapter , ini untuk memudahkan kita dalam hal pengaturan urutan Image , lihat contoh code :
package com.alcytrite.suarabinatang; 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; // constructor public ImageAdapter(Context c) { mContext = c; } public static Integer[] mThumbsId = { R.drawable.kodok, R.drawable.tokek, R.drawable.burung, R.drawable.macan }; public static String[] thumbString = { "Kodok keok", "Tokek Koek", "Suara Burung berkicau", "macan Ngamuk" }; @Override public int getCount() { // TODO Auto-generated method stub return mThumbsId.length; } @Override public Object getItem(int position) { // TODO Auto-generated method stub return null; } @Override public long getItemId(int position) { // TODO Auto-generated method stub return 0; } @Override public View getView(int position, View view, ViewGroup group) { // TODO Auto-generated method stub ImageView imageView; if (view == null) { imageView = new ImageView(mContext); int h = mContext.getResources().getDisplayMetrics().densityDpi; imageView .setLayoutParams(new GridView.LayoutParams(h - 45, h - 45)); imageView.setScaleType(ImageView.ScaleType.CENTER_CROP); imageView.setPadding(1, 1, 1, 1); } else { imageView = (ImageView) view; } imageView.setImageResource(mThumbsId[position]); return imageView; } }
Untuk menampilkan Image dalam bentuk satuan , kita buat SingleImage.java , berikut contoh code nya :
package com.alcytrite.suarabinatang; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.ImageView; public class SingleImage extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.singleimage); int position = this.getIntent().getExtras().getInt("position"); ImageView iv = (ImageView) findViewById(R.id.singleimage); int image = ImageAdapter.mThumbsId[position]; iv.setImageResource(image); final Intent intent = new Intent(this, SoundBase.class); intent.putExtra("position", position); startService(intent); iv.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub stopService(intent); finish(); } }); } }
Image dan sound service class sudah kita buat, sekarang kita ubah di MainActivity.java , dimana kita akan tampilkan image , dan kita tambahkan event kalau di klik image tsb maka akan muncul image dalam bentuk satuan dan bunyi suara dari image tsb.
package com.alcytrite.suarabinatang; import android.os.Bundle; import android.app.Activity; import android.content.Intent; import android.view.View; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.GridView; import android.widget.Toast; public class MainActivity extends Activity implements OnItemClickListener { GridView gridView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Toast.makeText(getBaseContext(), "Hai, Suara binatang akan tampil...",Toast.LENGTH_LONG).show(); setContentView(R.layout.activity_main); gridView = (GridView) findViewById(R.id.gridview); gridView.setAdapter(new ImageAdapter(this)); gridView.setOnItemClickListener(this); } @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { // TODO Auto-generated method stub Toast.makeText(getBaseContext(), ImageAdapter.thumbString[position],Toast.LENGTH_SHORT).show(); Intent i = new Intent(this,SingleImage.class); Bundle b = new Bundle(); b.putInt("position",position); i.putExtras(b); startActivity(i); } @Override public void onDestroy() { super.onDestroy(); Runtime.getRuntime().gc(); } }
Jangan lupa di file manifest anda perlu tambahkan activity lagi class SingleImage yang telah kita buat dan tambahkan service soundnya.
Anda bisa lihat contoh :
Anda bisa lihat contoh :
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.alcytrite.suarabinatang" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.alcytrite.suarabinatang.MainActivity" 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=".SingleImage" android:label="@string/app_name" > </activity> <service android:name="SoundBase" /> </application> </manifest>
Comments
Post a Comment
terima kasih telah berpartisipasi pada blog kami