如何在Android中增加图像视图的大小

5 java android galleryview android-gallery android-xml

我正在开发一个项目,要求我实现两个水平滚动视图,在它们之间有一个图像视图.我想扩展图像视图的大小以填补滚动视图之间的空白.我浏览了示例代码,但它似乎没有提到图像视图的大小.在描述我的问题的图像之后,我附上了下面的代码.

在此输入图像描述

public class Gallery2DemoActivity extends Activity {
private Gallery gallery,gallery1;
private ImageView imgView;

private Integer[] Imgid = {
    R.drawable.a_1, R.drawable.a_2, R.drawable.a_3, R.drawable.a_4, R.drawable.a_5, R.drawable.a_6, R.drawable.a_7
};

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

gallery = (Gallery) findViewById(R.id.examplegallery);
gallery.setAdapter(new AddImgAdp(this));

gallery1 = (Gallery)findViewById(R.id.examplegallery1);
gallery1.setAdapter(new AddImgAdp(this));

imgView = (ImageView)findViewById(R.id.ImageView01);    
imgView.setImageResource(Imgid[0]);


 gallery.setOnItemClickListener(new OnItemClickListener() {
    public void onItemClick(AdapterView parent, View v, int position, long id) {
        imgView.setImageResource(Imgid[position]); 
    }
});

}

public class AddImgAdp extends BaseAdapter {
int GalItemBg;
private Context cont;

public AddImgAdp(Context c) {
    cont = c;
    TypedArray typArray = obtainStyledAttributes(R.styleable.GalleryTheme);
    GalItemBg =  typArray.getResourceId(R.styleable.GalleryTheme_android_galleryItemBackground, 0);
    typArray.recycle();
}

public int getCount() {
    return Imgid.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 imgView = new ImageView(cont);

    imgView.setImageResource(Imgid[position]);
    imgView.setLayoutParams(new Gallery.LayoutParams(110, 100));
    imgView.setScaleType(ImageView.ScaleType.FIT_XY);
    imgView.setBackgroundResource(GalItemBg);

    return imgView;
}
}
Run Code Online (Sandbox Code Playgroud)

这是我的XML主文件.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/LinearLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Gallery
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/examplegallery"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" />
<ImageView
    android:id="@+id/ImageView01"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" 
    android:layout_weight="1"/>
<Gallery
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/examplegallery1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

下面是我的属性文件:

<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="GalleryTheme">
    <attr name="android:galleryItemBackground" />
</declare-styleable>
</resources>
Run Code Online (Sandbox Code Playgroud)

Fuz*_*gic 4

维纳,

这里的问题实际上是两个问题:

  1. ImageView 的大小基于父级,这意味着它共享其宽度高度。

  2. 调整图像大小以匹配最限制的尺寸(宽度)。

处理这个问题最简单的方法是设置 ImageView 的 ScaleType。有多种缩放类型,但您可能需要的是Center Crop。在代码中,这是由 完成的view_name.setScaleType(ImageView.ScaleType.CENTER_CROP)。您还可以使用 XML 属性为 ImageView 执行此操作android:scaleType="centerCrop"。请注意,这将调整您的图像大小并保持您的纵横比,但它会切断侧面。

只需将上述属性添加到 XML 中的 ImageView 中(可能在您的layout_weight 下),它就应该执行您想要的操作。

希望这可以帮助,

模糊逻辑