在Dip(s)中获取屏幕区域

时间:2016-02-21 21:04:34

标签: android android-studio

我正在研究pong克隆,我目前正在使用硬代码像素值来分配像桨和球这样的UI元素。

package com.nblsoft.ballpractise;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.view.MotionEvent;
import android.view.View;



public class BallDraw extends View {

    //Default System Variables

    int screenHeight = getResources().getDisplayMetrics().heightPixels;
    int screenWidth = getResources().getDisplayMetrics().widthPixels;

    private int xMin = 0;          // This view's bounds
    private int xMax;
    private int yMin = 0;
    private int yMax;


    //Game Variables

    public int ball_pos_x = 0;
    public int ball_pos_y = 800;
    public int ball_vel_x = 5;
    public int ball_vel_y = 5;
    public int pad_1_pos = 10;
    public int pad_2_pos = 10;
    public int randi=1;
    //Pads Implemented as Array of points
    //public int[] pad_1 = {100,200,220,240};
    //public int[] pad_2 = {100,200,1250,1270};

    Rect pad_1 = new Rect(400 + pad_1_pos,220,500 + pad_1_pos,240);
    Rect pad_2 = new Rect(400 + pad_2_pos,1250,500 + pad_1_pos,1270);

    Rect pad_1_touch = new Rect(0, 0, 1080, 200);
    Rect pad_2_touch = new Rect(0, 1300, 1080, 1500);


    //calculations for screen sizes



    private Paint paint;


    public BallDraw(Context context) {
        super(context);
        paint = new Paint();

        // To enable touch mode
        this.setFocusableInTouchMode(true);
    }


    @Override
    public boolean onTouchEvent(MotionEvent event) {

        int x = (int) event.getX();
        int y = (int) event.getY();

        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
            //case MotionEvent.ACTION_MOVE:

                if(pad_1_touch.contains(x,y)){
                    //pad_1[0]+=10;
                    //pad_1[1]+=10;

                    pad_1.offset(pad_1_pos,0);

                }
                else if(pad_2_touch.contains(x,y)){
                    //pad_2[0]+=10;
                    //pad_2[1]+=10;

                    pad_2.offset(pad_2_pos,0);
                }

        }
        return true; //Event Handled

    }

    @Override
    protected void onDraw(Canvas canvas) {

        paint.setColor(Color.WHITE);

        //Touch Regions (Un-comment to see touch Regions)
        //canvas.drawRect(pad_1_touch,paint);
        //canvas.drawRect(pad_2_touch,paint);

        //Draw Border
        canvas.drawRect(0,800,1080,803,paint);

        //Paddles Draw code
        canvas.drawRect(pad_1,paint);
        canvas.drawRect(pad_2,paint);
        //canvas.drawRect(pad_1[0],pad_1[2], pad_1[1], pad_1[3], paint);
        //canvas.drawRect(pad_2[0],pad_2[2],pad_2[1],pad_2[3],paint);

        //Ball code
        canvas.drawRect(ball_pos_x,ball_pos_y,ball_pos_x + 13, ball_pos_y+13,paint);

        // Update the position of the ball, including collision detection and reaction.
        update();

        // Delay
        try {
            Thread.sleep(30);
        } catch (InterruptedException e) { }

        invalidate();
    }

    @Override
    public void onSizeChanged(int w, int h, int oldW, int oldH) {
        // Set the movement bounds for the ball
        xMax = w-1;
        yMax = h-1;
    }

    private void update(){

        ball_pos_y += ball_vel_y;
        ball_pos_x += ball_vel_x;

        if(pad_1.contains(ball_pos_x,ball_pos_y)){
            ball_vel_y = -ball_vel_y;

        }
        else if(pad_2.contains(ball_pos_x,ball_pos_y)){
            ball_vel_y = -ball_vel_y;

        }
        else if(ball_pos_x >= xMax){
            ball_vel_x = -ball_vel_x;
        }
        else if(ball_pos_y >= yMax){
            ball_init();
        }
        else if(ball_pos_x <= 0){
            ball_vel_x = -ball_vel_x;
        }
        else if(ball_pos_y <= 0){
            ball_init();
        }

    }

    private void ball_init(){
        if (randi == 1){
            randi = -1;
        }
        else{
            randi= 1;
        }

        ball_pos_x=0;
        ball_pos_y=800;
        ball_vel_x=5;
        ball_vel_y=5 * randi;

    }

它与我的调试连接工作正常,但其他设备的屏幕尺寸很小。我正在寻找一些不同大小的设备接口保持不变的东西。我尝试了this方法,因此我可以在蘸水中获得可用的屏幕区域,但计算结束了。在我的1920 * 1080设备上,dpheight和dpwidth返回为509和316。我该怎么做到这一点?

编辑:好的,我已经弄明白了。如果我使用configuration.screenHeightDp计算下降,然后将该dp更改为像素,我将获得正在运行的屏幕应用程序的高度。

//set screen constrains in dip
    Configuration configuration = this.getResources().getConfiguration();
    int dpHeight = configuration.screenHeightDp; //The current height of the available screen space, in dp units, corresponding to screen height resource qualifier.
    int dpWidth = configuration.screenWidthDp; //The current width of the available screen space, in dp units, corresponding to screen width resource qualifier.
    //conversion methods
 private int dptopixel(int DESIRED_DP_VALUE){

        final float scale = getResources().getDisplayMetrics().density;    
        return (int)((DESIRED_DP_VALUE) * scale + 0.5f);
    }

    private int pixeltodp(int DESIRED_PIXEL_VALUE){

        final float scale = getResources().getDisplayMetrics().density;    
        return (int) ((DESIRED_PIXEL_VALUE) - 0.5f / scale);
    }

0 个答案:

没有答案