【问题标题】:Scale parentLayout without scaling its childView缩放 parentLayout 而不缩放其 childView
【发布时间】:2016-12-23 16:17:04
【问题描述】:

我正在缩放和翻译我的父布局,但问题是我的子视图也在缩放,但没有根据其父级进行翻译(我希望我的子视图位于屏幕的中心,无论我的父级在哪里布局正在翻译)。

下面是我的代码

public class Splash extends AppCompatActivity {

Animation zoomIn,zoomOut;
ImageView icon;
LinearLayout relativeLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_splash);


    zoomIn = AnimationUtils.loadAnimation(this,R.anim.zoom_in);
    zoomOut = AnimationUtils.loadAnimation(this,R.anim.zoom_out);

    relativeLayout = (LinearLayout) findViewById(R.id.activity_splash);
    relativeLayout.setBackgroundResource(R.drawable.utt);
    icon = (ImageView) findViewById(R.id.icon);

    relativeLayout.setAnimation(zoomIn);
    relativeLayout.setAnimation(zoomOut);

    LinearLayout.LayoutParams layoutParams=new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
   layoutParams.height= 75;
    layoutParams.width = 75;
    layoutParams.gravity= Gravity.CENTER;
    icon.setLayoutParams(layoutParams);



    zoomIn.setAnimationListener(new Animation.AnimationListener() {
        @Override
        public void onAnimationStart(Animation animation) {




        }

        @Override
        public void onAnimationEnd(Animation animation) {

          relativeLayout.startAnimation(zoomOut);


        }

        @Override
        public void onAnimationRepeat(Animation animation) {

        }
    });

    zoomOut.setAnimationListener(new Animation.AnimationListener() {
        @Override
        public void onAnimationStart(Animation animation) {

            icon.setAlpha(1.0f);

            icon.startAnimation(AnimationUtils.loadAnimation(getApplicationContext(),R.anim.translate_top_center));


        }

        @Override
        public void onAnimationEnd(Animation animation) {

          relativeLayout.startAnimation(zoomIn);


        }

        @Override
        public void onAnimationRepeat(Animation animation) {

        }
    });

}  

这是活动

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_splash"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
tools:context="store.shopnix.com.kenburns.Splash">



<ImageView
    android:id="@+id/icon"
    android:layout_width="75dp"
    android:layout_height="75dp"
    android:alpha="0.0"
    android:src="@drawable/icon"/>

anim.xml

放大

 <scale
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="20000"
    android:fromXScale="1"
    android:fromYScale="1"
    android:pivotX="50%"
    android:pivotY="50%"
    android:toXScale="1.5"
    android:toYScale="1.5"
    android:repeatMode = "reverse"
    android:translationX="100dp"
    android:translationY = "100dp">
</scale>  

缩小

<scale
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="20000"
    android:fromXScale="1.0"
    android:fromYScale="1.0"
    android:pivotX="70%"
    android:pivotY="20%"
    android:toXScale="1.5"
    android:toYScale="1.5"
    android:repeatCount="1"
    android:repeatMode = "reverse"
    android:translationX="20dp"
    android:translationY = "20dp">
</scale>

【问题讨论】:

  • 你不能把你不想动画的东西放在一个单独的布局中吗?为此,您的根布局应允许重叠(例如 RelativeLayoutFrameLayout
  • 这样可以解决这个问题
  • 当然,你缩放一个layout,另一个保持不动。
  • 我正在缩放线性布局,如果我放 视图它会工作
  • 我会为你写一个完整的答案。请等几分钟。

标签: android android-layout android-animation


【解决方案1】:

创建两个独立的布局,一个可以缩放,另一个不可以。

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/layout_to_scale">
        <!--put stuff that scales here-->
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/layout_to_stay">
        <!--put static stuff here-->
    </LinearLayout>

</FrameLayout>

现在在您的 activity 仅动画布局中使用 id = R.id.layout_to_scale

【讨论】:

  • 非常感谢,但我想缩放父背景图像,而不更改其子视图..
  • 只需将ImageView 添加到layout_to_scale 并设置其android:scaleType="centerCrop"。当您感到舒适时,您可以稍微重构代码并将layout_to_scale 替换为您的ImageView
  • 这个工作,但随着背景在放大图像视图也在放大,我们不能有一个相对缩放,以便图像视图相对于背景进行相反的缩放跨度>
  • 嗯,请决定你想要什么,首先你说你想要你的图像缩放,现在你没有。如果您不想缩放某些视图,请将其放入 layout_to_stay
【解决方案2】:

您可以使用 RelativeLayout 代替 LinearLayout 并将 childView 放在其中。

现在您为 childView 添加属性 android:layout_centerInParent="true" 现在您可以缩放 RelativeLayout 而无需缩放 childView 它肯定会停留在 parentView 的中心(RelativeLayout)。

希望这能解决您的问题。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_splash"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="store.shopnix.com.kenburns.Splash">

<ImageView
    android:id="@+id/icon"
    android:layout_width="75dp"
    android:layout_height="75dp"
    android:layout_centerInParent="true"
    android:alpha="0.0"
    android:src="@drawable/icon" />

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-02-24
    • 2021-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多