Android获取屏幕分辨率及dp与 pix间的转换

127 篇文章 0 订阅

获取屏幕分辨率代码:

public void onCreate(Bundle savedInstanceState) {  


  •         super.onCreate(savedInstanceState);  
  •         /*加载页面*/  
  •         setContentView(R.layout.main);  
  •           
  •         /*引用android.util.DisplayMetrics*/  
  •         DisplayMetrics dm = new DisplayMetrics();  
  •         getWindowManager().getDefaultDisplay().getMetrics(dm);  
  •           
  •         int width = dm.widthPixels;  
  •         int height = dm.heightPixels;  
  •           
  •         Button button = (Button)findViewById(R.id.b01);  
  •         button.setText("手机屏幕分辨率为:"+width+"*"+height);  
  • }  

  • // 方法1 Android获得屏幕的宽和高
             int  screenWidth;
             int  screenHeight;
     
             WindowManager windowManager = getWindowManager();
             Display display = windowManager.getDefaultDisplay();
             screenWidth = display.getWidth();
             screenHeight = display.getHeight();
     
             Log.d(TAG,  "screenWidth:"  + screenWidth);
             Log.d(TAG,  "screenHeight:"  + screenHeight);
             
             // 方法2
             dm =  new  DisplayMetrics();
             getWindowManager().getDefaultDisplay().getMetrics(dm);
     
             //获得手机的宽度和高度像素单位为px
             String str =  "方法1 手机屏幕分辨率为:"  + screenWidth + " * " + screenHeight +  "/n方法2  手机屏幕分辨率为:"  + dm.widthPixels + " * " + dm.heightPixels;
             textview1.setText(str);

  • 像素单位转换:

    网上介绍Android布局单位的文章很多,但是我翻了不少,却发现大部分都是一个拷贝的版本,当然网络上也有不少是是根据个人使用习惯写的一些心得,最终经过整合后,walfred将这些很基础的知识给整合吸收了,所以这里会结合自己的理解将Android的布局单位的使用做一个简单的概括。
    单位一览表

    px:单位尺寸里的像素点

    dp:一个基于density的抽象单位,如果一个160dpi的屏幕,1dp=1px

    dip:等同于dp

    sp:同dp相似,但还会根据用户的字体大小偏好来缩放,常用来作为字体大小的单位。

            这些单位我们都见过,但是真正拿到手上用的却(对walfred而言)用到较多的就是dip(dp)和sp呢,所以我会重点讲解这两个单位的。

    像素px与屏幕密度density

            既然知道了像素是单位里面的像素点,我们就可以通过像素来求得屏幕密度,下面的公式即为像素与像素密度的换算关系:

    pixs =dips * (densityDpi/160)

    dips=(pixs*160)/densityDpi

            那我们平时使用的240*320像素的手机(WQVGA/QVGA)、320*480的手机(HVGA)及现在主流的480*800的手机(WVGA),我们都可以算出其屏幕密度,一般我们在计算时计算宽度就可以了,高度同理,所以就得出:

    240*320像素的手机(WQVGA/QVGA)的density=120;

    320*480的手机(HVGA)的density=160;

    480*800的手机(WVGA)density=240;

            1、所以得出结论

            Android的屏幕密度是以160为基准的,屏幕密度(density)为160时,是将一英寸分为160份, 每一份是1像素;同理如果屏幕密度(density)为240时,是将一英寸分为240份,,每一份是1像素,所以近来的新的sdk为了适配不同的屏幕分辨率的机型,已经陆续取消采用像素px作为布局单位这主要是针对不同设备而言的。因为px不管在什么样的设备上都是那样长,但是dip会根据设备变化;

            2、谷歌的策略       

    当屏幕density=240时使用hdpi标签的资源;

    当屏幕density=160时,使用mdpi标签的资源 ;

    当屏幕density=120时,使用ldpi标签的资源

      

            4、单位代码换算

    Java代码
    1. public static int dip2px(Context context, float dipValue){   
    2.         final float scale = context.getResources().getDisplayMetrics().density;   
    3.         return (int)(dipValue * scale + 0.5f);   
    4. }   
    5.  
    6. public static int px2dip(Context context, float pxValue){   
    7.         final float scale = context.getResource().getDisplayMetrics().density;   
    8.         return (int)(pxValue / scale + 0.5f);   
    9. }   
    10.  
    11. public static int dip2px(Context context, float dipValue){   
    12.         final float scale = context.getResources().getDisplayMetrics().density;   
    13.         return (int)(dipValue * scale + 0.5f);   
    14. }   
    15.  
    16. public static int px2dip(Context context, float pxValue){   
    17.         final float scale = context.getResource().getDisplayMetrics().density;   
    18.         return (int)(pxValue / scale + 0.5f);   
    19. }  
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在C++中使用FFmpeg进行屏幕录制和推流获取屏幕分辨率,可以使用FFmpeg的C API。以下是一个简单的示例代码: ```c++ extern "C" { #include <libavutil/avutil.h> #include <libavutil/opt.h> #include <libavformat/avformat.h> #include <libavcodec/avcodec.h> #include <libswscale/swscale.h> #include <libavutil/error.h> } int main() { AVFormatContext *pFormatCtx; AVOutputFormat *pOutputFmt; AVStream *pStream; AVCodecContext *pCodecCtx; AVCodec *pCodec; AVFrame *pFrame; AVPacket pkt; int ret; int width, height; // 初始化FFmpeg av_register_all(); avformat_network_init(); // 打开输出文件 avformat_alloc_output_context2(&pFormatCtx, NULL, NULL, "output.mp4"); if (!pFormatCtx) { printf("Error allocating output context\n"); return -1; } // 添加视频流 pOutputFmt = pFormatCtx->oformat; pStream = avformat_new_stream(pFormatCtx, NULL); if (!pStream) { printf("Error creating new stream\n"); avformat_free_context(pFormatCtx); return -1; } // 设置编码器参数 pCodecCtx = pStream->codec; pCodecCtx->codec_id = pOutputFmt->video_codec; pCodecCtx->codec_type = AVMEDIA_TYPE_VIDEO; pCodecCtx->width = 1920; pCodecCtx->height = 1080; pCodecCtx->pix_fmt = AV_PIX_FMT_YUV420P; pCodecCtx->time_base = (AVRational){1, 30}; pCodecCtx->bit_rate = 400000; // 查找编码器 pCodec = avcodec_find_encoder(pCodecCtx->codec_id); if (!pCodec) { printf("Codec not found\n"); avformat_free_context(pFormatCtx); return -1; } // 打开编码器 if (avcodec_open2(pCodecCtx, pCodec, NULL) < 0) { printf("Error opening codec\n"); avformat_free_context(pFormatCtx); return -1; } // 创建帧 pFrame = av_frame_alloc(); if (!pFrame) { printf("Error allocating frame\n"); avcodec_close(pCodecCtx); avformat_free_context(pFormatCtx); return -1; } pFrame->format = pCodecCtx->pix_fmt; pFrame->width = pCodecCtx->width; pFrame->height = pCodecCtx->height; ret = av_frame_get_buffer(pFrame, 32); if (ret < 0) { printf("Error allocating frame buffer\n"); avcodec_close(pCodecCtx); avformat_free_context(pFormatCtx); return -1; } // 打开屏幕录制设备 AVInputFormat *pInputFmt = av_find_input_format("gdigrab"); AVDictionary *pOptions = NULL; av_dict_set(&pOptions, "framerate", "30", 0); av_dict_set(&pOptions, "draw_mouse", "0", 0); av_dict_set(&pOptions, "offset_x", "0", 0); av_dict_set(&pOptions, "offset_y", "0", 0); av_dict_set(&pOptions, "video_size", "1920x1080", 0); ret = avformat_open_input(&pFormatCtx, "desktop", pInputFmt, &pOptions); if (ret < 0) { char errbuf[AV_ERROR_MAX_STRING_SIZE]; av_strerror(ret, errbuf, AV_ERROR_MAX_STRING_SIZE); printf("Error opening input: %s\n", errbuf); avcodec_close(pCodecCtx); avformat_free_context(pFormatCtx); return -1; } // 读取屏幕分辨率 width = pCodecCtx->width; height = pCodecCtx->height; // 开始录制 avformat_write_header(pFormatCtx, NULL); while (1) { AVFrame *pFrameRaw = av_frame_alloc(); ret = av_read_frame(pFormatCtx, &pkt); if (ret < 0) { if (ret == AVERROR_EOF) { printf("End of file\n"); } else { printf("Error reading frame\n"); } break; } ret = avcodec_send_packet(pCodecCtx, &pkt); if (ret < 0) { printf("Error sending packet\n"); break; } while (ret >= 0) { ret = avcodec_receive_frame(pCodecCtx, pFrameRaw); if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) { break; } if (ret < 0) { printf("Error receiving frame\n"); break; } sws_scale(sws_getContext(width, height, AV_PIX_FMT_BGRA, pCodecCtx->width, pCodecCtx->height, pCodecCtx->pix_fmt, SWS_BILINEAR, NULL, NULL, NULL), (const uint8_t *const *)pFrameRaw->data, pFrameRaw->linesize, 0, height, pFrame->data, pFrame->linesize); pFrame->pts = av_gettime(); ret = avcodec_send_frame(pCodecCtx, pFrame); if (ret < 0) { printf("Error sending frame\n"); break; } } av_packet_unref(&pkt); } av_write_trailer(pFormatCtx); avcodec_close(pCodecCtx); avformat_free_context(pFormatCtx); return 0; } ``` 这个示例代码可以录制整个桌面,并将录制的视频输出为MP4格式的文件。其中,`avformat_open_input()`函数打开gdigrab设备,`avcodec_send_frame()`函数将录制的帧发送给编码器进行压缩,`av_write_frame()`函数将压缩后的帧写入输出文件。在代码中,我们也获取了屏幕的分辨率信息。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值