先創建一個文件夾/res/drawable, 並複製一些PNG圖形到/res/drawable內. 我使用的文件名為a01.png~a15.png.
修改main.xml, 在Gallery外多加一個圖像視圖(ImageView).
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Gallery
android:id="@+id/gallery"
android:layout_width="fill_parent"
android:layout_height="100dp"
android:spacing="10dp"
/>
<ImageView
android:id="@+id/imageview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>
修改源碼.
package com.AndroidGallery;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
public class AndroidGallery extends Activity{
private ImageView myImageView;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myImageView = (ImageView)findViewById(R.id.imageview);
Gallery myGallery = (Gallery)findViewById(R.id.gallery);
myGallery.setAdapter(new ImageAdapter(this));
myGallery.setOnItemSelectedListener(myGalleryOnItemSelectedListener);
}
private OnItemSelectedListener myGalleryOnItemSelectedListener
= new OnItemSelectedListener(){
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
myImageView.setImageResource(mThumbIds[arg2]);
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
};
private Integer[] mThumbIds = {
R.drawable.a01,
R.drawable.a02,
R.drawable.a03,
R.drawable.a04,
R.drawable.a05,
R.drawable.a06,
R.drawable.a07,
R.drawable.a08,
R.drawable.a09,
R.drawable.a10,
R.drawable.a11,
R.drawable.a12,
R.drawable.a13,
R.drawable.a14,
R.drawable.a15,
};
public class ImageAdapter extends BaseAdapter{
private Context context;
public ImageAdapter(Context c){
context = c;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return mThumbIds.length;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return mThumbIds[position];
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ImageView imageView;
if (convertView == null) {
// if it's not recycled, initialize some attributes
imageView = new ImageView(context);
imageView.setLayoutParams(new Gallery.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(8, 8, 8, 8);
} else {
imageView = (ImageView) convertView;
}
imageView.setImageResource(mThumbIds[position]);
return imageView;
}
}
}
參考另一篇: "畫廊(Gallery), 及有一點動畫效果的圖像切換器(ImageSwitcher)"
最近想做一個圖片瀏覽器
回覆刪除但是是做成電子書的形式
想不出好方法可以一次存大量圖檔至陣列裡
想不透有其他方式可以用for自動把數百張png的id存起來
R.java的id都是變數名 且也都是位置
為一目前能想到的就是用位置做運算
想問有沒有更好的方法