Android——例子:屏幕切换

 

 效果图如下:

 

                        

 

Xml文件代码:

<?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" >
    <Button
        android:id="@+id/myBtn"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="点击切换到竖屏"
        />
    <ImageView 
        android:id="@+id/showImage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/aaa"
        />
</LinearLayout>

Java代码

package com.example.clickproject;

import android.os.Bundle;
import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;

public class ScreenActivity extends Activity {
    private Button myBtn = null;
    private ImageView showImage = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.screen);
        myBtn = (Button) findViewById(R.id.myBtn);
        showImage = (ImageView) findViewById(R.id.showImage);
        
        myBtn.setOnClickListener(new MyOnClickListenerImpl());
    }

    private class MyOnClickListenerImpl implements OnClickListener { // 单击事件

        public void onClick(View v) {
            if (ScreenActivity.this.getRequestedOrientation() == ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED) {// 无法进行画面的旋转
                ScreenActivity.this.myBtn.setText("错误:无法改变屏幕方向。");
            } else {
                if (ScreenActivity.this.getRequestedOrientation() == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) { // 现在的方向是横屏显示
                    ScreenActivity.this
                            .setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); // 变为竖屏显示
                } else if (ScreenActivity.this.getRequestedOrientation() == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT) { // 如果为竖屏显示
                    ScreenActivity.this
                            .setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); // 变为横屏显示
                }
            }
        }
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) { // 表示的是系统设置修改的时候触发
        if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) { // 现在的屏幕方向是横屏
            ScreenActivity.this.myBtn.setText("改变屏幕方向为竖屏显示(当前为横屏显示)");
            ScreenActivity.this.showImage.setImageResource(R.drawable.aaa);// 显示横屏图片
        } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) { // 现在竖屏
            ScreenActivity.this.myBtn.setText("改变屏幕方向为竖屏显示(当前为横屏显示)");
            ScreenActivity.this.showImage.setImageResource(R.drawable.aaa);// 显示竖屏图片
        }
        super.onConfigurationChanged(newConfig);
    }
}

AndroidMainifest.xml文件中写入以下代码:

<activity
            android:name="com.example.clickproject.ScreenActivity"
            android:label="@string/app_name"
            android:screenOrientation="portrait" 
            android:configChanges="orientation|keyboard"
             >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        
    </application>
    <uses-permission android:name="android.persmission.CHANGE_CONFIGURATION" />

 

posted @ 2013-07-26 16:34  艺言弈行  阅读(316)  评论(0编辑  收藏  举报