[英]Android not animating the Relative Layout
I am creating a Relative layout which I want should come from above sliding into the layout so here's what I did我正在创建一个相对布局,我希望它应该从上方滑入布局,所以这就是我所做的
BUT但
when view is created the layout is at its right location with out showing any sliding effect from coming from top of screen创建视图时,布局位于正确的位置,不会显示来自屏幕顶部的任何滑动效果
public class OverlayActivity extends Activity implements View.OnClickListener {
RelativeLayout question_box;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_overlay);
// Slide Up the INVISIBLE layout so that I can call it by animation back to its original position
question_box = findViewById(R.id.question_box);
question_box.animate().translationY(-question_box.getHeight());
final Window window = getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
| WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
}
public void animateInObjects() {
question_box.setVisibility(View.VISIBLE);
question_box.animate().setDuration(1000).translationY(0);
}
@Override
public void onClick(View v) {
//Some Code
}
@Override
protected void onStop() {
super.onStop();
finish();
}
@Override
public void onWindowFocusChanged(boolean hasFocus) {
animateInObjects();
super.onWindowFocusChanged(hasFocus);
}
}
Layout布局
<RelativeLayout
android:id="@+id/question_box"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:color/transparent"
android:layout_marginTop="5dp"
android:layout_below="@+id/ad_view_container"
android:visibility="invisible">
<TextView
android:id="@+id/question"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/questiontext"
android:paddingStart="20dp"
android:paddingTop="7dp"
android:paddingEnd="20dp"
android:paddingBottom="20dp"
android:text="@string/sample_question"
android:textAlignment="center"
android:textColor="@color/text_quest"
android:textSize="23sp" />
<View
android:id="@+id/center_vertical"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_centerVertical="true" />
<LinearLayout
android:id="@+id/cover_opt1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/question"
android:layout_marginStart="15dp"
android:layout_marginTop="10dp"
android:background="@drawable/main_layout">
<Button
android:id="@+id/opt1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginVertical="1dp"
android:layout_marginHorizontal="2dp"
android:background="@android:color/transparent"
android:text="@string/sample_number"
android:textAlignment="center"
android:textColor="@color/text_quest"
android:textSize="18sp" />
</LinearLayout>
<LinearLayout
android:id="@+id/cover_opt2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/question"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:background="@drawable/main_layout">
<Button
android:id="@+id/opt2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginVertical="1dp"
android:layout_marginHorizontal="2dp"
android:background="@android:color/transparent"
android:text="@string/sample_number"
android:textAlignment="center"
android:textColor="@color/text_quest"
android:textSize="18sp" />
</LinearLayout>
<LinearLayout
android:id="@+id/cover_opt3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/question"
android:layout_alignParentEnd="true"
android:layout_marginTop="10dp"
android:layout_marginEnd="15dp"
android:background="@drawable/main_layout">
<Button
android:id="@+id/opt3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginVertical="1dp"
android:layout_marginHorizontal="2dp"
android:background="@android:color/transparent"
android:text="@string/sample_number"
android:textAlignment="center"
android:textColor="@color/text_quest"
android:textSize="18sp" />
</LinearLayout>
</RelativeLayout>
this is the theme of the activity这是活动的主题
<style name="Theme.Lockscreen" parent="Theme.AppCompat.NoActionBar">
<item name="android:background">#33000000</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:colorBackgroundCacheHint">@null</item>
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowAnimationStyle">@android:style/Animation</item>
</style>
well you try to animate the view in onCreate method.好吧,您尝试在 onCreate 方法中为视图设置动画。 at first the view is not drawn yet and you get a
getHeight = 0
.so you must wait when the view is drawn by using view.getViewTreeObserver().addOnGlobalLayoutListener
to be able to animate it起初视图还没有绘制,你得到一个
getHeight = 0
。所以你必须等待使用view.getViewTreeObserver().addOnGlobalLayoutListener
绘制视图才能对其进行动画处理
you need to add this on your onCreate():你需要在你的 onCreate() 上添加这个:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity2);
// Slide Up the INVISIBLE layout so that I can call it by animation back to its original position
question_box = findViewById(R.id.question_box);
question_box.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
if (question_box.getHeight() != 0)
question_box.animate().translationY(-question_box.getHeight());
}
});
final Window window = getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
| WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
}
Try this as your xml试试这个作为你的 xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<scale
android:fromYScale="0"
android:toYScale="50%"
android:duration="1000"></scale>
</set>
To have it till half of your screen and arrange layout respectively让它直到屏幕的一半并分别排列布局
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.