【问题标题】:Zooming a layout (Relative Layout) in android在android中缩放布局(相对布局)
【发布时间】:2016-11-05 05:23:31
【问题描述】:

我想放大和缩小布局内的所有图像。我见过很多问题,例如herehere,并试图实现它们,但我做不到。

请帮助我理解这一点。我的困惑是,为什么我要从相对布局扩展我的类,如上面的问题所述,我的意思是我的 XML 中有一个相对布局,我在内容视图中设置了这个 XML,但是我怎么能通过使用这种方法。即使我从相对布局扩展类,我将如何在包含我的布局内的图像的活动中使用这个类。

我的 XML 设置是这样的,谁能告诉我我必须遵循什么方法。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.cpec.farrukhtanveer.MainActivity">

   <RelativeLayout
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:layout_marginLeft="130dp"
       android:layout_marginRight="130dp"
       android:background="@drawable/imageBackGround">

      <ImageView
          android:id="@+id/img_one"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:visibility="invisible"
          android:src="@drawable/imageone"/>

      <ImageView
          android:id="@+id/img_two"
          android:visibility="invisible"
          android:layout_width="match_parent"
          android:layout_height="imagetwo"/>
   </RelativeLayout>
</RelativeLayout>

请帮助大家! 谢谢。 乌梅尔

【问题讨论】:

    标签: android android-layout pinchzoom zooming


    【解决方案1】:

    由于没有人提出解决方案,我自己整理了一下,并认为这可能对其他陷入此问题的人有所帮助。基本指导来自here,作者在其中实现了单个图像的放大和缩小,我们只需要添加图像并按照该教程中的说明处理其他初始化。

    ImageViewWithZoomClass:

    private Drawable imageOne, imageTwo;
    imageOne = context.getResources().getDrawable(R.drawable.icon1); 
    imageTwo = context.getResources().getDrawable(R.drawable.icon2);
    .
    .
    imageOne.setBounds(0, 0, imageOne.getIntrinsicWidth(), imageOne.getIntrinsicHeight());
    imageTwo.setBounds(0, 0, imageTwo.getIntrinsicWidth(), imageTwo.getIntrinsicHeight());
    .
    .
    imageOne.draw(canvas);
    imageTwo.draw(canvas);
    

    因为我使用的是相对布局,所以我只是像这样在 MainActivity 中使用了 onClickListener

    relativeLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setContentView(new ImageViewWithZoom(getApplicationContext()));
            }
        });
    

    相对布局的 XML 如下所示:

    <RelativeLayout
        android:id="@+id/relativeLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginLeft="130dp"
        android:layout_marginRight="130dp"
        android:background="@drawable/abc"
        android:clickable="true"
        android:focusable="true"
        android:focusableInTouchMode="true"
        >
    
        <ImageView
            android:id="@+id/img_1"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:src="@drawable/roadandrailways"
            android:visibility="invisible"/>
    
        <ImageView
            android:id="@+id/img_2"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:src="@drawable/capital"
            android:visibility="invisible"/>
    </RelativeLayout>
    

    您可以在布局中添加多个图像。 就是这样,希望对你有帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-02-29
      • 2012-09-08
      • 1970-01-01
      • 1970-01-01
      • 2013-09-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多