android - 相对布局中多个图像的缩放控件

标签 android android-layout android-relativelayout zooming

我是 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);
}

你们中有人遇到过像我这样的类似问题吗?你们是如何找到解决方案的?

最佳答案

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

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

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

关于android - 相对布局中多个图像的缩放控件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12248073/

相关文章:

android - Material Flat Button 上的波纹在哪里定义

android - 如何在 Activity 中实现 CordovaWebView

Android - EditText自定义样式

android - 在 Android 1.5 上查看与 RelativeLayout 的重叠

javax.net.ssl.SSLHandshakeException?

android - 如果我的应用程序没有运行,AlarmManager 会工作吗?

android - 如何在 Android 中的 native View 之上覆盖 PhoneGap 的 CordovaWebView?

java - 如何修复 Android 聊天中 ListView 中的此错误

Android 在 RelativeLayout 中裁剪 View 的不可见部分

Android "Add a vertical line"在 "RelativeLayout"/"Linear Layout"