BB10 Cascades介绍之Image和ImageTracker

Image和ImageTracker

Image控件负责载入本地图片,包括资源图片和本地路径的图片,它必须是即刻得到且不会失败的。
// Absolute path
ImageView {
    imageSource: "asset:///images/myimage.png"  
}
 
// Relative path
ImageView {
    imageSource: "images/myimage.png"  
}


当然,如果外部图片在本地,可以即刻得到,也可以使用Image控件,需要使用绝对路径并加file://前缀。可能需要借助C++来取得QDir::currentPath路径:
// ApplicationUI.cpp
 
// Load the QML file
QmlDocument *qml = QmlDocument::create("asset:///main.qml");
 
// Retrieve the path to the app's working directory
QString workingDir = QDir::currentPath();
 
// Build the path, add it as a context property, and expose
// it to QML
QDeclarativePropertyMap* dirPaths = new QDeclarativePropertyMap;
dirPaths->insert("camera", QVariant(QString(
        "file://" + workingDir + "/shared/camera/")));
qml->setContextProperty("dirPaths", dirPaths);
// main.qml
 
// Load the image asynchronously
ImageView {
    imageSource: dirPaths.camera + "camera0001.jpg" 
}



从网络得图片是异步操作,需要在Image中包含ImageTracker,将它的imageSource属性设置为图片地址,并编写onStateChanged事件处理函数,赋值Image.imgae =
ImageTracker.image:
ImageView {
    id: myImageView
    attachedObjects: [
        ImageTracker {
            id: tracker
            imageSource: "images/image.png"
               
            onStateChanged: {                    
                if (state == ResourceState.Loaded)
                {
                    myImageView.image = tracker.image
                }
            }
        }
    ]
}

参考网页:http://developer.blackberry.com/cascades/documentation/ui/image_resources/index.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章