下面列出了android.widget.ImageButton# setContentDescription ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private void updateComplicationView(ComplicationProviderInfo complicationProviderInfo,
ImageButton button, ImageView background) {
if (complicationProviderInfo != null) {
button.setImageIcon(complicationProviderInfo.providerIcon);
button.setContentDescription(
mContext.getString(R.string.edit_complication,
complicationProviderInfo.appName + " " +
complicationProviderInfo.providerName));
background.setVisibility(View.VISIBLE);
} else {
button.setImageDrawable(mDefaultComplicationDrawable);
button.setContentDescription(mContext.getString(R.string.add_complication));
background.setVisibility(View.INVISIBLE);
}
}
/**
* Initialize a known button with a click listener and a drawable resource id,
* and a content description resource id.
* Sets the button visible.
*/
public void initializePushButton(int buttonId, View.OnClickListener cb,
int imageId, int contentDescriptionId)
{
ImageButton button = getImageButtonOrError(buttonId);
button.setOnClickListener(cb);
if (imageId != NO_RESOURCE)
{
button.setImageResource(imageId);
}
if (contentDescriptionId != NO_RESOURCE)
{
button.setContentDescription(mAppController
.getAndroidContext().getResources().getString(contentDescriptionId));
}
if (!button.isEnabled())
{
button.setEnabled(true);
if (mListener != null)
{
mListener.onButtonEnabledChanged(this, buttonId);
}
}
button.setTag(R.string.tag_enabled_id, buttonId);
if (button.getVisibility() != View.VISIBLE)
{
button.setVisibility(View.VISIBLE);
if (mListener != null)
{
mListener.onButtonVisibilityChanged(this, buttonId);
}
}
}
private ImageButton inflateButton(final Context context, @DrawableRes final int icon, @StringRes final int categoryName, final ViewGroup parent) {
final ImageButton button = (ImageButton) LayoutInflater.from(context).inflate(R.layout.emoji_view_category, parent, false);
button.setImageDrawable(AppCompatResources.getDrawable(context, icon));
button.setColorFilter(themeIconColor, PorterDuff.Mode.SRC_IN);
button.setContentDescription(context.getString(categoryName));
parent.addView(button);
return button;
}
public static void updateContentDescription(
ImageButton button,
int stringId,
String sensorId,
Context context,
AppAccount appAccount,
Trial trial) {
String content =
ProtoSensorAppearance.getAppearanceFromProtoOrProvider(
trial.getAppearances().get(sensorId),
sensorId,
AppSingleton.getInstance(context).getSensorAppearanceProvider(appAccount))
.getName(context);
button.setContentDescription(content);
}
/**
* Builds an {@link ImageButton} from the data in this params. Generated buttons should be
* placed on the bottom bar. The button's tag will be its id.
* @param parent The parent that the inflated {@link ImageButton}.
* @param listener {@link OnClickListener} that should be used with the button.
* @return Parsed list of {@link CustomButtonParams}, which is empty if the input is invalid.
*/
ImageButton buildBottomBarButton(Context context, ViewGroup parent, OnClickListener listener) {
if (mIsOnToolbar) return null;
ImageButton button = (ImageButton) LayoutInflater.from(context)
.inflate(R.layout.custom_tabs_bottombar_item, parent, false);
button.setId(mId);
button.setImageBitmap(mIcon);
button.setContentDescription(mDescription);
if (mPendingIntent == null) {
button.setEnabled(false);
} else {
button.setOnClickListener(listener);
}
button.setOnLongClickListener(new OnLongClickListener() {
@Override
public boolean onLongClick(View view) {
final int screenWidth = view.getResources().getDisplayMetrics().widthPixels;
final int screenHeight = view.getResources().getDisplayMetrics().heightPixels;
final int[] screenPos = new int[2];
view.getLocationOnScreen(screenPos);
final int width = view.getWidth();
Toast toast = Toast.makeText(
view.getContext(), view.getContentDescription(), Toast.LENGTH_SHORT);
toast.setGravity(Gravity.BOTTOM | Gravity.END,
screenWidth - screenPos[0] - width / 2,
screenHeight - screenPos[1]);
toast.show();
return true;
}
});
return button;
}
/**
* Builds an {@link ImageButton} from the data in this params. Generated buttons should be
* placed on the bottom bar. The button's tag will be its id.
* @param parent The parent that the inflated {@link ImageButton}.
* @param listener {@link OnClickListener} that should be used with the button.
* @return Parsed list of {@link CustomButtonParams}, which is empty if the input is invalid.
*/
ImageButton buildBottomBarButton(Context context, ViewGroup parent, OnClickListener listener) {
if (mIsOnToolbar) return null;
ImageButton button = (ImageButton) LayoutInflater.from(context)
.inflate(R.layout.custom_tabs_bottombar_item, parent, false);
button.setId(mId);
button.setImageBitmap(mIcon);
button.setContentDescription(mDescription);
if (mPendingIntent == null) {
button.setEnabled(false);
} else {
button.setOnClickListener(listener);
}
button.setOnLongClickListener(new OnLongClickListener() {
@Override
public boolean onLongClick(View view) {
final int screenWidth = view.getResources().getDisplayMetrics().widthPixels;
final int screenHeight = view.getResources().getDisplayMetrics().heightPixels;
final int[] screenPos = new int[2];
view.getLocationOnScreen(screenPos);
final int width = view.getWidth();
Toast toast = Toast.makeText(
view.getContext(), view.getContentDescription(), Toast.LENGTH_SHORT);
toast.setGravity(Gravity.BOTTOM | Gravity.END,
screenWidth - screenPos[0] - width / 2,
screenHeight - screenPos[1]);
toast.show();
return true;
}
});
return button;
}
/**
* Builds an {@link ImageButton} from the data in this params. Generated buttons should be
* placed on the bottom bar. The button's tag will be its id.
* @param parent The parent that the inflated {@link ImageButton}.
* @param listener {@link OnClickListener} that should be used with the button.
* @return Parsed list of {@link CustomButtonParams}, which is empty if the input is invalid.
*/
ImageButton buildBottomBarButton(Context context, ViewGroup parent, OnClickListener listener) {
if (mIsOnToolbar) return null;
ImageButton button = (ImageButton) LayoutInflater.from(context)
.inflate(R.layout.custom_tabs_bottombar_item, parent, false);
button.setId(mId);
button.setImageBitmap(mIcon);
button.setContentDescription(mDescription);
if (mPendingIntent == null) {
button.setEnabled(false);
} else {
button.setOnClickListener(listener);
}
button.setOnLongClickListener(new OnLongClickListener() {
@Override
public boolean onLongClick(View view) {
final int screenWidth = view.getResources().getDisplayMetrics().widthPixels;
final int screenHeight = view.getResources().getDisplayMetrics().heightPixels;
final int[] screenPos = new int[2];
view.getLocationOnScreen(screenPos);
final int width = view.getWidth();
Toast toast = Toast.makeText(
view.getContext(), view.getContentDescription(), Toast.LENGTH_SHORT);
toast.setGravity(Gravity.BOTTOM | Gravity.END,
screenWidth - screenPos[0] - width / 2,
screenHeight - screenPos[1]);
toast.show();
return true;
}
});
return button;
}
/**
* Creates a close button that can be inserted into an infobar.
* @param context Context to grab resources from.
* @return {@link ImageButton} that represents a close button.
*/
static ImageButton createCloseButton(Context context) {
TypedArray a = context.obtainStyledAttributes(new int[] {R.attr.selectableItemBackground});
Drawable closeButtonBackground = a.getDrawable(0);
a.recycle();
ImageButton closeButton = new ImageButton(context);
closeButton.setId(R.id.infobar_close_button);
closeButton.setImageResource(R.drawable.btn_close);
closeButton.setBackground(closeButtonBackground);
closeButton.setContentDescription(context.getString(R.string.infobar_close));
closeButton.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
return closeButton;
}
/**
* Creates a button to put in the set of action buttons at the right of the top chrome.
* @param activity The activity that contains the video player.
* @param icon The image of the action (ex. trash can).
* @param contentDescription The text description this action. This is used in case the
* action buttons do not fit in the video player. If so, an overflow
* button will appear and, when clicked, it will display a list of the
* content descriptions for each action.
* @param onClickListener The handler for when the action is triggered.
*/
public void addActionButton(Activity activity,
Drawable icon,
String contentDescription,
View.OnClickListener onClickListener) {
ImageButton button = new ImageButton(activity);
button.setContentDescription(contentDescription);
button.setImageDrawable(icon);
button.setOnClickListener(onClickListener);
FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT
);
int margin = activity.getResources().getDisplayMetrics().densityDpi * 5;
layoutParams.setMargins(margin, 0, margin, 0);
button.setBackgroundColor(Color.TRANSPARENT);
button.setLayoutParams(layoutParams);
isFullscreen = false;
actionButtons.add(button);
if (playbackControlRootView != null) {
updateActionButtons();
updateColors();
}
}
private void setupImageButtonListeners(ImageButton... imageButtons) {
for(ImageButton b : imageButtons) {
if(b != null) {
Button info = Buttons.getButton(b.getId(), Preferences.get(b.getContext()));
b.setImageResource(info.getIconId());
b.setContentDescription(b.getContext().getString(info.getContentDescriptionId()));
b.setOnClickListener(this);
b.setOnLongClickListener(this);
}
}
}
public static View inflatePersonView(Context context, ViewGroup parent, Person person)
{
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
ImageButton personView = (ImageButton)inflater.inflate(R.layout.button_person, parent, false);
personView.setImageDrawable(context.getResources().getDrawable(person.getIcon()));
personView.setContentDescription(person.getName());
personView.setOnClickListener(mClickPersonView);
personView.setTag(person);
return personView;
}
/**
* Initialize a panorama orientation buttons.
*/
public void initializePanoOrientationButtons(final ButtonCallback cb)
{
int resIdImages = PhotoSphereHelper.getPanoramaOrientationOptionArrayId();
int resIdDescriptions = PhotoSphereHelper.getPanoramaOrientationDescriptions();
if (resIdImages > 0)
{
TypedArray imageIds = null;
TypedArray descriptionIds = null;
try
{
mModeOptions.setMainBar(ModeOptions.BAR_PANO);
imageIds = mAppController
.getAndroidContext().getResources().obtainTypedArray(resIdImages);
descriptionIds = mAppController
.getAndroidContext().getResources().obtainTypedArray(resIdDescriptions);
mModeOptionsPano.removeAllViews();
final boolean isHorizontal =
(mModeOptionsPano.getOrientation() == LinearLayout.HORIZONTAL);
final int numImageIds = imageIds.length();
for (int index = 0; index < numImageIds; index++)
{
int i;
// if in portrait orientation (pano bar horizonal), order buttons normally
// if in landscape orientation (pano bar vertical), reverse button order
if (isHorizontal)
{
i = index;
} else
{
i = numImageIds - index - 1;
}
int imageId = imageIds.getResourceId(i, 0);
if (imageId > 0)
{
ImageButton imageButton = (ImageButton) LayoutInflater
.from(mAppController.getAndroidContext())
.inflate(R.layout.mode_options_imagebutton_template,
mModeOptionsPano, false);
imageButton.setImageResource(imageId);
imageButton.setTag(String.valueOf(i));
mModeOptionsPano.addView(imageButton);
int descriptionId = descriptionIds.getResourceId(i, 0);
if (descriptionId > 0)
{
imageButton.setContentDescription(
mAppController.getAndroidContext().getString(descriptionId));
}
}
}
mModeOptionsPano.updateListeners();
mModeOptionsPano
.setOnOptionClickListener(new RadioOptions.OnOptionClickListener()
{
@Override
public void onOptionClicked(View v)
{
if (cb != null)
{
int state = Integer.parseInt((String) v.getTag());
mSettingsManager.setValueByIndex(SettingsManager.SCOPE_GLOBAL,
Keys.KEY_CAMERA_PANO_ORIENTATION,
state);
cb.onStateChanged(state);
}
}
});
updatePanoButtons();
} finally
{
if (imageIds != null)
{
imageIds.recycle();
}
if (descriptionIds != null)
{
descriptionIds.recycle();
}
}
}
}
/**
* Updates the custom buttons on bottom bar area.
* @param params The {@link CustomButtonParams} that describes the button to update.
*/
public void updateBottomBarButtons(CustomButtonParams params) {
ImageButton button = (ImageButton) getBottomBarView().findViewById(params.getId());
button.setContentDescription(params.getDescription());
button.setImageDrawable(params.getIcon(mActivity.getResources()));
}
/**
* Constructs a layout for the specified infobar. After calling this, be sure to set the
* message, the buttons, and/or the custom content using setMessage(), setButtons(), and
* setCustomContent().
*
* @param context The context used to render.
* @param infoBarView InfoBarView that listens to events.
* @param iconResourceId ID of the icon to use for the infobar.
* @param iconBitmap Bitmap for the icon to use, if the resource ID wasn't passed through.
* @param message The message to show in the infobar.
*/
public InfoBarLayout(Context context, InfoBarView infoBarView, int iconResourceId,
Bitmap iconBitmap, CharSequence message) {
super(context);
mControlLayouts = new ArrayList<InfoBarControlLayout>();
mInfoBarView = infoBarView;
// Cache resource values.
Resources res = getResources();
mSmallIconSize = res.getDimensionPixelSize(R.dimen.infobar_small_icon_size);
mSmallIconMargin = res.getDimensionPixelSize(R.dimen.infobar_small_icon_margin);
mBigIconSize = res.getDimensionPixelSize(R.dimen.infobar_big_icon_size);
mBigIconMargin = res.getDimensionPixelSize(R.dimen.infobar_big_icon_margin);
mMarginAboveButtonGroup =
res.getDimensionPixelSize(R.dimen.infobar_margin_above_button_row);
mMarginAboveControlGroups =
res.getDimensionPixelSize(R.dimen.infobar_margin_above_control_groups);
mPadding = res.getDimensionPixelOffset(R.dimen.infobar_padding);
mMinWidth = res.getDimensionPixelSize(R.dimen.infobar_min_width);
mAccentColor = ApiCompatibilityUtils.getColor(res, R.color.infobar_accent_blue);
// Set up the close button. Apply padding so it has a big touch target.
mCloseButton = new ImageButton(context);
mCloseButton.setId(R.id.infobar_close_button);
mCloseButton.setImageResource(R.drawable.btn_close);
TypedArray a = getContext().obtainStyledAttributes(
new int [] {R.attr.selectableItemBackground});
Drawable closeButtonBackground = a.getDrawable(0);
a.recycle();
mCloseButton.setBackground(closeButtonBackground);
mCloseButton.setPadding(mPadding, mPadding, mPadding, mPadding);
mCloseButton.setOnClickListener(this);
mCloseButton.setContentDescription(res.getString(R.string.infobar_close));
mCloseButton.setLayoutParams(new LayoutParams(0, -mPadding, -mPadding, -mPadding));
// Set up the icon.
if (iconResourceId != 0 || iconBitmap != null) {
mIconView = new ImageView(context);
if (iconResourceId != 0) {
mIconView.setImageResource(iconResourceId);
} else if (iconBitmap != null) {
mIconView.setImageBitmap(iconBitmap);
}
mIconView.setLayoutParams(new LayoutParams(0, 0, mSmallIconMargin, 0));
mIconView.getLayoutParams().width = mSmallIconSize;
mIconView.getLayoutParams().height = mSmallIconSize;
mIconView.setFocusable(false);
}
// Set up the message view.
mMessageMainText = message;
mMessageLayout = new InfoBarControlLayout(context);
mMessageTextView = mMessageLayout.addMainMessage(prepareMainMessageString());
}
/**
* Updates the custom buttons on bottom bar area.
* @param params The {@link CustomButtonParams} that describes the button to update.
*/
public void updateBottomBarButtons(CustomButtonParams params) {
ImageButton button = (ImageButton) getBottomBarView().findViewById(params.getId());
button.setContentDescription(params.getDescription());
button.setImageDrawable(params.getIcon(mActivity.getResources()));
}
/**
* Constructs a layout for the specified infobar. After calling this, be sure to set the
* message, the buttons, and/or the custom content using setMessage(), setButtons(), and
* setCustomContent().
*
* @param context The context used to render.
* @param infoBarView InfoBarView that listens to events.
* @param iconResourceId ID of the icon to use for the infobar.
* @param iconBitmap Bitmap for the icon to use, if the resource ID wasn't passed through.
* @param message The message to show in the infobar.
*/
public InfoBarLayout(Context context, InfoBarView infoBarView, int iconResourceId,
Bitmap iconBitmap, CharSequence message) {
super(context);
mControlLayouts = new ArrayList<InfoBarControlLayout>();
mInfoBarView = infoBarView;
// Cache resource values.
Resources res = getResources();
mSmallIconSize = res.getDimensionPixelSize(R.dimen.infobar_small_icon_size);
mSmallIconMargin = res.getDimensionPixelSize(R.dimen.infobar_small_icon_margin);
mBigIconSize = res.getDimensionPixelSize(R.dimen.infobar_big_icon_size);
mBigIconMargin = res.getDimensionPixelSize(R.dimen.infobar_big_icon_margin);
mMarginAboveButtonGroup =
res.getDimensionPixelSize(R.dimen.infobar_margin_above_button_row);
mMarginAboveControlGroups =
res.getDimensionPixelSize(R.dimen.infobar_margin_above_control_groups);
mPadding = res.getDimensionPixelOffset(R.dimen.infobar_padding);
mMinWidth = res.getDimensionPixelSize(R.dimen.infobar_min_width);
mAccentColor = ApiCompatibilityUtils.getColor(res, R.color.infobar_accent_blue);
// Set up the close button. Apply padding so it has a big touch target.
mCloseButton = new ImageButton(context);
mCloseButton.setId(R.id.infobar_close_button);
mCloseButton.setImageResource(R.drawable.btn_close);
TypedArray a = getContext().obtainStyledAttributes(
new int [] {R.attr.selectableItemBackground});
Drawable closeButtonBackground = a.getDrawable(0);
a.recycle();
mCloseButton.setBackground(closeButtonBackground);
mCloseButton.setPadding(mPadding, mPadding, mPadding, mPadding);
mCloseButton.setOnClickListener(this);
mCloseButton.setContentDescription(res.getString(R.string.infobar_close));
mCloseButton.setLayoutParams(new LayoutParams(0, -mPadding, -mPadding, -mPadding));
// Set up the icon.
if (iconResourceId != 0 || iconBitmap != null) {
mIconView = new ImageView(context);
if (iconResourceId != 0) {
mIconView.setImageResource(iconResourceId);
} else if (iconBitmap != null) {
mIconView.setImageBitmap(iconBitmap);
}
mIconView.setLayoutParams(new LayoutParams(0, 0, mSmallIconMargin, 0));
mIconView.getLayoutParams().width = mSmallIconSize;
mIconView.getLayoutParams().height = mSmallIconSize;
mIconView.setFocusable(false);
}
// Set up the message view.
mMessageMainText = message;
mMessageLayout = new InfoBarControlLayout(context);
mMessageTextView = mMessageLayout.addMainMessage(prepareMainMessageString());
}
/**
* Updates the custom buttons on bottom bar area.
* @param params The {@link CustomButtonParams} that describes the button to update.
*/
public void updateBottomBarButtons(CustomButtonParams params) {
ImageButton button = (ImageButton) getBottomBarView().findViewById(params.getId());
button.setContentDescription(params.getDescription());
button.setImageDrawable(params.getIcon(mActivity.getResources()));
}
private void updatePlayButtonAppearance(int resource, CharSequence contentDescription) {
ImageButton butPlay = getPlayButton();
butPlay.setImageResource(resource);
butPlay.setContentDescription(contentDescription);
}