Android竖屏模式实现横屏效果

android横竖屏切换是非常耗时间的,而且切换的过程也会存在明显的翻转卡顿效果,那么是否可以在竖屏模式下实现横屏的效果呢?答案是肯定的,这里提供一种方法,其思路是对view进行90度的旋转,而且实现起来非常简单,其效果图如下:

首先我们先在layout里排版出一个水平的布局,如下图:

是不是很简单?布局代码如下:

<?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">

    <LinearLayout
        android:id="@+id/layout_horizontal"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="hello my android , i'm obo" />

        <View
            android:layout_width="match_parent"
            android:layout_height="20dp"
            android:background="#FF0000" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="button" />
    </LinearLayout>
</LinearLayout>

以上代码中id为layout_horizontal的LinearLayout是我们将要进行旋转的布局。需要注意的是,待旋转的布局的父布局必须是LinearLayout,比如这里id为layout_horizontal的LinearLayout的外面还必须套一个LinearLayout。

activity中的代码非常简单,思路是先获取到屏幕的尺寸,然后将id为layout_horizontal的LinearLayout的尺寸进行主动的设置,其宽为屏幕的高,其高为屏幕的宽,之后再旋转90度,旋转完之后进行位移,这里的位移是为了将旋转后的布局放置在屏幕中心。

public class HorizontalActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_horizontal);

        DisplayMetrics displayMetrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);      // 获取屏幕的尺寸
        int width = displayMetrics.widthPixels;                         // 屏幕宽
        int height = displayMetrics.heightPixels;                       // 屏幕高

        LinearLayout layout = (LinearLayout) findViewById(R.id.layout_horizontal);  // 待旋转布局

        layout.setLayoutParams(new LinearLayout.LayoutParams(height, width));       // 设置布局的宽和高,必须要和屏幕的反过来
        layout.setRotation(90);             // 顺时针旋转90度
        layout.setY((height - width) / 2);
        layout.setX((width - height) / 2);  // 将布局位移到屏幕中心
    }

}
时间: 2024-08-02 14:40:46

Android竖屏模式实现横屏效果的相关文章

视频录制-android 竖屏录制 电脑播放旋转了90度

问题描述 android 竖屏录制 电脑播放旋转了90度 对于横屏录的,在电脑上播放正常. 竖屏录制,手机上录制和播放正常,但把竖屏录制的视频拷到电脑上,播放时被逆时针旋转了90度... 解决方案 楼主,你这个问题解决没 解决方案二: 楼主,你这问题解决了没,求共享啊 解决方案三: http://blog.csdn.net/he1454023820/article/details/49464423,我解决了,可以参考下 解决方案四: 无解...........

iphone-UIImagePickerController只支持竖屏模式

问题描述 UIImagePickerController只支持竖屏模式 根据IOS文档说明,**The UIImagePickerController class supports portrait mode only**,就是UIImagePickerController类只能支持竖屏模式,有没有什么方法能让横屏显示? 解决方案 可以用ALAssetsLibrary 获取设备中的图片,然后像uiimagepicker一样显示为横屏和竖屏 - (void)viewDidLoad { [super

Delphi XE之路(5)让应用程序禁止竖屏(只显示横屏)

怎么样让Delphi For iOS开发的应用程序禁止竖屏,也就是想让它一直横屏显示,横屏是好,一行可以放好几个控件,表格的话也可以多显示几列,看起来方便. 只要一句代码就可以让Delphi For iOS开发的应用程序禁止竖屏,如下: [delphi] view plaincopy Application.FormFactor.Orientations := [TFormOrientation.soLandscape, TFormOrientation.soInvertedLandscape]

Android开源二维码识别项目zxing横屏改为竖屏识别解决方案

在网上找了很多方法,但最后都有问题,自己调试了好几个小时,最后终于完美解决了竖屏识别. 首先你需要有zxing项目的简化版代码. 使用简化版可以免去许多不必要的代码,方便学习研究,更好定位核心功能. 如果你调试成功后,就可以着手修改将其变为竖屏识别了. 第1步: 在AndroidManifest中将CaptureActivity的screenOrientation属性做如下修改: android:screenOrientation="portrait"  第2步: 我们要把摄像头预览景

Android判断屏幕是横屏或是竖屏的简单实现方法_Android

本文所述为一个Android的常用技巧代码,主要用于判断手机屏幕是横向或是竖向的,在判断屏幕水平或垂直后你可以对程序做出相应的响应,该实例代码只是判断是否为竖屏,若判断正确返回true,否则返回false. 具体的程序代码如下: package com.lurencun.cfuture09.androidkit.util.ui; import android.content.Context; import android.content.res.Configuration; public cla

基于jQuery的判断iPad、iPhone、Android是横屏还是竖屏的代码_jquery

其实主要是通过window.orientation实现,下面看下代码吧 复制代码 代码如下: function orient() {if (window.orientation == 90 || window.orientation == -90) {//ipad.iphone竖屏:Andriod横屏$("body").attr("class", "landscape");orientation = 'landscape';return fals

使用JavaScript判断手机浏览器是横屏还是竖屏问题_javascript技巧

//判断手机横竖屏状态: function hengshuping(){ if(window.orientation==180||window.orientation==0){ alert("竖屏状态!") } if(window.orientation==90||window.orientation==-90){ alert("横屏状态!") } } window.addEventListener("onorientationchange" i

iOS中将个别页面强制横屏其他页面竖屏_IOS

 在AppDelegate.h里面添加@property(nonatomic,assign)NSInteger allowRotation; 在AppDelegate.m文件里面添加 - (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window { if (_allowRotation == 1) { return UIInterf

WPF判断surface+win8下的横屏 竖屏状态

今天在做surface2+win8.1的一个wpf程序,程序固定了分辨率和高度宽度了,也就是说直接不支持竖屏模式了. 当设备从横屏切换到竖屏时,WPF程序变小,有些控件被覆盖,当从竖屏重新切换到横屏时,程序不会变成原来的大小,还是在竖屏时的大小. 注:该程序外面是window,里面是page,发现page里面的控件会被覆盖 但是在竖屏模式下怎么提示用户那? 找到了下面这个方法: 在你的主窗体中加上一个事件注册DisplaySettingsChanged:  代码如下 复制代码 Microsoft