Ich möchte die Höhe und Breite der Bildansicht ändernbasierend auf der gezeichneten Bildgröße programmatisch? Ich habe zwei Arten von Bildern, eines ist horizontal und eines ist vertikal. Standardmäßig habe ich meinen XML-Code für vertikale Bilder geschrieben, aber ich möchte die Bildansicht Höhe und Breite für horizontale Bilder durch Java-Code ändern. Wie macht man das ?
Mein XML-Code lautet
<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" />
und mein Java-Code, um die Höhe und Breite der Bildansicht zu ändern
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();
}
Antworten:
0 für die Antwort № 1Legen Sie zwei Bilder für die Horizontale fest und ein anderes ist vertikal einstellbar. Anschließend können Sie die Bildschirmausrichtung anhand des festgelegten Bilds suchen
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 für die Antwort № 2
Verwenden Sie ViewGroup.LayoutParams.MATCH_PARENT im Java-Code.
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();
}