2

我是 android 新手,我必须对现有应用程序进行改进。在这个应用程序中,我们有一个包含一组 15 个 imageView、1 个动态 imageView 和两个 textView 的相对布局,我必须对它们进行缩放控制以进行放大和缩小。最近几天我一直在阅读这个网站,试图在现有问题中找到答案,但没有任何结果,因为很多问题都没有答案。

我最大的问题是我不知道要使用哪个框架或布局。所有图像都放置在相对布局中并且工作良好,但我似乎没有让缩放控件与内容一起工作,而且我在不同的线程和问题中阅读了 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);
}

你们有没有和我一样的问题,你是如何找到解决方案的?

4

1 回答 1

3

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

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

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

于 2012-09-03T13:00:39.997 回答