2

我会考虑一个GalleryView由少量图像组成的简单模型。我将从左到右滚动,反之亦然,然后离开。截至目前(如下面的 2 张图片所示),gallery停止在视图中间,在滚动完成时从初始位置或最终位置留下一些空间(在图像中标记为红色)。我担心的是,我希望我gallery在滚动后获得初始位置或最终位置(取决于滚动)。我该怎么做?

图片:

当从左到右滚动完成时:

当从右到左滚动完成时:

我的GalleryView

 public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    mAudio =(Button)findViewById(R.id.button1);
    mVideo =(Button)findViewById(R.id.button2);
    mImages=(Button)findViewById(R.id.button3);

    mAudio.setOnClickListener(this);
    mVideo.setOnClickListener(this);
    mImages.setOnClickListener(this);

    Gallery ga = (Gallery) findViewById(R.id.Gallery01);
    galleryAdapter = new ImageAdapter(this); 
    galleryAdapter.setResourseArray(image_pics);
    ga.setAdapter(galleryAdapter);

    imageView = (ImageView) findViewById(R.id.ImageView01);
    ga.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int pos, long arg3) {
            Toast.makeText(getBaseContext(), "You have selected picture " + (pos + 1) + " of Gallery", Toast.LENGTH_SHORT)
            .show();
            if(activeGallery==AUDIO){
                imageView.setImageResource(audio_pics[pos]);
            }else if(activeGallery==VIDEO){
                imageView.setImageResource(video_pics[pos]);
            }else if(activeGallery==IMAGE){
                imageView.setImageResource(image_pics[pos]);
            }
        }
    });
}

@Override
public void onClick(View view) {
    if (view == mAudio) {
        if (galleryAdapter != null) {
            galleryAdapter.setResourseArray(audio_pics);
            galleryAdapter.notifyDataSetChanged();
            activeGallery=AUDIO;
        }
    } else if (view == mVideo) {
        if (galleryAdapter != null) {
            activeGallery=VIDEO;
            galleryAdapter.setResourseArray(video_pics);
            galleryAdapter.notifyDataSetChanged();
        }
    } else if (view == mImages) {
        if (galleryAdapter != null) {
            activeGallery=IMAGE;
            galleryAdapter.setResourseArray(image_pics);
            galleryAdapter.notifyDataSetChanged();
        }
    }
}

public class ImageAdapter extends BaseAdapter {

    private Context ctx;
    int imageBackground;
    private int[] resourseArray = null;

    public ImageAdapter(Context c) {
        ctx = c;
        TypedArray ta = obtainStyledAttributes(R.styleable.Gallery1);
        imageBackground = ta.getResourceId(R.styleable.Gallery1_android_galleryItemBackground, 1);
        ta.recycle();
    }

    @Override
    public int getCount() {
        return resourseArray.length;
    }

    public int[] getResourseArray() {
        return resourseArray;
    }

    public void setResourseArray(int[] resourseArray) {
        this.resourseArray = resourseArray;
    }

    @Override
    public Object getItem(int arg0) {

        return arg0;
    }

    @Override
    public long getItemId(int arg0) {

        return arg0;
    }

    @Override
    public View getView(int position, View arg1, ViewGroup arg2) {
        ImageView iv = new ImageView(ctx);
        iv.setImageResource(resourseArray[position]);
        iv.setScaleType(ImageView.ScaleType.FIT_XY);
        iv.setLayoutParams(new Gallery.LayoutParams(150, 120));
        iv.setBackgroundResource(imageBackground);
        return iv;
    }

}

简单来说,我希望这些画廊视图具有弹性效果。

4

1 回答 1

2

我只知道如何从左侧删除空间,图像将从左侧开始。描述有关显示的一般信息的结构,例如其大小、密度和字体缩放。要访问 DisplayMetrics 成员,请像这样初始化一个对象

    DisplayMetrics metrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(metrics);

    Gallery g = (Gallery) findViewById(R.id.gallery);

    // set gallery to left side
    MarginLayoutParams mlp = (MarginLayoutParams) g.getLayoutParams();
    mlp.setMargins(-(metrics.widthPixels / 2 + (imageWidth/2)), mlp.topMargin,
                mlp.rightMargin, mlp.bottomMargin);
于 2014-04-16T06:29:18.430 回答