การใช้งานก็สามารถเรียกใช้งานผ่าน class Screensize ได้เลย โดยต้องส่ง context ไปกับเมธอดด้วย
1 2 | int widthScreen = ScreenSize.getWidth(context); int heightScreen = ScreenSize.getHeight(context); |
Code Class ScreenSize : สร้าง class ScreenSize แล้วนำ code ด้านล่างไปใส่
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | package com.royle.screensize; import android.content.Context; import android.graphics.Point; import android.os.Build; import android.view.Display; import android.view.WindowManager; /** * Created by royle on 8/25/2015. */ public class ScreenSize { static int width = 0; static int height = 0; public static int getWidth(Context context) { WindowManager window = (WindowManager) context .getSystemService(Context.WINDOW_SERVICE); Display display = window.getDefaultDisplay(); if (Build.VERSION.SDK_INT>12) { Point point = new Point(); display.getSize(point); width = point.x; }else { width = display.getWidth(); } return width; } public static int getHeight(Context context) { WindowManager window = (WindowManager) context .getSystemService(Context.WINDOW_SERVICE); Display display = window.getDefaultDisplay(); if (Build.VERSION.SDK_INT>12) { Point point = new Point(); display.getSize(point); height = point.y; }else { height = display.getHeight(); } return height; } } |
ไม่มีความคิดเห็น :
แสดงความคิดเห็น