=============急 安卓 gallery 滑动图片,图片左右跳动怎么解决======

酒比花香 2013-12-25 06:09:17
安卓使用gallery显示图片,在滑动图片时,有时(90%)图片会左右跳动,哆嗦。。。不显示下一个图片。这种情况怎么解决?这个已经解决了好几个星期了。。。

xml文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res/com.suma.activity.smartplayer"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/suma_shape_image_bg"
android:orientation="vertical" >

<Gallery
android:id="@+id/home_gallery"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:spacing="0dip" />
....
</RelativeLayout>

这个是gallery中的item 对应的getview
public View getView(int position, View convertView, ViewGroup parent) {
ImageView image = null;
if (convertView == null) {
image = new ImageView(mContext);
} else {
image = (ImageView) convertView;
}
//使用ImageLoader加载图片
imageLoader.displayImage(resourceList.get(position).getPicUrl(), image, options);
WindowManager wm = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
int screenWidth = wm.getDefaultDisplay().getWidth();
image.setLayoutParams(new Gallery.LayoutParams(screenWidth, screenWidth * 211 / 516));
return image;
}
...全文
569 14 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
hezhudong 2014-02-27
  • 打赏
  • 举报
回复
我也遇到这个问题了,试试。
酒比花香 2014-01-05
  • 打赏
  • 举报
回复
感谢上面各位兄弟回复,不过自己挖的坑自己埋。。。。 原因是使用了Image-loader,后来我把Image-loader的内存、硬盘缓存去掉了,好了。。缺点是让他每次都从网上去取吧。 话说用框架出现的问题还真是难找啊。。。 options = new DisplayImageOptions.Builder() .showImageOnLoading( GetItemId.getDrawableResIDByName(context, "suma_default_poster")) .showImageForEmptyUri( GetItemId.getDrawableResIDByName(context, "suma_default_poster")) .showImageOnFail(GetItemId.getDrawableResIDByName(context, "suma_default_poster")) // .cacheInMemory(true) // .cacheOnDisc(true)
咲咲小 2013-12-31
  • 打赏
  • 举报
回复
package org.crazyit.gallery;

import android.app.Activity;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.Gallery.LayoutParams;
import android.widget.ViewSwitcher.ViewFactory;

/**
 * Description:
 * <br/>site: <a href="http://www.crazyit.org">crazyit.org</a> 
 * <br/>Copyright (C), 2001-2012, Yeeku.H.Lee
 * <br/>This program is protected by copyright laws.
 * <br/>Program Name:
 * <br/>Date:
 * @author  Yeeku.H.Lee kongyeeku@163.com
 * @version  1.0
 */
public class GallaryTest extends Activity
{
	int[] imageIds = new int[]
	{
		R.drawable.shuangzi, R.drawable.shuangyu,
		R.drawable.chunv, R.drawable.tiancheng, R.drawable.tianxie,
		R.drawable.sheshou, R.drawable.juxie, R.drawable.shuiping,
		R.drawable.shizi, R.drawable.baiyang, R.drawable.jinniu,
		R.drawable.mojie };

	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		final Gallery gallery = (Gallery) findViewById(R.id.gallery);
		// 获取显示图片的ImageSwitcher对象
		final ImageSwitcher switcher = (ImageSwitcher) 
			findViewById(R.id.switcher);
		// 为ImageSwitcher对象设置ViewFactory对象
		switcher.setFactory(new ViewFactory()
		{
			@Override
			public View makeView()
			{
				ImageView imageView = new ImageView(GallaryTest.this);
				imageView.setBackgroundColor(0xff0000);
				imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
				imageView.setLayoutParams(new ImageSwitcher.LayoutParams(
					LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
				return imageView;
			}
		});
		// 设置图片更换的动画效果
		switcher.setInAnimation(AnimationUtils.loadAnimation(this,
			android.R.anim.fade_in));
		switcher.setOutAnimation(AnimationUtils.loadAnimation(this,
			android.R.anim.fade_out));
		// 创建一个BaseAdapter对象,该对象负责提供Gallery所显示的图片
		BaseAdapter adapter = new BaseAdapter()
		{
			@Override
			public int getCount()
			{
				return imageIds.length;
			}
			@Override
			public Object getItem(int position)
			{
				return position;
			}
			@Override
			public long getItemId(int position)
			{
				return position;
			}

			// 该方法的返回的View就是代表了每个列表项
			@Override
			public View getView(int position, View convertView, ViewGroup parent)
			{
				// 创建一个ImageView
				ImageView imageView = new ImageView(GallaryTest.this);
				imageView
					.setImageResource(imageIds[position % imageIds.length]);
				// 设置ImageView的缩放类型
				imageView.setScaleType(ImageView.ScaleType.FIT_XY);
				imageView.setLayoutParams(new Gallery.LayoutParams(75, 100));
				TypedArray typedArray = obtainStyledAttributes(
					R.styleable.Gallery);
				imageView.setBackgroundResource(typedArray.getResourceId(
					R.styleable.Gallery_android_galleryItemBackground, 0));
				return imageView;
			}
		};
		gallery.setAdapter(adapter);
		gallery.setOnItemSelectedListener(new OnItemSelectedListener()
		{
			// 当Gallery选中项发生改变时触发该方法
			@Override
			public void onItemSelected(AdapterView<?> parent, View view,
				int position, long id)
			{
				switcher.setImageResource(imageIds[position % imageIds.length]);
			}

			@Override
			public void onNothingSelected(AdapterView<?> parent)
			{
			}
		});
	}
}
以上是一份例子,楼主自己对照,图片需要卤煮自己定义, 一下是布局文件
<?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">
<!-- 定义一个ImageSwitcher组件 -->
<ImageSwitcher android:id="@+id/switcher"
	android:layout_width="320dp"
	android:layout_height="320dp"
	/>
<!-- 定义一个Gallery组件 -->
<Gallery android:id="@+id/gallery"
	android:layout_width="fill_parent"
	android:layout_height="wrap_content"
	android:layout_marginTop="25dp" 
	android:unselectedAlpha="0.6"
	android:spacing="3pt"
	/>	
</LinearLayout>
希望能帮助到你
卡卡的喵 2013-12-31
  • 打赏
  • 举报
回复
是不是和ListView冲突了?
酒比花香 2013-12-27
  • 打赏
  • 举报
回复
引用 6 楼 lulong1985 的回复:
到不了第二张,这不清楚啊,还没遇到过这样的情况,多贴点代码看看,怎么设置的,你要是不太依赖Gallery的话,我建议用viewpage,个人建议。
viewPager我用了下,还不如gallery,用viewpaper拖动时,上下的拖动范围不能太大,否则会上下滚动。而不是左右动。 先帮忙看下这个gallery的问题吧。
afairycell 2013-12-27
  • 打赏
  • 举报
回复
左右跳动大概是图片界面自动调整了吧?移动过去了,又调整到正常位置,不显示下一个图片这个明显就是你没加载到下一个图片
afairycell 2013-12-27
  • 打赏
  • 举报
回复
Gallery没有用过,用下SurfaceView试试?做游戏大多数都是用SurfaceView,显示图片什么的妥妥的
triplesky001 2013-12-27
  • 打赏
  • 举报
回复
到不了第二张,这不清楚啊,还没遇到过这样的情况,多贴点代码看看,怎么设置的,你要是不太依赖Gallery的话,我建议用viewpage,个人建议。
afairycell 2013-12-27
  • 打赏
  • 举报
回复
你的position是不是没有改变过?给份东西你参考下吧。感觉你改一下就可以完成上面的问题了 package com.eoemobile.book.ex_widgetdemo; 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.animation.AnimationUtils; import android.widget.AdapterView; import android.widget.BaseAdapter; import android.widget.Button; import android.widget.CheckBox; import android.widget.EditText; import android.widget.Gallery; import android.widget.ImageSwitcher; import android.widget.ImageView; import android.widget.TextView; import android.widget.ViewSwitcher; import android.widget.Gallery.LayoutParams; public class ImageShowActivity extends Activity implements AdapterView.OnItemSelectedListener, ViewSwitcher.ViewFactory { CheckBox plain_cb; CheckBox serif_cb; CheckBox italic_cb; CheckBox bold_cb; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.image_show); setTitle("ImageShowActivity"); mSwitcher = (ImageSwitcher) findViewById(R.id.switcher); mSwitcher.setFactory(this); mSwitcher.setInAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_in)); mSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_out)); Gallery g = (Gallery) findViewById(R.id.gallery); g.setAdapter(new ImageAdapter(this)); g.setOnItemSelectedListener(this); } public void onItemSelected(AdapterView parent, View v, int position, long id) { mSwitcher.setImageResource(mImageIds[position]); } public void onNothingSelected(AdapterView parent) { } public View makeView() { ImageView i = new ImageView(this); i.setBackgroundColor(0xFF000000); i.setScaleType(ImageView.ScaleType.FIT_CENTER); i.setLayoutParams(new ImageSwitcher.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); return i; } private ImageSwitcher mSwitcher; public class ImageAdapter extends BaseAdapter { public ImageAdapter(Context c) { mContext = c; } public int getCount() { return mThumbIds.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 i = new ImageView(mContext); i.setImageResource(mThumbIds[position]); i.setAdjustViewBounds(true); i.setLayoutParams(new Gallery.LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); i.setBackgroundResource(R.drawable.picture_frame); return i; } private Context mContext; } private Integer[] mThumbIds = { R.drawable.sample_thumb_0, R.drawable.sample_thumb_1, R.drawable.sample_thumb_2, R.drawable.sample_thumb_3, R.drawable.sample_thumb_4, R.drawable.sample_thumb_5, R.drawable.sample_thumb_6, R.drawable.sample_thumb_7}; private Integer[] mImageIds = { R.drawable.sample_0, R.drawable.sample_1, R.drawable.sample_2, R.drawable.sample_3, R.drawable.sample_4, R.drawable.sample_5, R.drawable.sample_6, R.drawable.sample_7}; } XML: <ImageSwitcher android:id="@+id/switcher" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_alignParentTop="true" android:layout_alignParentLeft="true" /> <Gallery android:id="@+id/gallery" android:background="#55000000" android:layout_width="fill_parent" android:layout_height="60dp" android:layout_alignParentBottom="true" android:layout_alignParentLeft="true" android:gravity="center_vertical" android:spacing="16dp" />
酒比花香 2013-12-26
  • 打赏
  • 举报
回复
有大神么?求解
酒比花香 2013-12-26
  • 打赏
  • 举报
回复
引用 4 楼 lulong1985 的回复:
什么叫左右哆嗦,你是指滑动后会有回弹的效果吗
是的,一直在那弹。。到不了第二个图片
triplesky001 2013-12-26
  • 打赏
  • 举报
回复
什么叫左右哆嗦,你是指滑动后会有回弹的效果吗
酒比花香 2013-12-26
  • 打赏
  • 举报
回复
100分都没人要么。。
酒比花香 2013-12-25
  • 打赏
  • 举报
回复

80,472

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧