可藉由游標的左右移動瀏覽照片 , 並顯示照片資料 , 這題使用到的類別 Gallery 和 Toast提示 , 另外這題所提供了10張png檔 , 而程式安裝到模擬器時會發現10張圖檔要上傳很久 , 所以我只使用6張png , 以下是程式碼。
package COM.TQC.GDD01;
import android.app.Activity;
import android.os.Bundle;
import android.content.Context;
import android.content.res.TypedArray;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.Toast;
public class GDD01 extends Activity
{
Gallery gallery;
int[] drawableResID = {R.drawable.png001 , R.drawable.png002 , R.drawable.png003
, R.drawable.png004 , R.drawable.png005 , R.drawable.png006};
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
gallery = (Gallery) this.findViewById(R.id.mygallery);
ImageAdapter imageAadapter = new ImageAdapter(this);
gallery.setAdapter(imageAadapter);
gallery.setOnItemSelectedListener(OISlistener);
}
Gallery.OnItemSelectedListener OISlistener = new Gallery.OnItemSelectedListener()
{
@Override
public void onItemSelected(AdapterView arg0, View arg1, int arg2,
long arg3)
{
Toast t = Toast.makeText(GDD01.this, getText(R.string.my_gallery_text_pre).toString()
+ arg2 + getText(R.string.my_gallery_text_post).toString(), Toast.LENGTH_SHORT);
t.show();
}
@Override
public void onNothingSelected(AdapterView arg0) {
// TODO Auto-generated method stub
}
};
public class ImageAdapter extends BaseAdapter
{
int backgroundID;
//建構子只有一個參數,即要儲存的Context
public ImageAdapter(Context c)
{
TypedArray arr = c.obtainStyledAttributes(R.styleable.Gallery);
backgroundID = arr.getResourceId(R.styleable.Gallery_android_galleryItemBackground , 0);
arr.recycle();
}
//回傳所有已定義的圖片總數量
public int getCount()
{
return drawableResID.length;
}
public Object getItem(int position)
{
return drawableResID[position];
}
//取得圖片編號
public long getItemId(int position)
{
return position;
}
@Override
public View getView(int arg0, View arg1, ViewGroup arg2)
{
// TODO Auto-generated method stub
ImageView imageView = new ImageView(GDD01.this);
imageView.setImageResource(drawableResID[arg0]);
imageView.setBackgroundResource(backgroundID);
imageView.setScaleType(ScaleType.FIT_XY);
imageView.setLayoutParams(new Gallery.LayoutParams(236,188));
return imageView;
}
}
//使用android.R.drawable裡的圖片當成圖庫來源
}
這題的Layout也很單純 , 只要擺一個Gallery就可以了。
由於Gallery元件不支援循環播放 , 所以如果要讓圖片一直循環下去的話 , 我們要自己動手實作 , 以下是程式碼。
package COM.TQC.GDD01;
import android.app.Activity;
import android.os.Bundle;
import android.content.Context;
import android.content.res.TypedArray;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.Toast;
public class GDD01 extends Activity
{
Gallery gallery;
int[] drawableResID = {R.drawable.png001 , R.drawable.png002 , R.drawable.png003
, R.drawable.png004 , R.drawable.png005 , R.drawable.png006};
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
gallery = (Gallery) this.findViewById(R.id.mygallery);
ImageAdapter imageAadapter = new ImageAdapter(this);
gallery.setAdapter(imageAadapter);
gallery.setOnItemSelectedListener(OISlistener);
}
Gallery.OnItemSelectedListener OISlistener = new Gallery.OnItemSelectedListener()
{
@Override
public void onItemSelected(AdapterView arg0, View arg1, int arg2,
long arg3)
{
Toast t = Toast.makeText(GDD01.this, getText(R.string.my_gallery_text_pre).toString()
+ arg2 % drawableResID.length + getText(R.string.my_gallery_text_post).toString(), Toast.LENGTH_SHORT);
t.show();
}
@Override
public void onNothingSelected(AdapterView arg0) {
// TODO Auto-generated method stub
}
};
public class ImageAdapter extends BaseAdapter
{
int backgroundID;
//建構子只有一個參數,即要儲存的Context
public ImageAdapter(Context c)
{
TypedArray arr = c.obtainStyledAttributes(R.styleable.Gallery);
backgroundID = arr.getResourceId(R.styleable.Gallery_android_galleryItemBackground , 0);
arr.recycle();
}
//回傳所有已定義的圖片總數量
public int getCount()
{
return Integer.MAX_VALUE;
//設定圖片數量為系統最大整數
}
public Object getItem(int position)
{
return drawableResID[position % drawableResID.length];
}
//取得圖片編號
public long getItemId(int position)
{
return position % drawableResID.length;
}
@Override
public View getView(int arg0, View arg1, ViewGroup arg2)
{
// TODO Auto-generated method stub
ImageView imageView = new ImageView(GDD01.this);
imageView.setImageResource(drawableResID[arg0 % drawableResID.length]);
imageView.setBackgroundResource(backgroundID);
imageView.setScaleType(ScaleType.FIT_XY);
imageView.setLayoutParams(new Gallery.LayoutParams(236,188));
return imageView;
}
}
//使用android.R.drawable裡的圖片當成圖庫來源
}
P.S. 題目中所要求的Variable和Method皆會保留 , 也會根據題目所要求的流程去實作 , 縱使題目要求繞遠路....

請問我第66行與67行的(R.styleable.)後面的東西都出現紅色線
回覆刪除要怎麼解決呢??謝謝