android旋转屏幕 简册,[Android][旋转屏幕]

1.落笔缘由

最近在研究旋转屏幕,网上可以找到资料,发现他们基本都是在Activity的基础上进行旋转。自己也想研究一下,能不能实现只旋转屏幕的内容,而不旋转屏幕上的菜单。例如,我点击屏幕上的按钮,页面的内容旋转,而按钮不跟随旋转。

2.具体实践

下面的代码是每调用一次就旋转90度,它是在Ac。

public class Test extends Activity implements OnClickListener {

private LinearLayout body = null;

private LinearLayout.LayoutParams params = null;

private LinearLayout innerBody = null;

private TextView tv = null;

private Button btn1 = null;

private SharedPreferences preferences = null;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

initView();

setContentView(body);

preferences = getSharedPreferences("test",Context.MODE_PRIVATE);

Editor editor=preferences.edit();

editor.putFloat("degree", 0);

editor.commit();

}

private void initView() {

try {

body = new LinearLayout(this);

if (body != null) {

body.setOrientation(LinearLayout.VERTICAL);

params = new LayoutParams(LayoutParams.MATCH_PARENT,

LayoutParams.MATCH_PARENT);

if (params != null) {

body.setLayoutParams(params);

}

innerBody = new LinearLayout(this);

if (innerBody != null) {

innerBody.setOrientation(LinearLayout.VERTICAL);

params = new LayoutParams(LayoutParams.MATCH_PARENT,

LayoutParams.MATCH_PARENT);

if (params != null) {

innerBody.setLayoutParams(params);

}

body.addView(innerBody);

tv = new TextView(this);

if (tv != null) {

tv.setTextSize(40);

tv.setText("测试一下");

innerBody.addView(tv);

}

btn1 = new Button(this);

if (btn1 != null) {

btn1.setTextSize(40);

btn1.setText("Button1");

innerBody.addView(btn1);

btn1.setOnClickListener(this);

}

}

}

} catch (Exception e) {

}

}

/**

* 本来想通过Property Animation动画来实现旋转屏幕,但是发现有好多事情要处理。

* 例如旋转屏幕后,你要重新计算body的大小;还要注意当打开设置里的自动选择按钮后,当界面随平板旋转的时候,我们要重新设置保存在

* SharedPreferences里的值

* @param view

*/

private void animRoateScreen(View view)

{

float startDegree = 0;

if (preferences.getFloat("degree", 0)==0) {

startDegree = 90f;

}

else if (preferences.getFloat("degree", 0)==90f) {

startDegree = 180f;

}

else if (preferences.getFloat("degree", 0)==180f) {

startDegree = 270f;

}

else if (preferences.getFloat("degree", 0)==270f) {

startDegree = 0f;

}

Editor editor=preferences.edit();

editor.putFloat("degree", startDegree);

editor.commit();

ObjectAnimator animatorx = ObjectAnimator

.ofFloat(view, "rotation", startDegree,startDegree+90f);

animatorx.start();

}

/*

* 在一些特殊的情况中,你可能希望当一种或者多种配置改变时避免重新启动你的activity。你可以通过在manifest中设置android

* :configChanges属性来实现这点。

* 你可以在这里声明activity可以处理的任何配置改变,当这些配置改变时不会重新启动activity,而会调用activity的

* onConfigurationChanged

* (Resources.Configuration)方法。如果改变的配置中包含了你所无法处理的配置(在android

* :configChanges并未声明),

* 你的activity仍然要被重新启动,而onConfigurationChanged(Resources.

* Configuration)将不会被调用。

*

* 其次:android:configChanges=""中可以用的值:keyboard|mcc|mnc|locale|touchscreen|

* keyboardHidden|navigation|orientation…… Configuration

* 类中包含了很多种信息,例如系统字体大小,orientation,输入设备类型等等.(如上图)

* 比如:android:configChanges="orientation|keyboard|keyboardHidden"

*

*

* 1.需要在AndroidMenifast对应的Activity配置android:configChanges=

* "orientation|screenSize" 2.需要在AndroidMenifast配置权限

* android:name="android.permission.CHANGE_CONFIGURATION" >

*/

@Override

public void onConfigurationChanged(Configuration newConfig) {

super.onConfigurationChanged(newConfig);

Log.i("lgy", "onConfigurationChanged========");

if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {

} else {

}

}

private void rotationScreen(Context mContext) {

Activity activity = null;

if (mContext instanceof Activity) {

activity = (Activity) mContext;

}

if (0 == getDisplayRotation(activity)) {

activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

} else if (90 == getDisplayRotation(activity)) {

activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT);

} else if (180 == getDisplayRotation(activity)) {

activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE);

} else if (270 == getDisplayRotation(activity)) {

activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

}

}

/**

* 获取当前屏幕旋转角度

*

* @param activity

* @return 0表示是竖屏; 90表示是左横屏; 180表示是反向竖屏; 270表示是右横屏

*/

private int getDisplayRotation(Activity activity) {

if (activity == null)

return 0;

int rotation = activity.getWindowManager().getDefaultDisplay()

.getRotation();

switch (rotation) {

case Surface.ROTATION_0:

return 0;

case Surface.ROTATION_90:

return 90;

case Surface.ROTATION_180:

return 180;

case Surface.ROTATION_270:

return 270;

}

return 0;

}

@Override

public void onClick(View v) {

if (v == btn1) {

// rotationScreen(this);

animRoateScreen(body);

}

}

}

3.总结

经过粗糙的研究,发现没那么简单实现,还是等到有时间或者有其他思路再研究。

4.参考文章

5.源码地址

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值