読者です 読者をやめる 読者になる 読者になる

duyojiぶろぐ

技術系ときどき日常系

作成

まずはじめにxmlで次のようにlayoutを作る

res/layout/gallery.xml

<?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">
    <com.test.test.ScrollControlGallery 
    	android:layout_width="fill_parent" android:layout_height="fill_parent"
    	android:layout_gravity="center" android:id="@+id/gallery"/>
</LinearLayout>

「com.test.test.ScrollControlGallery」は各々の環境に合わせてパスを変える。


次にActivityで使うカスタムしたGalleryクラスを作る。
このカスタムGalleryはこちらのサイトを参考

ScrollControlGallery

package com.test.test;

import android.content.Context;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.widget.Gallery;

public class ScrollControlGallery extends Gallery {

	public ScrollControlGallery(Context context) {
		super(context);
	}

	public ScrollControlGallery(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	public ScrollControlGallery(Context context, AttributeSet attrs,int defStyle) {
		super(context, attrs, defStyle);
	}

	
			
	private boolean isScrollingLeft(MotionEvent e1, MotionEvent e2){
		return e2.getX() > e1.getX();
	}
	
	@Override
	public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
		setAnimationDuration(600);
	    return super.onScroll(e1, e2, distanceX, distanceY);
	}

	@Override
	public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
	    float velMax = 2500f;
	    float velMin = 1000f;
	    float velX = Math.abs(velocityX);
	    
	    if (velX > velMax)
	      velX = velMax;
	    else if (velX < velMin)
	      velX = velMin;
	    
	    velX -= 600;
	    int k = 500000;
	    int speed = (int) Math.floor(1f / velX * k);
	    setAnimationDuration(speed);

	    int kEvent;
	    if (isScrollingLeft(e1, e2)) {
	      // Check if scrolling left
	      kEvent = KeyEvent.KEYCODE_DPAD_LEFT;
	    } else {
	      // Otherwise scrolling right
	      kEvent = KeyEvent.KEYCODE_DPAD_RIGHT;
	    }
	    onKeyDown(kEvent, null);

	    return true;
	}
	
}

最後にActivityを作る。

GalleryActivity

package com.test.test;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.LinearLayout;

public class GalleryActivity extends Activity { 
	public final int WC = LinearLayout.LayoutParams.WRAP_CONTENT;
	public final int FP = LinearLayout.LayoutParams.FILL_PARENT;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.gallery);                
        ScrollControlGallery gallery = (ScrollControlGallery) findViewById(R.id.gallery); 
        gallery.setSpacing(0);//スクリーンショットがくっついていたらこれで調整する
        gallery.setAdapter(new GalleryAdapter(this, addGalleryImage()));
    }
    
    private ArrayList<Integer> addGalleryImage(){
    	ArrayList<Integer> imageResource = new ArrayList<Integer>();
    	//今回はテストで適当にデフォルトで入っているicon画像を追加
    	imageResource.add(R.drawable.icon);
    	imageResource.add(R.drawable.icon);
    	imageResource.add(R.drawable.icon);
    	return imageResource;
    }
    
 
    private class GalleryAdapter extends BaseAdapter{
    	private Context context;    	
        private ArrayList<Integer> _imageResource = null;
        
        public GalleryAdapter(Context c, ArrayList<Integer> imageResource) {                
            context = c;            
            _imageResource = imageResource;
        }
    	
		@Override
		public int getCount() {
			return _imageResource.size();
		}

		@Override
		public Object getItem(int position) {
			return _imageResource.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ImageView image = new ImageView(context);
			image.setImageResource(_imageResource.get(position));
			//300,400にしたのはスクリーンショットっぽくするため
			image.setLayoutParams(new Gallery.LayoutParams(300, 400));
			return image;
		}
   	
    }
}

完成画像はこちら