2016-11-07 41 views
1

我想要以编程方式基于可绘制图像大小 更改Imageview高度和宽度?我有两种类型的图像,一种是水平的,另一种是垂直的。默认情况下,我已经为垂直图像编写了我的xml代码,但是我想通过java代码更改图像视图水平图像的高度和宽度。这个怎么做 ?基于可绘制图像以编程方式更改图像视图高度和宽度

我的XML代码是

<RelativeLayout 
    android:paddingTop="47dp" 
    android:paddingBottom="48dp" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_centerInParent="true" 
    android:id="@+id/flipcardWrapper"> 

    <ImageView 
     android:orientation="horizontal" 
     android:layout_width="365dp" 
     android:layout_height="match_parent" 
     android:layout_centerInParent="true" 
     android:layout_centerVertical="true" 
     android:id="@+id/imgtrans" 
     android:background="@android:color/transparent" /> 

和我的Java代码来改变高度和图像视图的宽度是

imgview = (ImageView) findViewById(R.id.imgtrans); 
    Drawable d = getResources().getDrawable(R.drawable.image1); 
    int h = d.getIntrinsicHeight(); 
    int w = d.getIntrinsicWidth(); 



    if(d.getIntrinsicHeight()==797 && d.getIntrinsicWidth()==1218) 
    { 
     imgtrans.getLayoutParams().width=300; 
     imgtrans.requestLayout(); 

    } 

回答

0

设置两个图像用于水平和另一个是在垂直然后根据设置图像找到屏幕方向

if(getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE){ 
Drawable d = getResources().getDrawable(R.drawable.imagehorizontal); 
//set your other properties 
} 

if(getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT){ 
Drawable d = getResources().getDrawable(R.drawable.imagevertical); 
//set your other properties 
} 
0

在java代码中使用ViewGroup.LayoutParams.MATCH_PARENT。

imgview = (ImageView) findViewById(R.id.imgtrans); 
Drawable d = getResources().getDrawable(R.drawable.image1); 
int h = d.getIntrinsicHeight(); 
int w = d.getIntrinsicWidth(); 



if(d.getIntrinsicHeight()==797 && d.getIntrinsicWidth()==1218) 
{ 
    imgtrans.getLayoutParams().width = ViewGroup.LayoutParams.MATCH_PARENT; 
    imgtrans.getLayoutParams().height = someValue; 
    imgtrans.requestLayout(); 
} 
+0

isHori(d)我在android中找不到它。你能告诉我如何实现这个 – user7125006

+0

@ user7125006对不起,我的英语很差,我无法表达清楚。所以我修改了我的答案。 – sunhang

相关问题