4

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

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

4

1 回答 1

7

这样你就不会看到一闪而过的效果。

画廊有一种方法可以做到这一点。

像这样创建画廊:

<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 个全局变量 variables 并在活动的 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();

...

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

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

于 2011-04-01T16:31:30.433 回答