自定义Gallery

未分类 , , , , , 发表评论

1. <com.socogame.MyGallery
       android:id=”@+id/head_gallery1″
       android:layout_width=”fill_parent”
       android:layout_height=”fill_parent”
       android:spacing=”0dip”

        />

2. MyGallery g = (MyGallery)galleryView.findViewById(R.id.head_gallery1);
        g.setAdapter(new ImageAdapter(this));
        g.setFadingEdgeLength(0);
        g.setSelection(200);

3.自定义Gallery  

package com.socogame.common;
/*
 * ����ԭ�й��Թ����Ĺ���
 * */
import android.content.Context;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.widget.Gallery;

public class MyGallery extends Gallery 
{
public MyGallery(Context context) {
 super(context);
 // TODO Auto-generated constructor stub
}

public MyGallery(Context context, AttributeSet attrSet) { 
super(context,attrSet); 
 // TODO Auto-generated constructor stub 


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

// @Override
// public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
//  // TODO Auto-generated method stub
//  return false;
// }

public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
// TODO Auto-generated method stub  
int kEvent;  

1. <com.socogame.MyGallery
       android:id="@+id/head_gallery1"
       android:layout_width="fill_parent"
       android:layout_height="fill_parent"
       android:spacing="0dip"
        />

2. MyGallery g = (MyGallery)galleryView.findViewById(R.id.head_gallery1);
        g.setAdapter(new ImageAdapter(this));
        g.setFadingEdgeLength(0);
        g.setSelection(200);

3.自定义Gallery  
package com.socogame.common;
/*
 * ����ԭ�й��Թ����Ĺ���
 * */
import android.content.Context;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.widget.Gallery;


public class MyGallery extends Gallery 
{
public MyGallery(Context context) {
 super(context);
 // TODO Auto-generated constructor stub
}


public MyGallery(Context context, AttributeSet attrSet) { 
super(context,attrSet); 
 // TODO Auto-generated constructor stub 
} 



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

// @Override
// public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
//  // TODO Auto-generated method stub
//  return false;
// }

public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
// TODO Auto-generated method stub  
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;    
}

private boolean isScrollingLeft(MotionEvent e1, MotionEvent e2){  
return e2.getX() > e1.getX();  
}  
}

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;    
}

private boolean isScrollingLeft(MotionEvent e1, MotionEvent e2){  
return e2.getX() > e1.getX();  
}  
}

  

发表评论

邮箱地址不会被公开。 必填项已用*标注

昵称 *