简体   繁体   English

在Android中缩放时固定底部

[英]Set bottom fixed while scaling in Android

I have two Image views, one is below the other one. 我有两个图像视图,一个在另一个视图之下。

I want to resize the images, and when one is getting bigger, I want the other one to be become smaller. 我想调整图像的大小,当一个图像变大时,我希望另一个图像变小。 But, I also want the image above to be pivoted at the top and the lower one to be pivoted at bottom. 但是,我也希望上面的图像在顶部旋转,而下面的图像在底部旋转。

I achieved this on first one (top), but I couldn't make the one below pivot at the bottom. 我在第一个(顶部)实现了此目的,但我无法在底部使下面的一个达到枢轴。

Question: How can I go about making the bottom image pivot and become smaller as the top one becomes larger . 问题:如何使底部图像旋转并随着顶部图像变而变小。

Here is my layout : 这是我的布局:

<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"
    tools:context=".MainActivity"
    android:id="@+id/root">

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/imageView"
            android:layout_alignParentTop="true"
            android:scaleType="fitCenter"
            android:src="@drawable/as" />

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/imageView"
            android:scaleType="fitCenter"
            android:id="@+id/imageView2"
            android:src="@drawable/as" />
</RelativeLayout>

Here is my Java code : 这是我的Java代码:

 root = (RelativeLayout) findViewById(R.id.root);
        iv = (ImageView) findViewById(R.id.imageView);
        iv2 = (ImageView) findViewById(R.id.imageView2);
        iv.setPivotY(iv.getX());


   detector = new GestureDetectorCompat(this, new GestureDetector.SimpleOnGestureListener() {

        @Override
        public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
            iv.setScaleY(iv.getScaleY() - distanceY / root.getHeight());
            iv2.setScaleY(iv2.getScaleY()+ distanceY / root.getHeight());
           return true;
        }
    });

在布局xml文件中,将android:pivotYandroid:pivotX设置为布局尺寸的1/2。

I am not entirely sure what you are trying to achieve, but this code splits the screen into two section, each of which is used for one of the imageviews. 我不确定您要实现的目标,但是这段代码将屏幕分为两个部分,每个部分用于其中一个imageviews。 I have left the code that scales the images and have code to set the pivot points, assuming you have some purpose in mind. 假设您有一些目的,我已经留下了缩放图像的代码并具有设置轴心点的代码。 I do suggest you comment them out and try to see if you get the desired results without them. 我确实建议您将它们注释掉,并尝试看看如果没有它们,您是否能获得所需的结果。

activity_main.xml: activity_main.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"
                tools:context=".MainActivity"
                android:id="@+id/root">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/imageView"
        android:layout_alignParentTop="true"
        android:scaleType="fitCenter"
        android:src="@mipmap/ic_launcher" />

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/imageView"
        android:scaleType="fitCenter"
        android:id="@+id/imageView2"

        android:src="@mipmap/ic_launcher" />
</RelativeLayout>

MainActivity.java: MainActivity.java:

import android.support.v4.view.GestureDetectorCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import static com.example.justtesting.R.id.imageView;

public class MainActivity extends AppCompatActivity{

    GestureDetectorCompat detector;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final int minH = 10;
        final RelativeLayout root = (RelativeLayout) findViewById(R.id.root);
        final ImageView iv = (ImageView) findViewById(imageView);
        final ImageView iv2 = (ImageView) findViewById(R.id.imageView2);

        detector = new GestureDetectorCompat(this, new GestureDetector.SimpleOnGestureListener() {

            @Override
            public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
                int height = iv.getHeight() + iv2.getHeight();
                android.view.ViewGroup.LayoutParams layoutParams = iv.getLayoutParams();
                if ((int) distanceY + layoutParams.height < minH)
                    layoutParams.height = minH;
                else if ((int) distanceY + layoutParams.height > height - minH)
                    layoutParams.height = height-minH;
                else
                    layoutParams.height = (int) distanceY + layoutParams.height;
                android.view.ViewGroup.LayoutParams layoutParams2 = iv2.getLayoutParams();
                layoutParams2.height = height-layoutParams.height;
                iv.setLayoutParams(layoutParams);
                iv2.setLayoutParams(layoutParams2);
                return true;
            }
        });
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        return detector.onTouchEvent(event);
    }
}

Notice that I replaced your referenced image with the default launcher image in both ImageViews as I don't have your image file in my project. 请注意,由于我的项目中没有图像文件,因此我在两个ImageView中都用默认的启动器图像替换了引用的图像。

android:src="@mipmap/ic_launcher"

Change it as you see fit. 根据需要更改它。

And here are some screenshots of the app running on Nexus 5 emulator (X86-64)/Android-22 (same result with Android-23). 以下是在Nexus 5模拟器(X86-64)/ Android-22上运行的应用程序的一些屏幕截图(与Android-23相同的结果)。

在此处输入图片说明

在此处输入图片说明

在此处输入图片说明

在此处输入图片说明

Good luck! 祝好运!

您是否尝试过将LinearLaoutlayout_weight

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM