问题描述
- Android Activity切换界面抖动
-
我从一个FullScreen的Activity A跳到另一个不是FullScreen的Activity B,界面会抖动一下
,就是B刚进入的时候貌似也是全屏,然后状态出来,B的布局就被向下挤了,看上去就有抖动,这个怎么解决
解决方案
其原理很简单,就是使用动画让组件来回摆动,代码如下:
MainActivity:
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package com.home.animwindow;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
public class MainActivity extends Activity {
private EditText edit;
private Button btn;
private ImageView image;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edit = (EditText) findViewById(R.id.edit);
image = (ImageView) findViewById(R.id.main_iv);
btn = (Button) findViewById(R.id.btn);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Animation animation = AnimationUtils.loadAnimation(
MainActivity.this, R.anim.myanim);
edit.startAnimation(animation);
image.startAnimation(animation);
}
});
}
}
布局:
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<EditText
android:id="@+id/edit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="100dp"
android:text="点击按钮、此窗口抖动" />
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="按钮" />
<ImageView
android:id="@+id/main_iv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" />
</LinearLayout>
动画myanim:
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@anim/cycle" >
<translate
android:duration="300"
android:fromXDelta="0"
android:fromYDelta="0"
android:toXDelta="-10"
android:toYDelta="-10" />
<translate
android:duration="300"
android:fromXDelta="0"
android:fromYDelta="0"
android:startOffset="300"
android:toXDelta="10"
android:toYDelta="-10" />
<translate
android:duration="300"
android:fromXDelta="0"
android:fromYDelta="0"
android:startOffset="600"
android:toXDelta="-10"
android:toYDelta="10" />
<translate
android:duration="300"
android:fromXDelta="0"
android:fromYDelta="0"
android:startOffset="900"
android:toXDelta="10"
android:toYDelta="10" />
</set>
cycle:
?
1
2
3
<?xml version="1.0" encoding="utf-8"?>
<cycleInterpolator xmlns:android="http://schemas.android.com/apk/res/android"
android:cycles="5" />
解决方案二:
其原理很简单,就是使用动画让组件来回摆动,代码如下:
MainActivity:
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package com.home.animwindow;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
public class MainActivity extends Activity {
private EditText edit;
private Button btn;
private ImageView image;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edit = (EditText) findViewById(R.id.edit);
image = (ImageView) findViewById(R.id.main_iv);
btn = (Button) findViewById(R.id.btn);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Animation animation = AnimationUtils.loadAnimation(
MainActivity.this, R.anim.myanim);
edit.startAnimation(animation);
image.startAnimation(animation);
}
});
}
}
布局:
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<EditText
android:id="@+id/edit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="100dp"
android:text="点击按钮、此窗口抖动" />
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="按钮" />
<ImageView
android:id="@+id/main_iv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" />
</LinearLayout>
动画myanim:
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@anim/cycle" >
<translate
android:duration="300"
android:fromXDelta="0"
android:fromYDelta="0"
android:toXDelta="-10"
android:toYDelta="-10" />
<translate
android:duration="300"
android:fromXDelta="0"
android:fromYDelta="0"
android:startOffset="300"
android:toXDelta="10"
android:toYDelta="-10" />
<translate
android:duration="300"
android:fromXDelta="0"
android:fromYDelta="0"
android:startOffset="600"
android:toXDelta="-10"
android:toYDelta="10" />
<translate
android:duration="300"
android:fromXDelta="0"
android:fromYDelta="0"
android:startOffset="900"
android:toXDelta="10"
android:toYDelta="10" />
</set>
cycle:
?
1
2
3
<?xml version="1.0" encoding="utf-8"?>
<cycleInterpolator xmlns:android="http://schemas.android.com/apk/res/android"
android:cycles="5" />
解决方案三:
全屏和非全屏高度不一样,切换可能会造成视觉差异,可以做个页面切换动画,透明度变化来减少这种视觉差
解决方案四:
不全屏的activity就用dialog代替吧
解决方案五:
app的theme可能是全屏
解决方案六:
Acitivity的全屏是在每个Acitivity的初始化时设置的,按理说不应该出现这样的问题。建议你先试试都是全屏的切换。
时间: 2024-11-17 09:21:19