qt android开发篇之如何实现在不同分辨率手机中自适应字体大小,或者像素什么的

我们知道现在手机屏幕的分辨率跟电脑的分辨率不同,或许你直接在电脑中设置的字体像素大小为20,在电脑中运行的时候看起来很正常 的字体,但是部署到手机后大家分先那些字看起来像蚂蚁一样小,这时候改怎么办呢?小编通过调用android api来解决这个问题,本来很 高兴的说发现qt中的qpplicationwindows这个有一个devicePixelRatio属性,但是它的值我不知道怎么算到手机上,所以只能调用android API了! 源码: Screen.java

package com.tommego.work;
//tommego
import java.util.List;
import android.content.Context;
//屏幕像素密度
import android.util.DisplayMetrics;

//蓝牙 开关
import android.bluetooth.BluetoothAdapter;

public class Screen extends org.qtproject.qt5.android.bindings.QtActivity{
    private static Screen m_instance;//单例对象
    private static BluetoothAdapter m_bluetoothAdapter;//蓝牙控制对象
    public Screen(){
        m_instance = this;//实例化单例对象


        m_bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();


        if (!m_bluetoothAdapter.isEnabled()){
            //打开蓝牙
            m_bluetoothAdapter.enable();

        }
    }
    //获取屏幕像素密度
    public static double getDentisy(){
        DisplayMetrics metrics=new DisplayMetrics();
        m_instance.getWindowManager().getDefaultDisplay().getMetrics(metrics);
        return metrics.density;
//        return 3;
    }
}

devicescreen.h

#ifndef DEVICESCREEN_H
#define DEVICESCREEN_H
#include <QObject>
#include <QAndroidJniObject>
#include <QList>
#include <QString>

class DeviceScreen : public QObject
{
    Q_OBJECT
public:
    explicit DeviceScreen();
    ~DeviceScreen();

    Q_PROPERTY(double dentisty READ dentisty WRITE setDentisty NOTIFY dentistyChanged)

    //获取屏幕像素密度
    Q_INVOKABLE double getDentisy();

    double dentisty(){
        return this->m_dentisty;
    }
    void setDentisty(double a){
        this->m_dentisty=a;
    }

signals:
    void dentistyChanged();

public slots:
//    void onDentistyChanged
private:
    double m_dentisty;
};
#endif // DEVICESCREEN_H

>##devicscreen.cpp
#include"devicescreen.h"

DeviceScreen::DeviceScreen()
{
    m_dentisty=getDentisy();
    dentistyChanged();

}

DeviceScreen::~DeviceScreen()
{

}

//获取屏幕像素密度
double DeviceScreen::getDentisy(){
    return QAndroidJniObject::callStaticMethod("com/tommego/work/Screen",
                                                    "getDentisy","()D");
}

main.cpp

#include <QApplication>
#include <QQmlApplicationEngine>
#include <QtQml>
#include <QQmlContext>
#include "Device/devicescreen.h"

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);

    QQmlApplicationEngine engine;
    qmlRegisterType<DeviceScreen>("Device",1,0,"MyDevice");

//    QQmlContext *context = new QQmlContext(engine.rootContext());
    DeviceScreen device;
//    context->setContextProperty("mydevice",&device);
//    context->set
    engine.rootContext()->setContextProperty("mydevice",&device);


    engine.load(QUrl(QStringLiteral("qrc:/main.qml")));

    return app.exec();
}

main.qml


import QtQuick 2.4
import QtQuick.Controls 1.3
import QtQuick.Window 2.2
ApplicationWindow {
    width: Screen.desktopAvailableWidth
    height: Screen.desktopAvailableHeight
    visible: true
    Text{
            text:"hello"
            font.pixelSize:20*mydevice.dentisty//设置字体大小
            anchors.centerIn:parent
    }
}

 

转载:http://ju.outofmemory.cn/entry/200667

你可能感兴趣的:(Qt)