android - 在android中缩放布局(相对布局)

标签 android android-layout pinchzoom zooming

我想放大和缩小布局内的所有图像。我见过很多类似here这样的问题和 here并尝试实现它们,但我做不到。

请帮助我理解这一点。我的困惑是,为什么我要从相对布局扩展我的类,如上面的问题所述,我的意思是我的 XML 中有一个相对布局,并且我在内容 View 中设置此 XML,但是我如何通过以下方式获取特定的相对布局使用这种方法。即使我从相对布局扩展了类,我如何在布局中包含我的图像的 Activity 中使用此类。

我的 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>

请大家帮忙! 谢谢。 乌迈尔

最佳答案

由于没有人提出解决方案,我自己解决了这个问题,并认为这可能对其陷入此问题的其他人有所帮助。基本指导来自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>

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

关于android - 在android中缩放布局(相对布局),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38168432/

相关文章:

android Activity 已泄露窗口 com.android.internal.policy.impl.phonewindow$decorview 问题

android - R 无法解析为变量 - Android- Eclipse Fresh Project

Android:字符串可见性问题

javascript - 如何从Javascript重置网页上的缩放(捏缩放后)?

iOS 捏合缩放从上一个比例开始

ios - 在同一个 UIImageView 上缩放和固定

Android studio 布局设计 View 未加载

java heap size error dex2jar android反编译解决方法?

android SimpleOnGestureListener 只触发onscroll事件

android - 多 fragment 最佳实践