gpt4 book ai didi

java - 如何在 Android 中增加 ImageView 的大小

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:58:36 26 4
gpt4 key购买 nike

我正在从事一个项目,该项目要求我实现两个水平 ScrollView ,并在它们之间放置一个 ImageView 。我想扩展 ImageView 的大小以填补 ScrollView 之间的空白。我浏览了示例代码,但它似乎没有提到 ImageView 大小的任何地方。我在描述我的问题的图像之后附上了下面的代码。

enter image description here

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;
}
}

这是我的 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>

下面是我的属性文件:

<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="GalleryTheme">
<attr name="android:galleryItemBackground" />
</declare-styleable>
</resources>

最佳答案

维纳,

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

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

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

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

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

希望对您有所帮助,

模糊逻辑

关于java - 如何在 Android 中增加 ImageView 的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8891909/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com