如何在JavaFX中获得系统DPI?

时间:2016-04-28 07:08:57

标签: javafx dpi

从Java 8u60开始,JavaFX似乎隐式地根据当前系统DPI缩放所有坐标。

例如,如果Windows DPI设置从100%(96 DPI)更改为200%(192 DPI),则JavaFX会使所有组件按预期的两倍大。无论是以em单位还是像素给出坐标,都是如此。

然而,Screen.getPrimary().getDpi()仍然返回96,可能是因为屏幕的DPI实际上没有变化,只有系统DPI设置。

如何获得JavaFX用来扩展组件的系统DPI值?

1 个答案:

答案 0 :(得分:2)

可以单独管理缩放,例如在Windows上为125%:

fireBaseConnection.reg_schools.addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {

        for(DataSnapshot snapshot : dataSnapshot.getChildren()){
            resultProcessorObject = snapshot.child("school_name").getValue(String.class);
            //schools_retreived =  resultProcessorObject.getSchool_name();
            schools.add(resultProcessorObject);
            count = dataSnapshot.getChildrenCount();
            Toast.makeText(Main2Activity.this, "Total number of " + count + " schools were retreived ", Toast.LENGTH_SHORT).show();
            // schools = (ArrayList<Object>) snapshot.getValue();
            // Toast.makeText(Main2Activity.this, "Schools are : " + schools, Toast.LENGTH_SHORT).show();
        }

        Toast.makeText(Main2Activity.this, "Schools are : " + schools, Toast.LENGTH_SHORT).show();
        pBar.dismiss();
    }


    @Override
    public void onCancelled(DatabaseError databaseError) {

    }
});

输出:

Screen screen = Screen.getPrimary();
double dpi = screen.getDpi();
double scaleX = screen.getOutputScaleX();
double scaleY = screen.getOutputScaleY();

System.out.println("DPI: " + dpi + " - scaleX: " + scaleX + " - scaleY: " + scaleY);

修改 从Java 9开始!