谢谢!!在尝试了很多之后。我找到了一个解决方案:我使用了 Gallery 小部件并对其进行了自定义以达到我的目的。
//自定义图库类:SlowGallery.java
public class SlowGallery extends Gallery {
public SlowGallery(Context context, AttributeSet attrs, int defStyle)
{
super(context, attrs, defStyle);
}
public SlowGallery(Context context, AttributeSet attrs)
{
super(context, attrs);
}
public SlowGallery(Context context)
{
super(context);
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY)
{
//limit the max speed in either direction
if (velocityX > 400.0f)
{
velocityX = 400.0f;
}
else if(velocityX < 400.0f)
{
velocityX = -400.0f;
}
return super.onFling(e1, e2, velocityX, velocityY);
//return false;
}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY)
{
return false;
}
}
现在在您的主活动类中,在 oncreate 函数中添加以下代码:
Gallery gallery = (Gallery) findViewById(R.id.gallery);
gallery.setAdapter(new ImageAdapter(this));
然后将 ImageAdapter 类创建为:
public class ImageAdapter extends BaseAdapter {
int mGalleryItemBackground;
private Context mContext;
private String[] mText = {
"View 1","View 2", "View 3", "View 4"
};
public ImageAdapter(Context c) {
mContext = c;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return mText.length;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return 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
TextView textview = new TextView(mContext); textview.setTextColor(Color.WHITE);
textview.setText(mText[position]);
textview.setFocusable(true);
textview.setTextSize(16);
textview.setLayoutParams(new Gallery.LayoutParams(230, 100));
textview.setBackgroundResource(mGalleryItemBackground);
changePosition(position, textview);
return textview;
}
而已!!:)