Gallery在滑動的時候有一個脫手時候的速度,這個速度將會影響Gallery滑動的慣性,有時候咱們須要對這個慣性進行處理,這個時候咱們就得本身定義View了,自定義個Gallery繼承Gallery,而且重寫onFling()方法,以下:java
public class GalleryDemo extends Gallery { public GalleryDemo(Context context, AttributeSet attrs) { super(context, attrs); // TODO Auto-generated constructor stub } @Override public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { // TODO Auto-generated method stub return super.onFling(e1, e2, velocityX / 4, velocityY); } }
值得注意的是本身定義控件的時候得使用 public GalleryDemo(Context context, AttributeSet attrs)構造方法,要不則會出現莫名其妙的異常。
下面就是去使用這個自定義的Gallery了:ide
public class GalleryDemoActivity extends Activity { private Gallery gl_main; private GalleryAdapter adapter; private int[] pics = new int[] { R.drawable.a1, R.drawable.a2, R.drawable.a3, R.drawable.a4, R.drawable.a5, R.drawable.a6, R.drawable.a7, R.drawable.a8, R.drawable.a9, R.drawable.a10 }; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); gl_main = (Gallery) findViewById(R.id.gl_main); adapter = new GalleryAdapter(); //gl_main. gl_main.setAdapter(adapter); } private class GalleryAdapter extends BaseAdapter { @Override public int getCount() { // TODO Auto-generated method stub return pics.length; } @Override public Object getItem(int position) { // TODO Auto-generated method stub return null; } @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 ImageView iv = new ImageView(GalleryDemoActivity.this); iv.setLayoutParams(new Gallery.LayoutParams(160, 320)); iv.setImageResource(pics[position]); return iv; } } }