缩放parentLayout而不缩放其childView

时间:2016-12-23 16:17:04

标签: android android-layout android-animation

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

以下是我的代码

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

zoom_in

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

zoom_out

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

2 个答案:

答案 0 :(得分:0)

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

activity

现在位于id仅限动画的布局中R.id.layout_to_scale = .map { $0 != nil } .map{return ($0 != nil)}

答案 1 :(得分:0)

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

现在为childView添加属性 android:layout_centerInParent =“true” 现在你可以缩放 RelativeLayout 而不缩放childView它肯定会保留在parentView的中心( RelativeLayout )。

希望这能解决你的问题。

 let url = URL(string: "tel://123456789")!
 UIApplication.shared.open(url)