gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-11-01 09:45:52 26 4
gpt4 key购买 nike

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

请帮助我理解这一点。我的困惑是,为什么我要从相对布局扩展我的类,如上面的问题所述,我的意思是我在我的 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com