2

我有一些使用轮播效果显示的图像:

这是代码:

** carousel_main.xml **

    <com.digitalaria.gama.carousel.Carousel
        android:id="@+id/carousel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"/>    

</LinearLayout>

** carousel_item.xml **

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="match_parent"
  android:layout_height="match_parent">

    <ImageView 
        android:id="@+id/itemImage" 
        android:layout_width="match_parent"    
        android:layout_height="match_parent" />

</FrameLayout>

.java文件:

 @Override
 public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.carousel_main);

    init();
    }

private void init() {
// create the carousel object.
carousel = (Carousel) findViewById(R.id.carousel);

// configurations for the carousel.
carousel.setType(Carousel.TYPE_COVERFLOW);
carousel.setOverScrollBounceEnabled(true);
carousel.setInfiniteScrollEnabled(false);
carousel.setItemRearrangeEnabled(true);

// set images for the carousel.
adapter = new ImageAdapter(this);
carousel.setAdapter(adapter);

// change the first selected position.
carousel.setCenterPosition(3);
}
public class ImageAdapter extends BaseAdapter {
    private Context mContext;

    public ImageAdapter(Context c) {
        mContext = c;
    }

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

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return musicCover[position];
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = convertView;
        if (view == null) {
        view = LayoutInflater.from(mContext).inflate(R.layout.carousel_item, parent, false);
        view.setLayoutParams(new Carousel.LayoutParams(450, 450));

        ViewHolder holder = new ViewHolder();
        holder.imageView = (ImageView) view.findViewById(R.id.itemImage);

        view.setTag(holder);
        }

        ViewHolder holder = (ViewHolder) view.getTag();
        holder.imageView.setImageResource(musicCover[position]);

        return view;
    }

    private class ViewHolder {
        ImageView imageView;
    }
    }

在此处输入图像描述

在图像中,您可以看到左侧、中间和右侧(黑色空间)之间有很多空间。

发生的事情是,如果我将大小view.setLayoutParams(new Carousel.LayoutParams(450, 450)); 增加到 600,600,那么左右项目将不可见。

如何解决这个问题?

4

0 回答 0