Trên Android TV, các chế độ cài đặt được cung cấp để người dùng xác định kiểu phụ đề của riêng mình. Hướng dẫn này minh hoạ cách ứng dụng có thể lấy và áp dụng kiểu phụ đề do hệ thống cung cấp.
Bạn có thể tìm thấy các lựa chọn về phụ đề trong phần Cài đặt > Hệ thống > Hỗ trợ tiếp cận > Phụ đề:
Tải CaptioningManager
Từ một hoạt động, bạn có thể lấy dịch vụ phụ đề qua Context
của hoạt động đó bằng cách sử dụng CaptioningManager
:
CaptioningManager captioningManager = (CaptioningManager)context.getSystemService(Context.CAPTIONING_SERVICE);
Xử lý các thay đổi về kiểu phụ đề
Sau đó, bạn có thể xử lý các thay đổi về kiểu phụ đề bằng cách triển khai CaptioningChangeListener
:
if (captioningManager != null) {
// Define a class to store the CaptionStyle details.
CurrentCaptionStyle currentCaptionStyle = new CurrentCaptionStyle;
// Define the listeners.
captioningManager.addCaptioningChangeListener(new CaptioningChangeListener() {
@Override
public void onEnabledChanged(boolean enabled) {
super.onEnabledChanged(enabled);
Log.d(TAG, "onEnabledChanged");
currentCaptionStyle.isEnabled = enabled;
}
@Override
public void onLocaleChanged(@Nullable Locale locale) {
super.onLocaleChanged(locale);
Log.d(TAG, "onLocaleChanged");
currentCaptionStyle.locale = locale;
}
@Override
public void onFontScaleChanged(float fontScale) {
super.onFontScaleChanged(fontScale);
Log.d(TAG, "onFontScaleChanged");
currentCaptionStyle.fontScale = fontScale;
}
@Override
public void onUserStyleChanged(@NonNull CaptionStyle userStyle) {
super.onUserStyleChanged(userStyle);
Log.d(TAG, "onUserStyleChanged");
currentCaptionStyle.hasBackgroundColor = userStyle.hasBackgroundColor();
currentCaptionStyle.backgroundColor = userStyle.backgroundColor;
currentCaptionStyle.backgroundOpcity = userStyle.backgroundColor >>> 24;
currentCaptionStyle.hasForegroundColor = userStyle.hasForegroundColor();
currentCaptionStyle.foregroundColor = userStyle.foregroundColor;
currentCaptionStyle.foregroundOpacity = userStyle.foregroundColor >>> 24;
currentCaptionStyle.hasWindowColor = userStyle.hasWindowColor();
currentCaptionStyle.windowColor = userStyle.windowColor;
currentCaptionStyle.windowOpcity = userStyle.windowColor >>> 24;
currentCaptionStyle.hasEdgeColor = userStyle.hasEdgeColor();
currentCaptionStyle.edgeColor = userStyle.edgeColor;
currentCaptionStyle.hasEdgeType = userStyle.hasEdgeType();
currentCaptionStyle.edgeType = userStyle.edgeType;
currentCaptionStyle.typeFace = userStyle.getTypeface();
}
});
Để lấy hệ thống CaptionStyle
, bạn có thể gọi trực tiếp getUserStyle()
:
CaptionStyle systemCaptionStyle = captioningManager.getUserStyle();