将视图宽度和高度调整为50%

时间:2013-01-07 15:13:11

标签: android gallery adapter

我正在创建一个带有自定义适配器的图库(相对布局,我将imageview作为缩略图,textview作为Name和其他工作人员)。

图库全屏显示,但这不是我想要的..我希望自定义相对布局的高度和宽度相对于屏幕尺寸缩放50%。我试过这个:

gallery.getLayoutParams().width = relativelayout.getWidth() / 2;
gallery.getLayoutParams().height = relativelayout.getHeight() / 2;

这是一个错误的尝试,它只是将高度和宽度设置为正确的大小而不缩放内容。

这是原始视图:

Original

这就是我的预期:

Expectation

这就是我所拥有的:

Result

这是布局封面:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/ivTourCover"
        android:layout_width="640dp"
        android:layout_height="240dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:contentDescription="@string/app_name"
        android:scaleType="fitXY"
        android:src="@drawable/croppedstopgallery_f45d88e4_7864_4dbc_9123_ad7953d8724a" />

    <ImageButton
        android:id="@+id/ivHorizontalLine"
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:layout_below="@+id/tvTourName"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:layout_marginTop="0dp"
        android:contentDescription="@string/app_name"
        android:scaleType="fitXY"
        android:src="@drawable/horizontal_line" />

    <TextView
        android:id="@+id/tvAuthorName"
        style="@style/Cover.AuthorName"
        android:layout_width="fill_parent"
        android:layout_height="19dp"
        android:layout_alignTop="@+id/ivAuthorThumbnail"
        android:layout_marginLeft="12dp"
        android:layout_marginRight="17dp"
        android:layout_toLeftOf="@+id/ivGo"
        android:layout_toRightOf="@+id/ivAuthorThumbnail"
        android:text="Anna Greenleaf" />

    <ImageView
        android:id="@+id/ivAuthorThumbnail"
        android:layout_width="46dp"
        android:layout_height="46dp"
        android:layout_alignLeft="@+id/ivHorizontalLine"
        android:layout_below="@+id/ivHorizontalLine"
        android:layout_marginTop="20dp"
        android:contentDescription="@string/app_name"
        android:scaleType="fitXY"
        android:src="@drawable/croppedstopgallery_f45d88e4_7864_4dbc_9123_ad7953d8724a" />

    <ImageView
        android:id="@+id/ivGo"
        android:layout_width="46dp"
        android:layout_height="46dp"
        android:layout_alignRight="@+id/ivHorizontalLine"
        android:layout_alignTop="@+id/tvAuthorName"
        android:contentDescription="@string/app_name"
        android:scaleType="fitXY"
        android:src="@drawable/tour_cover_go" />

    <TextView
        android:id="@+id/tvAuthorDescription"
        style="@style/Cover.AuthorDescription"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/tvAuthorName"
        android:layout_alignRight="@+id/tvAuthorName"
        android:layout_below="@+id/tvAuthorName"
        android:text="London native and London over!" />

    <SurfaceView
        android:id="@+id/svFooter"
        android:layout_width="wrap_content"
        android:layout_height="30dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:background="@color/CoverFooter" />

    <TextView
        android:id="@+id/tvFooterText"
        style="@style/Cover.FooterText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/svFooter"
        android:layout_alignBottom="@+id/svFooter"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="10dp"
        android:text="Individual" />

    <com.example.zipdownload.utilities.ui.AutoResizeTextView
        android:id="@+id/tvTourName"
        style="@style/Cover.TourName"
        android:layout_width="wrap_content"
        android:layout_height="79dp"
        android:layout_below="@+id/ivTourCover"
        android:layout_centerHorizontal="true"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:layout_marginTop="15dp"
        android:gravity="center|top"
        android:text="Beautiful London in Winston Churchill&apos;s footsteps" />

</RelativeLayout>

查看图库:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/rlGallery"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Gallery" >

    <Gallery
        android:id="@+id/gCoverflow"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:gravity="top" />

</RelativeLayout>

活动图库:

public class Gallery extends Activity {
    private android.widget.Gallery gCoverflow;
    private RelativeLayout rlGallery;
    private CoverAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_gallery);
        rlGallery = (RelativeLayout) findViewById(R.id.rlGallery);
        adapter = new CoverAdapter(this);
        gCoverflow = (android.widget.Gallery) findViewById(R.id.gCoverflow);
        gCoverflow.setAdapter(adapter);
    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        gCoverflow.getLayoutParams().width = rlGallery.getWidth() / 2;
        gCoverflow.getLayoutParams().height = rlGallery.getHeight() / 2;
    }
}

2 个答案:

答案 0 :(得分:1)

您可以尝试使用ScaleAnimation,但在这种情况下,触摸事件将无效:

final ScaleAnimation animation = new ScaleAnimation (
        1f, 0.5f, 1f, 0.5f,
        Animation.RELATIVE_TO_SELF, 0.5f,
        Animation.RELATIVE_TO_SELF, 0.5f
);

final LayoutAnimationController controller = new LayoutAnimationController(animation);

animation.setDuration(0);
animation.setFillAfter(true);

gCoverflow.setLayoutAnimation(controller);

此代码应添加到onCreate方法中。

答案 1 :(得分:0)

我找到了家伙,而不是:

gCoverflow.getLayoutParams().width = rlGallery.getWidth() / 2;
gCoverflow.getLayoutParams().height = rlGallery.getHeight() / 2;

我用这个:

gCoverflow.setScaleX((float) 0.5);
gCoverflow.setScaleY((float) 0.5);