BaseAdapter + Gallery
galleryAdapter.java
package com.example.administrator.gallery;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
/**
* Created by Administrator on 2015/7/23.
*/
public class galleryAdapter extends BaseAdapter{
private int[] resId;
private Context mContext;
public galleryAdapter(int[] resId,Context mContext) {
this.resId = resId;
this.mContext = mContext;
}
@Override
public int getCount() {
// return resId.length;
return Integer.MAX_VALUE; //讓gallery迴圈,所以設定為最大數
}
@Override
public Object getItem(int position) {
return resId[position];
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView img = new ImageView(mContext);
// img.setBackgroundResource(resId[position]);
img.setBackgroundResource(resId[position % resId.length]); //迴圈
//設定縮放
// img.setLayoutParams(new Gallery.LayoutParams(300,400));
// img.setScaleType(ImageView.ScaleType.FIT_XY);
return img;
}
}
MainActivity.java
package com.example.administrator.gallery;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.Gallery;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.ViewSwitcher;
public class MainActivity extends ActionBarActivity implements ViewSwitcher.ViewFactory{
private Gallery gallery;
private ImageSwitcher switcher;
private int[] resId = new int[]{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.item11,R.drawable.item12};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gallery = (Gallery)findViewById(R.id.gallery);
switcher = (ImageSwitcher)findViewById(R.id.switcher);
galleryAdapter adapter = new galleryAdapter(resId,this);
gallery.setAdapter(adapter);
gallery.setOnItemSelectedListener(new Gallery.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// img.setBackgroundResource(resId[position % resId.length]); //迴圈
switcher.setBackgroundResource(resId[position % resId.length]);
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
//ImageSwitcher載入工廠
switcher.setFactory(this);
//動畫效果
switcher.setInAnimation(AnimationUtils.loadAnimation(this,android.R.anim.fade_in));
switcher.setOutAnimation(AnimationUtils.loadAnimation(this,android.R.anim.fade_out));
}
@Override
public View makeView() {
ImageView img = new ImageView(this);
img.setScaleType(ImageView.ScaleType.FIT_CENTER);
return img;
}
}
相關文章
- Android BaseAdapterAndroidAPT
- arrayadapter simpleadapter baseadapterAPT
- react元件(react-grid-gallery)React元件
- Android gallery 3D效果Android3D
- Android 自定義 BaseAdapter 最佳實踐AndroidAPT
- HNM Web Gallery Creator V2.12Web
- 孟老闆 BaseAdapter封裝(五) ListAdapterAPT封裝
- 小程式:無限自動滾動的Gallery
- 用RecyclerView做一個小清新的Gallery效果View
- Android-Gallery[使用C# And Java實現]AndroidC#Java
- Android UI控制元件系列:Gallery(畫廊檢視)AndroidUI控制元件
- 在Android Studio中利用List Fragments建立相簿GalleryAndroidFragment
- 處理好item點選事件的gallery(畫廊)效果(無bug)事件
- 使用 RecyclerView 實現 Gallery 畫廊效果,並控制 Item 停留位置View
- JavaScript 5/30:Flex Panel Gallery即FLEX佈局的圖片庫JavaScriptFlex
- android開發 BaseAdapter中getView()裡的3個引數的含義AndroidAPTView
- android開發 BaseAdapter中getView()裡的3個引數是什麼意思AndroidAPTView
- 探索Matplotlib-Gallery:Python資料視覺化的遊樂園Python視覺化
- Android開發 - (介面卡)Adapter類中BaseAdapter實現類詳細解析AndroidAPT
- LUT Gallery怎麼用?LUTS調色預設預覽管理外掛使用教程 (FCPX外掛)
- 網站開發必備」8款免費 React Gallery, Lightbox, 和 Photo Viewer開發庫網站ReactView
- QuickPic Gallery Mod – 只有 1.5MB 的相簿應用,快圖第三方編譯版本UIKPI編譯
- android Gallery實現非同步載入網路圖片 並只載入當前停止頁面圖Android非同步