如何增加图像查看Android中的大小 [英] How to Increase the size of Image View in Android

查看:181
本文介绍了如何增加图像查看Android中的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的工作要求我实施两个水平滚动的意见图片视图它们之间的项目。我想延长图像视图的大小以填满间隙中滚动视图之间。我通过样品code去了,但它似乎并没有在图像视图的大小在任何地方提到。我的图片描述了我的问题后附上低于code。

 公共类Gallery2DemoActivity延伸活动{
私人画廊的画廊,gallery1;
私人ImageView的imgView;私人整数[] = 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
};@覆盖
公共无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.main);画廊=(图库论坛)findViewById(R.id.examplegallery);
gallery.setAdapter(新AddImgAdp(本));gallery1 =(图库论坛)findViewById(R.id.examplegallery1);
gallery1.setAdapter(新AddImgAdp(本));imgView =(ImageView的)findViewById(R.id.ImageView01);
imgView.setImageResource(Imgid [0]);
 gallery.setOnItemClickListener(新OnItemClickListener(){
    公共无效onItemClick(适配器视图父母,视图V,INT位置,长的id){
        imgView.setImageResource(Imgid [位置]);
    }
});}公共类AddImgAdp延伸BaseAdapter {
INT GalItemBg;
私人语境续;公共AddImgAdp(上下文C){
    CONT = C;
    TypedArray typArray = obtainStyledAttributes(R.styleable.GalleryTheme);
    GalItemBg = typArray.getResourceId(R.styleable.GalleryTheme_android_galleryItemBackground,0);
    typArray.recycle();
}公众诠释的getCount(){
    返回Imgid.length;
}公共对象的getItem(INT位置){
    返回的位置;
}众长getItemId(INT位置){
    返回的位置;
}公共查看getView(INT位置,查看convertView,父母的ViewGroup){
    ImageView的imgView =新ImageView的(续)    imgView.setImageResource(Imgid [位置]);
    imgView.setLayoutParams(新Gallery.LayoutParams(110,100));
    imgView.setScaleType(ImageView.ScaleType.FIT_XY);
    imgView.setBackgroundResource(GalItemBg);    返回imgView;
}
}

这是我的XML主文件。

 <?XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
机器人:ID =@ + ID / LinearLayout01
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT
机器人:方向=垂直>
<画廊
    的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:ID =@ + ID / examplegallery
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =WRAP_CONTENT/>
< ImageView的
    机器人:ID =@ + ID / ImageView01
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT
    机器人:layout_weight =1/>
<画廊
    的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:ID =@ + ID / examplegallery1
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =WRAP_CONTENT/>
< / LinearLayout中>

下面是我的属性文件:

 <?XML版本=1.0编码=UTF-8&GT?;
<资源>
<申报-设置样式名称=GalleryTheme>
    < attr指示名=机器人:galleryItemBackground/>
< /申报,设置样式>
< /资源>


解决方案

Vinner,

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


  1. 在ImageView的大小是根据家长的,这意味着它的股票其宽度的的高度。


  2. 图片大小调整最为匹配限制尺寸(宽)。


来处理最简单的方法是设置的ImageView的ScaleType。有许多缩放类型,但你可能想要的是的中心作物的。在code,这是由完成view_name.setScaleType(ImageView.ScaleType.CENTER_CROP)。 scaleType =centerCrop:您也可以与属性 Android平台的ImageView为此在XML。你要知道,这将调整图像和维护长宽比,但它会切断面。

就在上面的属性添加到您的ImageView在你的XML,(可能在你的layout_weight),它应该做你想做的。

希望这有助于

FuzzicalLogic

I am working on a project that requires me to implement two horizontal scroll views with an image view in between them. I would like to extend the size of the image view to fill up the gap in between the scroll view. I went through the sample code, but it doesn't seem to mention anywhere in the size of the image view. I've enclosed the code below after the Image describing my problem.

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

This is my XML main file.

<?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>

Below is my Attributes file:

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

解决方案

Vinner,

The issue here is really two issues:

  1. The size of the ImageView is based on the parent, which means that it shares its width and height.

  2. The Image is resized to match the most limiting dimension (width).

The easiest way to handle this is to set the ScaleType of the ImageView. There are a number of Scaling Types, but the one you probably want is Center Crop. In code, this is done by view_name.setScaleType(ImageView.ScaleType.CENTER_CROP). You may also do this in XML for your ImageView with the attribute android:scaleType="centerCrop". Mind you, this will resize your Image and maintain your aspect ratio, but it will cut off the sides.

Just add the above attribute to your ImageView in your XML, (probably under your layout_weight), and it should do what you want.

Hope this helps,

FuzzicalLogic

这篇关于如何增加图像查看Android中的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆