Android 如何停止在Gallery小部件中滚动?
我把一些图片载入了一个图库。现在我可以滚动了,但一旦开始滚动,滚动就不会停止。我想画廊只是滚动到下一个图像,然后停止,直到用户再次做滚动手势 这是我的代码Android 如何停止在Gallery小部件中滚动?,android,scroll,android-widget,gallery,Android,Scroll,Android Widget,Gallery,我把一些图片载入了一个图库。现在我可以滚动了,但一旦开始滚动,滚动就不会停止。我想画廊只是滚动到下一个图像,然后停止,直到用户再次做滚动手势 这是我的代码 import android.widget.ImageView; import android.widget.Toast; import android.widget.AdapterView.OnItemClickListener; public class GalleryExample extends Activity { priva
import android.widget.ImageView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
public class GalleryExample extends Activity {
private Gallery gallery;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
gallery = (Gallery) findViewById(R.id.examplegallery);
gallery.setAdapter(new AddImgAdp(this));
gallery.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView parent, View v, int position, long id) {
Toast.makeText(GalleryExample.this, "Position=" + position, Toast.LENGTH_SHORT).show();
}
});
}
public class AddImgAdp extends BaseAdapter {
int GalItemBg;
private Context cont;
private Integer[] Imgid = {
R.drawable.a_1, R.drawable.a_2, R.drawable.a_3, R.drawable.a_4, R.drawable.a_5, R.drawable.a_6, R.drawable.a_7
};
public AddImgAdp(Context c) {
cont = c;
TypedArray typArray = obtainStyledAttributes(R.styleable.GalleryTheme);
GalItemBg = typArray.getResourceId(R.styleable.GalleryTheme_android_galleryItemBackground, 0);
typArray.recycle();
}
public int getCount() {
return Imgid.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 imgView = new ImageView(cont);
imgView.setImageResource(Imgid[position]);
i.setScaleType(ImageView.ScaleType.FIT_CENTER);
imgView.setBackgroundResource(GalItemBg);
return imgView;
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="@+id/LinearLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android" >
<Gallery xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/examplegallery"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
public class CustomGallery extends Gallery {
public CustomGallery(Context context) {
super(context);
}
public CustomGallery(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomGallery(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
private boolean isScrollingLeft(MotionEvent e1, MotionEvent e2){
return e2.getX() > e1.getX();
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY){
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;
}
}
}
和xmlLayout文件
import android.widget.ImageView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
public class GalleryExample extends Activity {
private Gallery gallery;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
gallery = (Gallery) findViewById(R.id.examplegallery);
gallery.setAdapter(new AddImgAdp(this));
gallery.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView parent, View v, int position, long id) {
Toast.makeText(GalleryExample.this, "Position=" + position, Toast.LENGTH_SHORT).show();
}
});
}
public class AddImgAdp extends BaseAdapter {
int GalItemBg;
private Context cont;
private Integer[] Imgid = {
R.drawable.a_1, R.drawable.a_2, R.drawable.a_3, R.drawable.a_4, R.drawable.a_5, R.drawable.a_6, R.drawable.a_7
};
public AddImgAdp(Context c) {
cont = c;
TypedArray typArray = obtainStyledAttributes(R.styleable.GalleryTheme);
GalItemBg = typArray.getResourceId(R.styleable.GalleryTheme_android_galleryItemBackground, 0);
typArray.recycle();
}
public int getCount() {
return Imgid.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 imgView = new ImageView(cont);
imgView.setImageResource(Imgid[position]);
i.setScaleType(ImageView.ScaleType.FIT_CENTER);
imgView.setBackgroundResource(GalItemBg);
return imgView;
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="@+id/LinearLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android" >
<Gallery xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/examplegallery"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
public class CustomGallery extends Gallery {
public CustomGallery(Context context) {
super(context);
}
public CustomGallery(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomGallery(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
private boolean isScrollingLeft(MotionEvent e1, MotionEvent e2){
return e2.getX() > e1.getX();
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY){
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 onFling方法,并提供我自己的velocityX值:
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
return super.onFling(e1, e2, 10, velocityY);
}
这并不完美,但它确实起到了作用。理想情况下,您可能会为onFling编写一些自定义内容,使其完全按照您的喜好工作。我想我找到了一种方法,既可以在库中一次只滚动一个视图,又可以使用最小的滑动长度来触发动画 覆盖gallery小部件的onFling方法,而不是调用super.onFling,检查刷卡是从左向右还是从右向左,并调用相应的dpad事件,如下所示:
private boolean isScrollingLeft(MotionEvent e1, MotionEvent e2){
return e2.getX() > e1.getX();
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY){
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;
}
我发现onFling上的以下覆盖对于小刷卡和单页分页来说已经足够好了:
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY)
{
boolean leftScroll = isScrollingLeft(e1, e2);
float velX;
if(leftScroll)
{
velX=500;
}
else
{
velX=-500;
}
return super.onFling(e1, e2, velX, velocityY);
}
velX+-500值似乎提供了足够好的结果,但可以根据您的喜好进行调整
(注意:这是使用中介绍的
IsCrollingleft
方法)这是适用于我的代码
Nadewad的解决方案+一些动画速度调整:
创建扩展库的类,并重写此方法:
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
setAnimationDuration(600);
return super.onScroll(e1, e2, distanceX, distanceY);
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
float velMax = 2500f;
float velMin = 1000f;
float velX = Math.abs(velocityX);
if (velX > velMax) {
velX = velMax;
} else if (velX < velMin) {
velX = velMin;
}
velX -= 600;
int k = 500000;
int speed = (int) Math.floor(1f / velX * k);
setAnimationDuration(speed);
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;
}
@覆盖
公共布尔onScroll(MotionEvent e1、MotionEvent e2、浮点距离X、浮点距离Y){
设置动画持续时间(600);
返回super.onScroll(e1,e2,distanceX,distanceY);
}
@凌驾
公共布尔onFling(MotionEvent e1、MotionEvent e2、float-velocityX、float-velocityY){
浮子最大值=2500f;
浮动下限=1000f;
float velX=数学绝对值(velocityX);
如果(velX>velMax){
velX=velMax;
}否则如果(velX
享受吧 简单的方法如下:
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
return super.onFling(e1, e2, 0, velocityY);
}
您也可以在此处查看:
感谢上帝!!!
天哪,我真的把它插入了一些代码中,它成功了!啊,连续48小时,我只需要在谷歌上搜索我的答案。实际上,即插即用。非常感谢
新代码
import android.widget.ImageView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
public class GalleryExample extends Activity {
private Gallery gallery;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
gallery = (Gallery) findViewById(R.id.examplegallery);
gallery.setAdapter(new AddImgAdp(this));
gallery.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView parent, View v, int position, long id) {
Toast.makeText(GalleryExample.this, "Position=" + position, Toast.LENGTH_SHORT).show();
}
});
}
public class AddImgAdp extends BaseAdapter {
int GalItemBg;
private Context cont;
private Integer[] Imgid = {
R.drawable.a_1, R.drawable.a_2, R.drawable.a_3, R.drawable.a_4, R.drawable.a_5, R.drawable.a_6, R.drawable.a_7
};
public AddImgAdp(Context c) {
cont = c;
TypedArray typArray = obtainStyledAttributes(R.styleable.GalleryTheme);
GalItemBg = typArray.getResourceId(R.styleable.GalleryTheme_android_galleryItemBackground, 0);
typArray.recycle();
}
public int getCount() {
return Imgid.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 imgView = new ImageView(cont);
imgView.setImageResource(Imgid[position]);
i.setScaleType(ImageView.ScaleType.FIT_CENTER);
imgView.setBackgroundResource(GalItemBg);
return imgView;
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="@+id/LinearLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android" >
<Gallery xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/examplegallery"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
public class CustomGallery extends Gallery {
public CustomGallery(Context context) {
super(context);
}
public CustomGallery(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomGallery(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
private boolean isScrollingLeft(MotionEvent e1, MotionEvent e2){
return e2.getX() > e1.getX();
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY){
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;
}
}
旧代码:仅供比较之用
public class CustomGallery extends Gallery {
public CustomGallery(Context context) {
super(context);
}
public CustomGallery(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomGallery(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
float distanceY) {
//Do something specific here.
return super.onScroll(e1, e2, distanceX, distanceY);
}
}
不需要做任何事情,只需返回false即可
private boolean isScrollingLeft(MotionEvent e1, MotionEvent e2){
return e2.getX() > e1.getX();
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY){
return false;
}
这对我很有效你应该提供更多关于你的问题的信息以获得答案好吧,我在一个图库中加载了几张图片,当我向右滚动时,它不会停止,我希望每张图片都停止。Janusz我相信你知道如何解决这个问题!:把你的密码贴出来你是一只聪明的狐狸。感谢您提供了这个优雅的解决方案,这正是我需要的,使我的图库可用。这正是我想要的。为了澄清这个解决方案,我无法弄清楚如何内联设置onFling(),只能求助于创建自定义库。这个解决方案最终帮助了我:这对我来说非常有效,除了如果我尝试滚动(而不是抛出)一个图像,比如说左图像到中心,当我到达中心时,左图像到达中心。如何防止这种情况发生???对不起,我不太清楚你在问什么。你能说得更具体一点吗?哈,很好,但我们必须寻找这样的解决办法,而不是一个简单的属性,这是多么可怜啊……我很高兴这对你有所帮助。事实上谷歌已经发布了ViewPager类,它实现了同样的目标,但实际上在你滑动手指的时候做了一个中间的滚动动画,而不是在滑动动作完成后滚动。这一个对我来说效果最好,因为它使图库一次只滚动一个,不管你滑动的速度有多快。我已经尝试过KeyEvent解决方案,如果你刷得太快,它会像两到三个一样跳过。