【问题标题】:How to Increase the size of Image View in Android如何在 Android 中增加 Image View 的大小
【发布时间】:2012-01-17 09:00:10
【问题描述】:

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

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)
  • 感谢@Fuzzical Logic...我希望读者更容易理解我的问题,并且回复也会更好:)
  • @Mansi:我刚刚添加了 Styleable 属性文件。你可以通过它。

标签: java android android-xml android-gallery galleryview


【解决方案1】:

维纳,

这里的问题其实是两个问题:

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

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

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

只需将上述属性添加到您的 XML 中的 ImageView(可能在您的 layout_weight 下),它应该可以满足您的需求。

希望这会有所帮助,

模糊逻辑

【讨论】:

  • 没问题。看看所有的 ScaleTypes。它们实际上非常全面。 :)
  • 是否可以在不裁剪图像的情况下实现我的目标?
  • 是的,但是图像会失真,纵横比也会不同。使用fitXY 中的scaleType。注意,它可能很丑......
【解决方案2】:

你会尝试下面的代码吗...?

?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" android:layout_weight="1" />
<ImageView
android:id="@+id/ImageView01"
android:layout_width="fill_parent"
android:layout_height="0dp" 
android:src="@drawable/ic_launcher"
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" android:layout_weight="1"/>
</LinearLayout>

如果它不起作用,请尝试将您的画廊高度设置为某个 dp,它会起作用..谢谢

【讨论】:

  • 我只是想为一些硬编码值添加画廊高度,但这不会导致应用在各种屏幕尺寸上的工作方式不同吗?
  • 如果你在 dp 中添加高度,我认为这不会导致任何问题.. 你尝试过上面的代码吗.. 我认为它应该适合你
  • 添加一个layout_weight,导致Horizo​​ntal 和Image 视图缩小。
  • 由于您的回答,我能够组合代码并使其正常工作。谢谢 Ankit
  • ya,非常欢迎,请您上传工作的 xml 布局,在您的问题中,它将帮助其他有类似问题的人,:)
【解决方案3】:

ImageView 的大小是基于父级的,这意味着它共享它的宽度和高度。此外,图像会调整大小以匹配最大限制尺寸(宽度)。

【讨论】:

    【解决方案4】:

    看起来像在 AddImgAdp 类中,您将 ImageView 的高度和宽度设置为特定的像素大小。

    imgView.setLayoutParams(new Gallery.LayoutParams(110, 100)); //width = "110px", height="100px"
    

    我个人会将布局设置为 RelativeLayout,将第一个画廊与顶部对齐,第二个画廊与底部对齐,并将 ImageView 设置为与第二个画廊上方对齐。然后制作图像:

    imgView.setLayoutParams( new Gallery.LayoutParams(
       Gallery.LayoutParams.FILL_PARENT, 
       Gallery.LayoutParams.FILL_PARENT) );
    

    如果使用 LinearLayout 将 ImageView 设置为 fill_parent,它将填充到屏幕底部,并且第二个画廊将离开屏幕。

    还要知道,通过填充所有空间,您很可能会扭曲图片。考虑所有调整大小的选项:http://developer.android.com/reference/android/widget/ImageView.ScaleType.html

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-10-15
      • 2023-03-22
      • 1970-01-01
      • 2020-12-03
      • 2011-12-18
      • 2018-02-16
      • 2015-11-25
      相关资源
      最近更新 更多