您的位置:首页 > 移动开发 > Android开发

Android Api Demos登顶之路(十九)Rotation Animation

2015-08-05 05:59 543 查看
这个Demo演示了手机屏幕旋转时的动画效果,基本思路就是通过设置Window的属性,实现屏幕旋转时的动画效果, 但要求必须是Api18以上。

在这个小Demo上还出了一点小状况,程序运行时一直看不到效果,开始怀疑是不是自己对rotationAnimation 理解错了?查了一天的资料也没找到问题所在(不过还是有所收获的,呵呵),一天都比较郁闷。

后来才注意到, 当我旋转模拟器的屏幕时,屏幕的状态居然没有调整,于是在oncreate()方法里加了一了syso语句来监听 果然当屏幕旋转时居然没有重新调用oncreate()方法。

我用的是Api19的模拟器。不知道是模拟器有bug还是什么 原因,有知道的大神请帮忙解答下。不知道大家是否也遇到过这种情况。

后来换了一个Api18的模拟器,问题解决,运行正常。

布局文件activity .xml

<LinearLayout 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"
android:orientation="vertical"
android:padding="5dp" >

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/info"
android:textAppearance="?android:attr/textAppearanceMedium" />

<CheckBox
android:id="@+id/cb_fullScren"
android:layout_marginTop="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="FULLSCREEN" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Rotation Animation:"
android:layout_marginTop="10dp"
android:textAppearance="?android:attr/textAppearanceMedium" />

<RadioGroup
android:id="@+id/radioGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:checkedButton="@+id/rb_rotate" >

<RadioButton
android:id="@+id/rb_rotate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ROTATE"/>

<RadioButton
android:id="@+id/rb_crossfade"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="XFADE" />

<RadioButton
android:id="@+id/rb_jumpcut"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="JUMPCUT" />
</RadioGroup>

</LinearLayout>


MainActivity

public class MainActivity extends Activity {
private int mRotationAnimation=LayoutParams.ROTATION_ANIMATION_ROTATE;
private RadioGroup mRadioGroup;
private CheckBox cb_fullScreen;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//设置默认的旋转动画
setRotationAnimation(mRotationAnimation);
setContentView(R.layout.activity_main);
mRadioGroup=(RadioGroup) findViewById(R.id.radioGroup);
cb_fullScreen=(CheckBox) findViewById(R.id.cb_fullScren);

cb_fullScreen.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
setFullScreen(isChecked);
}
});

//注意这里要使用全名RadioGroup.OnCheckedChangeListener()
mRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (checkedId) {
case R.id.rb_rotate:
mRotationAnimation=WindowManager.LayoutParams.ROTATION_ANIMATION_ROTATE;
break;
case R.id.rb_crossfade:
mRotationAnimation=WindowManager.LayoutParams.ROTATION_ANIMATION_CROSSFADE;
break;
case R.id.rb_jumpcut:
mRotationAnimation=WindowManager.LayoutParams.ROTATION_ANIMATION_JUMPCUT;
break;
}
setRotationAnimation(mRotationAnimation);
}
});

}

/**
* 设置窗体是否全屏显示
* @param on
*/
protected void setFullScreen(boolean on) {
Window win=getWindow();
WindowManager.LayoutParams params=win.getAttributes();
if(on){
params.flags |=WindowManager.LayoutParams.FLAG_FULLSCREEN;
}else{
params.flags &= ~WindowManager.LayoutParams.FLAG_FULLSCREEN;
}
win.setAttributes(params);
}

/**
* 设置旋转屏幕时的动画效果
* @param rotationAnimation:系统定义的动画常量
*/
private void setRotationAnimation(int rotationAnimation) {
Window win=getWindow();
WindowManager.LayoutParams params=win.getAttributes();
params.rotationAnimation=mRotationAnimation;
win.setAttributes(params);
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: