Gallery和ImageSwitcher ---------- 製作圖片瀏覽器
阿新 • • 發佈:2019-02-01
Gallery和ImageSwitcher
Gallery
- 設定資料來源,Gallery 佈局;
- 例項化 gallery;
- 自定義 ImageAdapter 圖片介面卡,並完成介面卡的成員方法;
- 例項化 ImageAdapter 圖片介面卡;
- gallery載入介面卡。
MainActivity.java
package com.example.administrator.stu_garry_imageswitcher;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget .Gallery;
public class MainActivity extends AppCompatActivity {
private int [] res = {R.drawable.item1,R.drawable.item2,R.drawable.item3,
R.drawable.item4,R.drawable.item5,R.drawable.item6,R.drawable.item7,
R.drawable.item8,R.drawable.item9,R.drawable.item10,R.drawable.item 11,
R.drawable.item12}; //設定資料來源
private ImageAdapater adapater;
private Gallery gallery;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout .activity_main);
gallery = (Gallery) findViewById(R.id.gallery); //例項化gallery
adapater = new ImageAdapater(res,this);
gallery.setAdapter(adapater); //載入介面卡
}
}
Imagedapter.java
package com.example.administrator.stu_garry_imageswitcher;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
public class ImageAdapater extends BaseAdapter{
private int []res;
private Context context;
ImageAdapater(int[] res, Context context){ //通過建構函式傳遞對應的資料
this.res = res;
this.context = context;
}
//返回資料來源檔案中的內容數目
@Override
public int getCount() { //控制可同一方向滑動的最大值
return res.length; //返回資料來源陣列的大小
//return Integer.MAX_VALUE; //設定資料來源大小為整形最大值
}
//獲取圖片內容
@Override
public Object getItem(int position) {
return res[position]; //返回當前圖片內容
}
//獲取圖片ID
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView = new ImageView(context);
imageView.setBackgroundResource(res[position%res.length]); //設定縮圖片
imageView.setLayoutParams(new Gallery.LayoutParams(400,300)); //設定縮圖的寬、高
imageView.setScaleType(ImageView.ScaleType.FIT_XY); //設定縮放模式
return imageView;
}
}
ImagerSwitcher
MainActivity.java
...implements AdapterView.OnItemSelectedListener,ViewSwitcher.ViewFactory{ //ImageSwitch 使用ViewFactory 顯示圖片
...
private ImageSwitcher imageSwitcher;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gallery = (Gallery) findViewById(R.id.gallery);
adapater = new ImageAdapater(res,this);
imageSwitcher = (ImageSwitcher) findViewById(R.id.switcher);
gallery.setAdapter(adapater);
gallery.setOnItemSelectedListener(this);
imageSwitcher.setFactory(this);
}
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
imageSwitcher.setBackgroundResource(res[position]);
//設定進入動畫效果--淡入
imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,android.R.anim.fade_in));
//設定進入動畫效果--淡出
imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,android.R.anim.fade_out));
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
@Override
public View makeView() { //設定 ViewFactory
ImageView imageView = new ImageView(this);
imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
return imageView;
}
}