首先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>
DetialGallery 是自己继承的类。
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;
}
}
}
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;
}
}
让Android自带的Gallery实现多点缩放,拖动和边界回弹效果,效果流畅
http://www.havenliu.com/android/668.html