android.content.Context.getDrawable()方法的使用及代码示例

x33g5p2x  于2022-01-17 转载在 其他  
字(9.0k)|赞(0)|评价(0)|浏览(239)

本文整理了Java中android.content.Context.getDrawable()方法的一些代码示例,展示了Context.getDrawable()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Context.getDrawable()方法的具体详情如下:
包路径:android.content.Context
类名称:Context
方法名:getDrawable

Context.getDrawable介绍

暂无

代码示例

代码示例来源:origin: wasabeef/glide-transformations

public static Drawable getMaskDrawable(Context context, int maskId) {
  Drawable drawable;
  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
   drawable = context.getDrawable(maskId);
  } else {
   drawable = context.getResources().getDrawable(maskId);
  }

  if (drawable == null) {
   throw new IllegalArgumentException("maskId is invalid");
  }

  return drawable;
 }
}

代码示例来源:origin: gzu-liyujiang/AndroidPicker

@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public static Drawable getDrawable(Context context, @DrawableRes int drawableRes) {
  if (Build.VERSION.SDK_INT < 21) {
    //noinspection deprecation
    return context.getResources().getDrawable(drawableRes);
  } else {
    return context.getDrawable(drawableRes);
  }
}

代码示例来源:origin: alexvasilkov/GestureViews

@SuppressWarnings("deprecation")
private static Drawable getDrawable(Context context, @DrawableRes int id) {
  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    return context.getDrawable(id);
  } else {
    return context.getResources().getDrawable(id);
  }
}

代码示例来源:origin: rey5137/material

/**
 * Set the background drawable for the spinner's popup window of choices.
 *
 * @param resId Resource ID of a background drawable
 *
 * @attr ref android.R.styleable#Spinner_popupBackground
 */
public void setPopupBackgroundResource(int resId) {
  setPopupBackgroundDrawable(getContext().getDrawable(resId));
}

代码示例来源:origin: rockerhieu/emojicon

private void addTabIcon(EmojiconPage page, int index) {
  ImageButton icon = new ImageButton(getContext());
  LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.MATCH_PARENT);
  params.weight = 1;
  icon.setBackground(null);
  icon.setScaleType(ImageView.ScaleType.CENTER);
  if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
    icon.setImageDrawable(getContext().getResources().getDrawable(page.getIcon()));
  } else {
    icon.setImageDrawable(getContext().getDrawable(page.getIcon()));
  }
  mTabsContainer.addView(icon, mTabsContainer.getChildCount() - 2, params);
  mTabs[index] = icon;
  final int indexToMove = index;
  icon.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
      mViewPager.setCurrentItem(indexToMove, true);
    }
  });
}

代码示例来源:origin: termux/termux-app

mLeftSelectionHandle = (BitmapDrawable) getContext().getDrawable(R.drawable.text_select_handle_left_material);
mRightSelectionHandle = (BitmapDrawable) getContext().getDrawable(R.drawable.text_select_handle_right_material);

代码示例来源:origin: guolindev/giffun

public static Drawable getMaskDrawable(Context context, int maskId) {
  Drawable drawable;
  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
   drawable = context.getDrawable(maskId);
  } else {
   drawable = context.getResources().getDrawable(maskId);
  }

  if (drawable == null) {
   throw new IllegalArgumentException("maskId is invalid");
  }

  return drawable;
 }
}

代码示例来源:origin: julian-klode/dns66

void updateState() {
  iconView.setImageAlpha(255 * 87 / 100);
  if (stateChoices == 2) {
    switch (item.state) {
      case Configuration.Item.STATE_IGNORE:
      case Configuration.Item.STATE_DENY:
        iconView.setImageDrawable(context.getDrawable(R.drawable.ic_check_box_outline_blank_black_24dp));
        iconView.setContentDescription(context.getString(R.string.do_not_use_dns_server));
        break;
      case Configuration.Item.STATE_ALLOW:
        iconView.setImageDrawable(context.getDrawable(R.drawable.ic_check_box_black_24dp));
        iconView.setContentDescription(context.getString(R.string.use_dns_server));
        break;
    }
  } else {
    switch (item.state) {
      case Configuration.Item.STATE_IGNORE:
        iconView.setImageDrawable(context.getDrawable(R.drawable.ic_state_ignore));
        iconView.setImageAlpha(255 * 38 / 100);
        break;
      case Configuration.Item.STATE_DENY:
        iconView.setImageDrawable(context.getDrawable(R.drawable.ic_state_deny));
        break;
      case Configuration.Item.STATE_ALLOW:
        iconView.setImageDrawable(context.getDrawable(R.drawable.ic_state_allow));
        break;
    }
    iconView.setContentDescription(context.getResources().getStringArray(R.array.item_states)[item.state]);
  }
}

代码示例来源:origin: julian-klode/dns66

@Override
  public void onClick(View v) {
    if (extra.getVisibility() == View.GONE) {
      if (preferences.getBoolean("extraBarClosed:" + name, false)) {
        SharedPreferences.Editor editor = preferences.edit();
        editor.putBoolean("extraBarClosed:" + name, false);
        editor.apply();
      }
      view.announceForAccessibility(view.getContext().getString(R.string.expand_bar_expanded));
      expand.setImageDrawable(view.getContext().getDrawable(R.drawable.ic_expand_less_black_24dp));
      expand.setContentDescription(view.getContext().getString(R.string.expand_bar_toggle_close));
      extra.setVisibility(View.VISIBLE);
    } else {
      if (!preferences.getBoolean("extraBarClosed:" + name, false)) {
        SharedPreferences.Editor editor = preferences.edit();
        editor.putBoolean("extraBarClosed:" + name, true);
        editor.apply();
      }
      view.announceForAccessibility(view.getContext().getString(R.string.expand_bar_closed));
      expand.setImageDrawable(view.getContext().getDrawable(R.drawable.ic_expand_more_black_24dp));
      expand.setContentDescription(view.getContext().getString(R.string.expand_bar_toggle_expand));
      extra.setVisibility(View.GONE);
    }
  }
};

代码示例来源:origin: julian-klode/dns66

case AdVpnService.VPN_STATUS_STARTING:
case AdVpnService.VPN_STATUS_STOPPING:
  stateImage.setImageDrawable(context.getDrawable(R.drawable.ic_settings_black_24dp));
  startButton.setText(R.string.action_stop);
  break;
case AdVpnService.VPN_STATUS_STOPPED:
  stateImage.setImageDrawable(context.getDrawable(R.mipmap.app_icon_large));
  stateImage.setImageAlpha(32);
  stateImage.setImageTintList(null);
  break;
case AdVpnService.VPN_STATUS_RUNNING:
  stateImage.setImageDrawable(context.getDrawable(R.drawable.ic_verified_user_black_24dp));
  startButton.setText(R.string.action_stop);
  break;
case AdVpnService.VPN_STATUS_RECONNECTING_NETWORK_ERROR:
  stateImage.setImageDrawable(context.getDrawable(R.drawable.ic_error_black_24dp));
  startButton.setText(R.string.action_stop);
  break;

代码示例来源:origin: klinker24/Android-Blur-Launcher

public TintedDrawableSpan(Context context, int resourceId) {
  super(ALIGN_BOTTOM);
  mDrawable = context.getDrawable(resourceId);
  mOldTint = 0;
}

代码示例来源:origin: AlexMofer/SelectionView

@Override
  public Drawable getDrawable(Context context, int id) {
    return context.getDrawable(id);
  }
}

代码示例来源:origin: noties/Markwon

@NonNull
  private Drawable getDrawable(int resId) {
    final Drawable drawable;
    if (IS_L) {
      drawable = context.getDrawable(resId);
    } else {
      drawable = resources.getDrawable(resId);
    }
    //noinspection ConstantConditions
    return drawable;
  }
}

代码示例来源:origin: AlexMofer/ProjectX

@Override
public FloatingMenuItem setIcon(int icon) {
  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
    mIcon = mContext.getDrawable(icon);
  else
    mIcon = mContext.getResources().getDrawable(icon);
  return this;
}

代码示例来源:origin: huangweicai/OkLibDemo

static Drawable getDrawable(@NonNull Context context, @DrawableRes int id) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
      return context.getDrawable(id);
    else
      return context.getResources().getDrawable(id);
  }
}

代码示例来源:origin: googlesamples/android-WatchFace

public void setDefaultComplicationDrawable(int resourceId) {
  Context context = mWatchFaceArmsAndTicksView.getContext();
  mDefaultComplicationDrawable = context.getDrawable(resourceId);
  mLeftComplication.setImageDrawable(mDefaultComplicationDrawable);
  mLeftComplicationBackground.setVisibility(View.INVISIBLE);
  mRightComplication.setImageDrawable(mDefaultComplicationDrawable);
  mRightComplicationBackground.setVisibility(View.INVISIBLE);
}

代码示例来源:origin: mapsforge/mapsforge

@SuppressWarnings("deprecation")
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
static Marker createMarker(Context c, int resourceIdentifier,
              LatLong latLong) {
  Drawable drawable = Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP ? c.getDrawable(resourceIdentifier) : c.getResources().getDrawable(resourceIdentifier);
  Bitmap bitmap = AndroidGraphicFactory.convertToBitmap(drawable);
  return new Marker(latLong, bitmap, 0, -bitmap.getHeight() / 2);
}

代码示例来源:origin: googlesamples/android-WatchFace

public void setIcon(int resourceId) {
  Context context = mBackgroundComplicationButton.getContext();
  mBackgroundComplicationButton.setCompoundDrawablesWithIntrinsicBounds(
      context.getDrawable(resourceId), null, null, null);
}

代码示例来源:origin: googlesamples/android-WatchFace

private void updateIcon(Context context, Boolean currentState) {
  int currentIconResourceId;
  if (currentState) {
    currentIconResourceId = mEnabledIconResourceId;
  } else {
    currentIconResourceId = mDisabledIconResourceId;
  }
  mUnreadNotificationSwitch.setChecked(currentState);
  mUnreadNotificationSwitch.setCompoundDrawablesWithIntrinsicBounds(
      context.getDrawable(currentIconResourceId), null, null, null);
}

代码示例来源:origin: davideas/FlipView

@TargetApi(Build.VERSION_CODES.LOLLIPOP)
@SuppressWarnings("deprecation")
public void setChildBackgroundDrawable(int whichChild, @DrawableRes int drawableResId) {
  try {
    setChildBackgroundDrawable(whichChild,
        hasLollipop() ? getContext().getDrawable(drawableResId) :
            getContext().getResources().getDrawable(drawableResId));
  } catch (Resources.NotFoundException e) {
    Log.e(TAG, "Resource with id " + drawableResId + " could not be found. Drawable cannot be set!");
  }
}

相关文章

Context类方法