繁体   English   中英

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

[英]How to Increase the size of Image View in Android

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

在此输入图像描述

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>

Vinner,

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

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

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

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

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

希望这可以帮助,

FuzzicalLogic

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

?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,它会工作..谢谢

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

看起来在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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM