【发布时间】: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>
【问题讨论】:
-
你不能把你不想动画的东西放在一个单独的布局中吗?为此,您的根布局应允许重叠(例如
RelativeLayout或FrameLayout) -
这样可以解决这个问题
-
当然,你缩放一个
layout,另一个保持不动。 -
我正在缩放线性布局,如果我放
视图它会工作 -
我会为你写一个完整的答案。请等几分钟。
标签: android android-layout android-animation