android,程序中如何改变ImageView大小

如题所述

1、 在布局文件中定义imageview,但不为其设置资源。
<LinearLayout
android:orientation="vertical"
android:layout_below="@id/title_bar"
android:layout_width="fill_parent"
android:layout_height="fill_parent">

<ImageView
android:id="@+id/topImageview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
2、在activity的初始化代码中,初始化imageview 并设定大小:
public void initTopImageView(View view) {
ImageView imageTopview = (ImageView)view.findViewById(R.id.topImageview);

WindowManager windowManager = mParent.getWindowManager();
Display display = windowManager.getDefaultDisplay();

int imageWidth = display.getWidth();
int imageHeight = 0;

BitmapFactory.Options option = new BitmapFactory.Options();
option.inJustDecodeBounds = true;

Bitmap myMap = BitmapFactory.decodeStream(getResources().openRawResource(R.raw.landing_hot_product_1));

imageHeight = (imageWidth*myMap.getHeight())/myMap.getWidth();
imageTopview.setScaleType(ImageView.ScaleType.FIT_XY);
imageTopview.setLayoutParams(new LinearLayout.LayoutParams(imageWidth,imageHeight));
imageTopview.setImageResource(R.raw.landing_hot_product_1);

myMap.recycle();
}
在给imageview设置图片资源之前,根据比例关系动态计算图片的宽和高,并设置给imageview.
温馨提示:答案为网友推荐,仅供参考
第1个回答  2013-08-05
在layout.xml文件中定义了ImageView的绝对大小后,无法动态修改以后的大小显示,其实Android平台在设计UI控件时考虑到这个问题,为了适应不同的Drawable可以通过在xml的相关ImageView中加入android:scaleType="fitXY" 这行即可,但因为使用了缩放可能会造成当前UI有所变形。
第2个回答  2013-08-05
图片视图大小改不了

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网