缩放控制相对布局中的多个图像

时间:2012-09-03 12:39:00

标签: android android-layout relativelayout zooming

我是Android新手,我必须对现有应用进行改进。在这个应用程序内部,我们有一个相对布局,一组15个imageViews,1个动态imageView和两个textViews,我必须编程缩放控件放大和缩小。最近几天我一直在阅读这个网站,试图在现有问题中找到答案而没有任何结果,因为很多问题没有答案。

我最大的问题是我不知道要使用哪种框架或布局。所有的图像都放置在相对布局中并且运行良好,但我似乎没有让缩放控件与内容一起使用,而且我已经阅读了不同的线程和问题,而RelativeLayout根本不适用于缩放。我想过可能使用FrameLayout,但我认为它有同样的问题。

这是布局中的XML代码:

<RelativeLayout
        android:id="@+id/relativeLayoutMachine"
        android:layout_width="1000dp"
        android:layout_height="550dp"
        android:layout_above="@+id/relativeLayout1"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:background="@color/brm_back_1" >

        <ImageView
            android:id="@+id/imageView01"
            android:layout_width="82dp"
            android:layout_height="67dp"
            android:layout_marginLeft="409dp"
            android:layout_marginTop="83dp"
            android:contentDescription="@id/imageVeuwBandInloopKoker"
            android:src="@drawable/lva_bandinloopkoker" />

        <ImageView
            android:id="@+id/imageView02"
            android:layout_width="82dp"
            android:layout_height="67dp"
            android:layout_marginLeft="409dp"
            android:layout_marginTop="149dp"
            android:contentDescription="@id/imageViewTussenKoker"
            android:src="@drawable/lva_tussenkoker" />

        <ImageView
            android:id="@+id/imageView03"
            android:layout_width="172dp"
            android:layout_height="88dp"
            android:layout_marginLeft="401dp"
            android:layout_marginTop="216dp"
            android:contentDescription="@id/imageViewHMI_Graph_04_Flap"
            android:src="@drawable/lva_flap_open" />

        <ImageView
            android:id="@+id/imageView04"
            android:layout_width="281dp"
            android:layout_height="61dp"
            android:layout_marginLeft="401dp"
            android:layout_marginTop="303dp"
            android:contentDescription="@id/imageViewHMI_Graph_03_Ram"
            android:src="@drawable/lva_ram_retour" />

        <ImageView
            android:id="@+id/imageView05"
            android:layout_width="110dp"
            android:layout_height="47dp"
            android:layout_marginLeft="571dp"
            android:layout_marginTop="257dp"
            android:contentDescription="@id/imageViewHMI_Graph_01_Unit"
            android:src="@drawable/lva_unit" />

        <ImageView
            android:id="@+id/imageView06"
            android:layout_width="51dp"
            android:layout_height="54dp"
            android:layout_marginLeft="431dp"
            android:layout_marginTop="28dp"
            android:contentDescription="@id/imageViewHMI_Graph_15_Feeder"
            android:src="@drawable/lva_feeder_off" />

        <ImageView
            android:id="@+id/imageView07"
            android:layout_width="90dp"
            android:layout_height="40dp"
            android:layout_marginLeft="585dp"
            android:layout_marginTop="218dp"
            android:contentDescription="@id/imageViewHMI_Graph_02_Pump"
            android:src="@drawable/lva_main_motor_off" />

        <ImageView
            android:id="@+id/imageView08"
            android:layout_width="199dp"
            android:layout_height="124dp"
            android:layout_marginLeft="157dp"
            android:layout_marginTop="240dp"
            android:contentDescription="@id/imageViewChannel"
            android:src="@drawable/lva_channel" />

        <ImageView
            android:id="@+id/imageView09"
            android:layout_width="82dp"
            android:layout_height="166dp"
            android:layout_marginLeft="410dp"
            android:layout_marginTop="50dp"
            android:contentDescription="@id/imageViewHMI_Graph_013_Turbo"
            android:src="@drawable/lva_turbo_off"
            android:visibility="invisible" />

        <ImageView
            android:id="@+id/imageView10"
            android:layout_width="88dp"
            android:layout_height="67dp"
            android:layout_marginLeft="402dp"
            android:layout_marginTop="149dp"
            android:contentDescription="@id/imageViewHMI_Graph_012_Perforator"
            android:src="@drawable/lva_perforator_off"
            android:visibility="invisible" />

        <ImageView
            android:id="@+id/ImageView11"
            android:layout_width="88dp"
            android:layout_height="67dp"
            android:layout_marginLeft="408dp"
            android:layout_marginTop="149dp"
            android:contentDescription="@id/ImageViewHMI_Graph_10_Ruffler"
            android:src="@drawable/lva_ruffler_off"
            android:visibility="invisible" />

        <ImageView
            android:id="@+id/ImageView12"
            android:layout_width="174dp"
            android:layout_height="72dp"
            android:layout_marginLeft="362dp"
            android:layout_marginTop="146dp"
            android:contentDescription="@id/ImageViewHMI_Graph_11_Prepress"
            android:src="@drawable/lva_prepress_open"
            android:visibility="invisible" />

        <ImageView
            android:id="@+id/imageView13"
            android:layout_width="46dp"
            android:layout_height="68dp"
            android:layout_marginLeft="356dp"
            android:layout_marginTop="297dp"
            android:contentDescription="@id/imageViewHMI_Graph_07_Needles_Hor"
            android:src="@drawable/lva_needles_hor_none" />

        <ImageView
            android:id="@+id/imageView14"
            android:layout_width="44dp"
            android:layout_height="111dp"
            android:layout_marginLeft="356dp"
            android:layout_marginTop="187dp"
            android:contentDescription="@id/imageViewHMI_Graph_05_Needles_Vert"
            android:src="@drawable/lva_needles_vert_high" />

        <ImageView
            android:id="@+id/imageView15"
            android:layout_width="10dp"
            android:layout_height="24dp"
            android:layout_marginLeft="371dp"
            android:layout_marginTop="272dp"
            android:contentDescription="@id/imageViewHMI_Graph_06_Knotter_Vert"
            android:src="@drawable/lva_motor_off" />

        <FrameLayout
            android:id="@+id/frameLayoutBale"
            android:layout_width="140dp"
            android:layout_height="51dp"
            android:layout_marginLeft="19dp"
            android:layout_marginTop="307dp" >

            <ImageView
                android:id="@+id/imageViewAnimation"
                android:layout_width="140dp"
                android:layout_height="51dp"
                android:layout_gravity="right"
                android:contentDescription="@id/imageViewHMI_Graph_Bale"
                android:scaleType="fitXY"
                android:src="@drawable/baal" />

        </FrameLayout>

        <TextView
            android:id="@+id/textView01"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="110dp"
            android:layout_marginTop="316dp"
            android:gravity="right"
            android:text="@string/percentage"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:textColor="@color/brm_back_2" />

        <TextView
            android:id="@+id/textView02"
            android:layout_width="142dp"
            android:layout_height="wrap_content"
            android:layout_marginLeft="22dp"
            android:layout_marginTop="271dp"
            android:gravity="center"
            android:text="@string/puntjes"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:textColor="@color/brm_back_2" />

        <ZoomControls
            android:id="@+id/zoomControls"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_alignRight="@+id/textCurrMaterial" />

    </RelativeLayout>

这是我在main上用来声明缩放控件的代码:

btnZoomControls = (ZoomControls) findViewById(R.id.zoomControls);
btnZoomControls.setOnZoomInClickListener(new View.OnClickListener() {public void onClick(View v) {setZoomIn(); }});
btnZoomControls.setOnZoomOutClickListener(new View.OnClickListener() {public void onClick(View v) {setZoomOut(); }});

这是放大和缩小的功能

private void setZoomIn()
{
    m_ZoomController.getContainer();
    m_ZoomController.getZoomControls();
    m_ZoomController.setZoomInEnabled(true);
}

private void setZoomOut()
{
    m_ZoomController.getContainer();
    m_ZoomController.getZoomControls();
    m_ZoomController.setZoomOutEnabled(true);
}

你们中间有没有类似我的问题,以及你们是如何找到解决问题的?

1 个答案:

答案 0 :(得分:3)

实际上,您有两种主要的缩放方式:

1)在整个布局或特定视图上调用setScaleX() and setScaleY()(自API 11起)。根据我的经验,图像缩放工作正常,但某些版本的文本会产生奇怪的行为(这种缩放在绘制过程中由视图组应用,因此非常快)。对于不是很大的布局,这种方式非常流畅和快速;

2)在每个视图上更改布局参数(可能需要在缩放期间为图像视图提供新图片)。这种方式不是那么快,但应该适用于几乎所有的Android修订版。