`
mengsina
  • 浏览: 188254 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

android开发之gallery 实现滚动一张且短距离滑动实现滚动(转)

阅读更多
转自:http://hi.baidu.com/lvqiyong/blog/item/afbaad3daa102ff454e72390.html
首先gallery的特点就不用多说了吧,惯性滚动、半屏翻页,但是很多时候我们不需要它的这些特性。我今天就介绍一下 去掉惯性滚动 以及 短距离翻页的实现:
代码先晒出来:
main.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.mh.DetialGallery  android:id="@+id/gallery" android:layout_width="fill_parent"
        android:layout_height="fill_parent"  />
</LinearLayout>

注意红色的地方 是自己继承的类。

main.java:
package com.mh;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;

public class main extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
      
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN ,
     WindowManager.LayoutParams.FLAG_FULLSCREEN);

        setContentView(R.layout.main);
        Gallery gallery = (Gallery) findViewById(R.id.gallery); 
        gallery.setAdapter(new ImageAdapter(this));; 

    }
  
    private int[] resIds = 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  
         }; 
   
   
  

public class ImageAdapter extends BaseAdapter 
    { 
        int mGalleryItemBackground; 
        private Context mContext; 
    
        public ImageAdapter(Context context) 
        { 
            mContext = context;                    
        } 

        public int getCount() 
        { 
            return resIds.length; 
        } 
        public Object getItem(int position) 
        { 
            return position; 
        } 
    
        public long getItemId(int position) 
        { 
            return position; 
        } 
      
        public View getView(int position, View convertView, ViewGroup parent) 
        { 
            ImageView imageView = new ImageView(mContext); 
            imageView.setImageResource(resIds[position]);
            imageView.setScaleType(ImageView.ScaleType.FIT_XY); 
            imageView.setLayoutParams(new Gallery.LayoutParams(480, 800));   //分辨率自己定
            return imageView; 
        } 
    }

}


DetialGallery.java:
package com.mh;

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

public class DetialGallery extends Gallery {

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

private boolean isScrollingLeft(MotionEvent e1, MotionEvent e2)
   { 
    return e2.getX() > e1.getX();
   }
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
   float velocityY) {
  // TODO Auto-generated method stub
//  return super.onFling(e1, e2, 0, velocityY);//方法一:只去除翻页惯性
//  return false;//方法二:只去除翻页惯性  注:没有被注释掉的代码实现了开始说的2种效果。
  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;
  }
}

如何在Gallery中每次滑动只显示一页
2011-01-20 20:09:50

要求在Gallery中每次滑动最多显示一页。解决方法是重载onfling函数。
有两种方法,可以在onfling中检测X轴的速度,如果大于一定值,将其减少为只能移动一页的速度。但是这个滑动一页的速度比较难掌握,据我估计在500到700之间。还有一种方法是直接调用gallery的setSelection来跳到下一页,但是这样做的缺点是没有了页间动画。

因为屏幕分辨率问题,检测速度是否大于一定值我的计算方法为
        // Convert the dips to pixels
    float scale = getResources().getDisplayMetrics().density;
    FLINGTHRESHOLD = (int) (20.0f * scale + 0.5f);

这是我的onFling函数:
public boolean onFling (MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
Log.d("OnePageGallery:onFling:",String.format("VX:%f, VY:%f", velocityX, velocityY));
// cap the velocityX to scroll only one page
if (velocityX>FLINGTHRESHOLD) {
return super.onFling(e1, e2, SPEED, velocityY);
} else if (velocityX<-FLINGTHRESHOLD) {
return super.onFling(e1, e2, -SPEED, velocityY);
} else {
return super.onFling(e1, e2, velocityX, velocityY);
}
}


分享到:
评论
2 楼 mengsina 2012-03-14  
ldci3gandroid 写道
给力 谢谢你吖 帮我了一个大忙

呵呵,我也是共享别人的代码。但是能帮到你很高兴。
1 楼 ldci3gandroid 2012-03-14  
给力 谢谢你吖 帮我了一个大忙

相关推荐

Global site tag (gtag.js) - Google Analytics