【问题标题】:sliding between images android在图像之间滑动android
【发布时间】:2011-04-01 15:51:15
【问题描述】:

我对 Android 动画和手势比较陌生。

我有 15 张图片要左右滑动。一次只在屏幕上显示一个图像,当我在第一张图像上滑动 L->R 时,会显示第二张图像,依此类推——就像幻灯片一样。我查看了 Android Gallery 教程,但我不想显示缩略图。我的理解是使用 ImageView 并不断更改图像。这是正确的方法还是有更好的方法?

【问题讨论】:

  • 您仍然可以使用图库;只需将其设为全屏并首先使用其中较大的图像。
  • 感谢 Yoni。有了画廊,我不知何故没有得到同样的效果。此外,在画廊缩略图上 - 我需要显示文本,当点击缩略图时 - 我需要打开一个新画廊。
  • 谢谢你!我使用了图库,将图像设为全屏,然后从此处使用合并教程:developer.android.com/resources/articles/…

标签: android gestures


【解决方案1】:

这样你就不会看到弹跳效果了。

画廊有一种方法。

像这样创建画廊:

<Gallery xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" android:id="@+id/HorizontalGallery"
    android:gravity="center_vertical" android:spacing="2px"/>

在 getview 上你必须:

public View getView(int position, View convertView, ViewGroup parent) {

ImageView i = new ImageView(_Context);

i.setImageResource(R.drawable.YourPicture);
i.setLayoutParams(new Gallery.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));

//setting the scale:

int viewWidthtmp;
int viewHeighttmp;

if(getHeight() == 0)
{
    if(_horizGallery.getWidth() == 0){
        viewWidthtmp = _horizGallery.getWidth();
        viewHeighttmp = _horizGallery.getHeight();
    }
    else
    {
        viewWidthtmp = _screenWidth;
        viewHeighttmp = _screenHeight;
    }

//getting the size of the image.
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true; //returns null, but fills the out methods
bm = BitmapFactory.decodeResource(getResources(), R.drawable.YourPicture, o);
if(o.outHeight> viewHeight || o.outWidth> viewWidth) 
   {i.setScaleType(ImageView.ScaleType.CENTER_INSIDE);}
else
   {i.setScaleType(ImageView.ScaleType.FIT_CENTER);}

//DO NOT ADD the line below
//i.setBackgroundResource(mGalleryItemBackground);

return i;

}

您还必须声明 2 个全局变量变量并在活动的 OnCreate 中对其进行初始化。

public class ScrollingGallery extends Activity
{
    private int _screenWidth;
    private int _screenHeight;


...

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.scrollingallery);

        Display display = getWindowManager().getDefaultDisplay(); 
        _screenWidth = display.getWidth();
        _screenHeight = display.getHeight();

...

之后,您只需使用计时器让画廊滚动即可。

如果我没记错的话,这应该适用于整页画廊。代码有点长,我刚写的,可能有bug。

【讨论】:

  • 感谢您的努力!我实际上必须使用 fling,因为只有当用户在屏幕上水平滑动手指时,图像才会更改。
  • 画廊已经火了。如果您希望在 lingigng 时只滚动一张图像,请转到 stackoverflow.com/questions/2373617/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-06-12
  • 1970-01-01
  • 2011-10-15
  • 1970-01-01
  • 2011-11-07
相关资源
最近更新 更多