当前位置:
首页 >
重写Gallery中方法去处理Gallery滑动时的惯性
发布时间:2023/12/20
65
豆豆
生活随笔
收集整理的这篇文章主要介绍了
重写Gallery中方法去处理Gallery滑动时的惯性
小编觉得挺不错的,现在分享给大家,帮大家做个参考.
2019独角兽企业重金招聘Python工程师标准>>>
Gallery在滑动的时候有一个脱手时候的速度,这个速度将会影响Gallery滑动的惯性,有时候我们需要对这个惯性进行处理,这个时候我们就得自己定义View了,自定义个Gallery继承Gallery,并且重写onFling()方法,如下:
public class GalleryDemo extends Gallery {public GalleryDemo(Context context, AttributeSet attrs) {super(context, attrs);// TODO Auto-generated constructor stub}@Overridepublic boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {// TODO Auto-generated method stubreturn super.onFling(e1, e2, velocityX / 4, velocityY);}}值得注意的是自己定义控件的时候得使用 public GalleryDemo(Context context, AttributeSet attrs)构造方法,要不则会出现莫名其妙的异常。
下面就是去使用这个自定义的Gallery了:
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. */@Overridepublic 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 {@Overridepublic int getCount() {// TODO Auto-generated method stubreturn pics.length;}@Overridepublic Object getItem(int position) {// TODO Auto-generated method stubreturn null;}@Overridepublic long getItemId(int position) {// TODO Auto-generated method stubreturn position;}@Overridepublic View getView(int position, View convertView, ViewGroup parent) {// TODO Auto-generated method stubImageView iv = new ImageView(GalleryDemoActivity.this);iv.setLayoutParams(new Gallery.LayoutParams(160, 320));iv.setImageResource(pics[position]);return iv;}} }
转载于:https://my.oschina.net/keeponmoving/blog/64069
总结
以上是生活随笔为你收集整理的重写Gallery中方法去处理Gallery滑动时的惯性的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: 一个应用程序和另一个应用程序apk文件信
- 下一篇: 重置oracle 11G的system、