android.graphics.drawable.Drawable Java Examples

The following examples show how to use android.graphics.drawable.Drawable. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: RefreshImageView.java    From QiQuYing with Apache License 2.0 6 votes vote down vote up
private Bitmap getBitmapFromDrawable(Drawable drawable) {
    if (drawable == null) {
        return null;
    }

    if (drawable instanceof BitmapDrawable) {
        return ((BitmapDrawable) drawable).getBitmap();
    }

    try {
        Bitmap bitmap;

        if (drawable instanceof ColorDrawable) {
            bitmap = Bitmap.createBitmap(COLORDRAWABLE_DIMENSION, COLORDRAWABLE_DIMENSION, BITMAP_CONFIG);
        } else {
            bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), BITMAP_CONFIG);
        }

        Canvas canvas = new Canvas(bitmap);
        drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
        drawable.draw(canvas);
        return bitmap;
    } catch (OutOfMemoryError e) {
        return null;
    }
}
 
Example #2
Source File: CustomDrawableTextView.java    From QuickDevFramework with Apache License 2.0 6 votes vote down vote up
private void initAttrs(Context context, AttributeSet attr) {
    if (attr != null) {
        TypedArray typedArray = context.obtainStyledAttributes(attr, R.styleable.CustomDrawableTextView);
        drawableWidth = typedArray.getDimensionPixelOffset(R.styleable.CustomDrawableTextView_t_drawableWidth, 0);
        drawableHeight = typedArray.getDimensionPixelOffset(R.styleable.CustomDrawableTextView_t_drawableHeight, 0);
        drawableLeftWidth = typedArray.getDimensionPixelOffset(R.styleable.CustomDrawableTextView_t_drawableLeftWidth, 0);
        drawableLeftHeight = typedArray.getDimensionPixelOffset(R.styleable.CustomDrawableTextView_t_drawableLeftHeight, 0);
        drawableRightWidth = typedArray.getDimensionPixelOffset(R.styleable.CustomDrawableTextView_t_drawableRightWidth, 0);
        drawableRightHeight = typedArray.getDimensionPixelOffset(R.styleable.CustomDrawableTextView_t_drawableRightHeight, 0);
        drawableTopWidth = typedArray.getDimensionPixelOffset(R.styleable.CustomDrawableTextView_t_drawableTopWidth, 0);
        drawableTopHeight = typedArray.getDimensionPixelOffset(R.styleable.CustomDrawableTextView_t_drawableTopHeight, 0);
        drawableBottomWidth = typedArray.getDimensionPixelOffset(R.styleable.CustomDrawableTextView_t_drawableBottomWidth, 0);
        drawableBottomHeight = typedArray.getDimensionPixelOffset(R.styleable.CustomDrawableTextView_t_drawableBottomHeight, 0);

        typedArray.recycle();

    }

    Drawable drawables[] = getCompoundDrawables();
    Drawable left = drawables[0];
    Drawable top = drawables[1];
    Drawable right = drawables[2];
    Drawable bottom = drawables[3];
    setCompoundDrawables(left, top, right, bottom);

}
 
Example #3
Source File: DrawableCenterButton.java    From fangzhuishushenqi with Apache License 2.0 6 votes vote down vote up
@Override
protected void onDraw(Canvas canvas) {
    Drawable[] drawables = getCompoundDrawables();
    if (drawables != null) {
        Drawable drawableLeft = drawables[0];
        if (drawableLeft != null) {
            float textWidth = getPaint().measureText(getText().toString());
            int drawablePadding = getCompoundDrawablePadding();
            int drawableWidth = 0;
            drawableWidth = drawableLeft.getIntrinsicWidth();
            float bodyWidth = textWidth + drawableWidth + drawablePadding;
            canvas.translate((getWidth() - bodyWidth) / 11 * 5, 0);
        }
    }
    super.onDraw(canvas);
}
 
Example #4
Source File: TestFragment.java    From glide-support with The Unlicense 6 votes vote down vote up
@Override protected void load(Context context) throws Exception {
	int newIndex = (oldIndex + 1) % IMAGES.length;
	Glide
			.with(context)
			.load(IMAGES[newIndex])
			.fitCenter() // can be implicit, but see thumbnail
			.thumbnail(Glide
					.with(context)
					.load(IMAGES[oldIndex])
					.fitCenter() // have to be explicit here to match outer load exactly
			)
			.listener(new LoggingListener<String, GlideDrawable>() {
				@Override public boolean onResourceReady(GlideDrawable resource, String model,
						Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
					if (isFirstResource) {
						return false; // thumbnail was not shown, do as usual
					}
					return new DrawableCrossFadeFactory<Drawable>(/* customize timings here */)
							.build(false, false) // force crossFade() even if coming from memory cache
							.animate(resource, (ViewAdapter)target);
				}
			})
			.into(imageView)
	;
	oldIndex = newIndex;
}
 
Example #5
Source File: ProgressSpec.java    From litho with Apache License 2.0 6 votes vote down vote up
@OnMount
static void onMount(
    ComponentContext c,
    ProgressBar progressBar,
    @Prop(optional = true, resType = ResType.COLOR) int color,
    @FromPrepare Drawable resolvedIndeterminateDrawable) {

  if (resolvedIndeterminateDrawable != null) {
    progressBar.setIndeterminateDrawable(resolvedIndeterminateDrawable);
  }

  if (color != Color.TRANSPARENT && progressBar.getIndeterminateDrawable() != null) {
    progressBar
        .getIndeterminateDrawable()
        .mutate()
        .setColorFilter(color, PorterDuff.Mode.MULTIPLY);
  }
}
 
Example #6
Source File: BaseFragment.java    From arcusandroid with Apache License 2.0 6 votes vote down vote up
/**
 * update background color based on the current device connectivity
 */
public void updateBackground(boolean isConnected) {
    try {
        ColorMatrix cm = new ColorMatrix();
        cm.setSaturation(0f);
        final ColorFilter filter = new ColorMatrixColorFilter(cm);
        final ColorFilter filterOnline = getOnlineColorFilter();
        final View bgView = ImageManager.getWallpaperView();
        if (bgView != null) {
            final Drawable bgDrawable = bgView.getBackground();
            if (bgDrawable != null) {
                bgDrawable.setColorFilter(isConnected ? filterOnline : filter);
            }
        }
        if(this instanceof ArcusProductFragment) {
            final ArcusProductFragment fragment = (ArcusProductFragment) this;
            fragment.setEnabled(isConnected);
        }
        if(callback != null) {
            callback.backgroundUpdated();
        }
    }catch (Exception e){
        logger.error("Can't change background color filter: {}", e);
    }
}
 
Example #7
Source File: AnimatedImageSpan.java    From BigApp_Discuz_Android with Apache License 2.0 6 votes vote down vote up
/**
 * 代码跟父类代码相似,就是getCachedDrawable()替换成getDrawable(),因为前者里面的图片是WeakReference,
 * 容易被gc回收,所以这里要避免这个问题
 */
@Override
public int getSize(Paint paint, CharSequence text, int start, int end,
                   FontMetricsInt fm) {
    Drawable d = getDrawable();

    if (lineHeight > 0) {
        return (int) (d.getIntrinsicWidth() * scale);
    } else {
        Rect rect = d.getBounds();

        if (fm != null) {
            fm.ascent = -rect.bottom;
            fm.descent = 0;

            fm.top = fm.ascent;
            fm.bottom = 0;
        }
        return rect.right;

    }
}
 
Example #8
Source File: ColorPickerAdapter.java    From PhotoEditor with MIT License 6 votes vote down vote up
private void buildColorPickerView(View view, int colorCode) {
    view.setVisibility(View.VISIBLE);

    ShapeDrawable biggerCircle = new ShapeDrawable(new OvalShape());
    biggerCircle.setIntrinsicHeight(20);
    biggerCircle.setIntrinsicWidth(20);
    biggerCircle.setBounds(new Rect(0, 0, 20, 20));
    biggerCircle.getPaint().setColor(colorCode);

    ShapeDrawable smallerCircle = new ShapeDrawable(new OvalShape());
    smallerCircle.setIntrinsicHeight(5);
    smallerCircle.setIntrinsicWidth(5);
    smallerCircle.setBounds(new Rect(0, 0, 5, 5));
    smallerCircle.getPaint().setColor(Color.WHITE);
    smallerCircle.setPadding(10, 10, 10, 10);
    Drawable[] drawables = {smallerCircle, biggerCircle};

    LayerDrawable layerDrawable = new LayerDrawable(drawables);

    view.setBackgroundDrawable(layerDrawable);
}
 
Example #9
Source File: PhotoView.java    From soas with Apache License 2.0 6 votes vote down vote up
/**
 * Generate label background and foreground colors using Palette base on downloaded image colors.
 *
 * @param bitmap Download bitmap.
 */
@Override
public void onBitmapChange(Bitmap bitmap) {
    if (bitmap == null) { return; }

    Palette.generateAsync(bitmap, new Palette.PaletteAsyncListener() {
        @SuppressWarnings("deprecation")
        public void onGenerated(Palette palette) {
            Resources res = getResources();
            int photoNameColorBg = palette.getDarkMutedColor(res.getColor(R.color.list_item_photo_name_bg));
            int photoNameColorFg = palette.getLightMutedColor(res.getColor(R.color.view_photo_name_fg));

            ColorFilter photoNameColorFilter = new LightingColorFilter(photoNameColorBg, 1);
            Drawable photoNameDrawableBg = res.getDrawable(R.drawable.view_photo_name_bg);
            photoNameDrawableBg.setColorFilter(photoNameColorFilter);
            mPhotoName.setBackgroundDrawable(photoNameDrawableBg);

            mPhotoName.setTextColor(photoNameColorFg);
        }
    });
}
 
Example #10
Source File: ApplicationUtils.java    From zephyr with MIT License 5 votes vote down vote up
@Nullable
public Drawable getAppIcon(@NonNull PackageInfo packageInfo) {
    try {
        return mPackageManager.getApplicationIcon(packageInfo.applicationInfo);
    } catch (Exception e) {
        return mContext.getDrawable(R.drawable.ic_app_icon_unknown);
    }
}
 
Example #11
Source File: SkinCompatDrawableUtils.java    From Android-skin-support with MIT License 5 votes vote down vote up
/**
 * VectorDrawable has an issue on API 21 where it sometimes doesn't create its tint filter.
 * Fixed by toggling it's state to force a filter creation.
 */
private static void fixVectorDrawableTinting(final Drawable drawable) {
    final int[] originalState = drawable.getState();
    if (originalState == null || originalState.length == 0) {
        // The drawable doesn't have a state, so set it to be checked
        drawable.setState(SkinCompatThemeUtils.CHECKED_STATE_SET);
    } else {
        // Else the drawable does have a state, so clear it
        drawable.setState(SkinCompatThemeUtils.EMPTY_STATE_SET);
    }
    // Now set the original state
    drawable.setState(originalState);
}
 
Example #12
Source File: MaterialEditText.java    From XERUNG with Apache License 2.0 5 votes vote down vote up
private Bitmap[] generateIconBitmaps(Drawable drawable) {
  if (drawable == null)
    return null;
  Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
  Canvas canvas = new Canvas(bitmap);
  drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
  drawable.draw(canvas);
  return generateIconBitmaps(Bitmap.createScaledBitmap(bitmap, iconSize, iconSize, false));
}
 
Example #13
Source File: FloatingActionButton.java    From UltimateAndroid with Apache License 2.0 5 votes vote down vote up
Drawable getIconDrawable() {
  if (mIcon != 0) {
    return getResources().getDrawable(mIcon);
  } else {
    return new ColorDrawable(Color.TRANSPARENT);
  }
}
 
Example #14
Source File: BindingAdapters.java    From PainlessMusicPlayer with Apache License 2.0 5 votes vote down vote up
@BindingAdapter({"src", "tintAttr"})
public static void setSrcTintedFromAttr(
        @NonNull final ImageView imageView,
        @Nullable Drawable src,
        @AttrRes final int tintAttr) {
    if (src != null) {
        src = DrawableUtils
                .getTintedDrawableFromAttrTint(imageView.getContext(), src, tintAttr);
    }
    imageView.setImageDrawable(src);
}
 
Example #15
Source File: ImageUtil.java    From Leisure with GNU Lesser General Public License v3.0 5 votes vote down vote up
public static Bitmap getBitmap(SimpleDraweeView imageView){
    Bitmap bitmap;
    if (imageView.getDrawable() instanceof BitmapDrawable) {
        bitmap = ((BitmapDrawable) imageView.getDrawable()).getBitmap();
    } else {
        Drawable d = imageView.getDrawable();
        bitmap = Bitmap.createBitmap(d.getIntrinsicWidth(), d.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        d.draw(canvas);
    }
    return bitmap;
}
 
Example #16
Source File: ShowMaxImageView.java    From JianDan with Apache License 2.0 5 votes vote down vote up
private Bitmap drawableToBitamp(Drawable drawable) {

		if (drawable != null) {
			BitmapDrawable bd = (BitmapDrawable) drawable;
			return bd.getBitmap();
		} else {
			return null;
		}
	}
 
Example #17
Source File: ActivityNewsHolder.java    From SimpleChatView with Apache License 2.0 5 votes vote down vote up
@Override
public void bindData(Object obj, int position) {
    MyChatMsg data = (MyChatMsg) obj;
    TextView textView = (TextView) getView(R.id.tv_activity_news);
    Drawable drawableLeft = ContextCompat.getDrawable(AppUtils.getContext(), R.drawable.ic_activity);
    Drawable drawableRight = ContextCompat.getDrawable(AppUtils.getContext(), R.drawable.ic_arrow);
    if (drawableLeft != null) {
        drawableLeft.setBounds(0, 0, DensityUtils.dp2px(AppUtils.getContext(), 18), DensityUtils.dp2px(AppUtils.getContext(), 18));
    }
    if (drawableRight != null) {
        drawableRight.setBounds(0, 0, DensityUtils.dp2px(AppUtils.getContext(), 12), DensityUtils.dp2px(AppUtils.getContext(), 12));
    }
    textView.setCompoundDrawables(drawableLeft, null, drawableRight, null);
    textView.setText(data.activityNews);
}
 
Example #18
Source File: GradientTabStrip.java    From ProjectX with Apache License 2.0 5 votes vote down vote up
private void setItemBackground(GradientTabStripItem item) {
    if (mItemBackgroundId != NO_ID) {
        item.setBackgroundResource(mItemBackgroundId);
    } else {
        if (mItemBackgroundDrawable != null) {
            final Drawable.ConstantState state = mItemBackgroundDrawable.getConstantState();
            Drawable background;
            if (state != null)
                background = state.newDrawable(getResources()).mutate();
            else
                background = mItemBackgroundDrawable;
            item.setBackgroundDrawable(background);
        }
    }
}
 
Example #19
Source File: SuntimesUtils.java    From SuntimesWidget with GNU General Public License v3.0 5 votes vote down vote up
public static ImageSpan createImageSpan(ImageSpan other)
{
    Drawable drawable = null;
    if (other != null)
        drawable = other.getDrawable();

    return new ImageSpan(drawable);
}
 
Example #20
Source File: HolographicLinearLayout.java    From TurboLauncher with Apache License 2.0 5 votes vote down vote up
@Override
protected void drawableStateChanged() {
    super.drawableStateChanged();

    if (mImageView != null) {
        Drawable d = mImageView.getDrawable();
        if (d instanceof StateListDrawable) {
            StateListDrawable sld = (StateListDrawable) d;
            sld.setState(getDrawableState());
            sld.invalidateSelf();
        }
    }
}
 
Example #21
Source File: IconHintView.java    From styT with Apache License 2.0 5 votes vote down vote up
private Drawable zoomDrawable(Drawable drawable, int w, int h) {
    int width = drawable.getIntrinsicWidth();
    int height = drawable.getIntrinsicHeight();
    Bitmap oldbmp = drawableToBitmap(drawable);
    Matrix matrix = new Matrix();
    float scaleWidth = ((float) w / width);
    float scaleHeight = ((float) h / height);
    matrix.postScale(scaleWidth, scaleHeight);
    Bitmap newbmp = Bitmap.createBitmap(oldbmp, 0, 0, width, height,
            matrix, true);
    return new BitmapDrawable(null, newbmp);
}
 
Example #22
Source File: BubbleTextView.java    From LB-Launcher with Apache License 2.0 5 votes vote down vote up
@Override
protected void onAttachedToWindow() {
    super.onAttachedToWindow();

    if (mBackground != null) mBackground.setCallback(this);
    Drawable top = getCompoundDrawables()[1];

    if (top instanceof PreloadIconDrawable) {
        ((PreloadIconDrawable) top).applyTheme(getPreloaderTheme());
    }
    mSlop = ViewConfiguration.get(getContext()).getScaledTouchSlop();
}
 
Example #23
Source File: IconsHandler.java    From LaunchTime with GNU General Public License v3.0 5 votes vote down vote up
public static void applyIconTint(final Drawable app_icon, int mask_color) {
    if (Color.alpha(mask_color) > 10) {

        app_icon.mutate();

        //int avg = (Color.red(mask_color) + Color.green(mask_color) + Color.blue(mask_color) ) / 3;

        if (Color.red(mask_color)>5 && Color.red(mask_color) == Color.green(mask_color) && Color.red(mask_color) == Color.blue(mask_color) ) {
            setSaturation(app_icon, (255f-Color.red(mask_color))/255f, Color.alpha(mask_color)/255f);
        } else {
            PorterDuff.Mode mode = PorterDuff.Mode.MULTIPLY;
            app_icon.setColorFilter(mask_color, mode);
        }
    }
}
 
Example #24
Source File: CircularProgressButton.java    From UltimateAndroid with Apache License 2.0 5 votes vote down vote up
private void setIcon(int icon) {
    Drawable drawable = getResources().getDrawable(icon);
    if (drawable != null) {
        int padding = (getWidth() / 2) - (drawable.getIntrinsicWidth() / 2);
        setCompoundDrawablesWithIntrinsicBounds(icon, 0, 0, 0);
        setPadding(padding, 0, 0, 0);
    }
}
 
Example #25
Source File: AvatarWorkerTask.java    From Pix-Art-Messenger with GNU General Public License v3.0 5 votes vote down vote up
public static AvatarWorkerTask getBitmapWorkerTask(ImageView imageView) {
    if (imageView != null) {
        final Drawable drawable = imageView.getDrawable();
        if (drawable instanceof AsyncDrawable) {
            final AsyncDrawable asyncDrawable = (AsyncDrawable) drawable;
            return asyncDrawable.getAvatarWorkerTask();
        }
    }
    return null;
}
 
Example #26
Source File: BaseHeaderFooterAdapter.java    From SimpleAdapterDemo with Apache License 2.0 5 votes vote down vote up
public BaseViewHolder setCompoundDrawables(@IdRes int id,
                                           @Nullable Drawable left,
                                           @Nullable Drawable top,
                                           @Nullable Drawable right,
                                           @Nullable Drawable bottom) {
    View view = findViewById(id);
    if (view instanceof TextView)
        ((TextView) view).setCompoundDrawables(left, top, right, bottom);
    return this;
}
 
Example #27
Source File: Glide4Engine.java    From AndroidAnimationExercise with Apache License 2.0 5 votes vote down vote up
@Override
public void loadThumbnail(Context context, int resize, Drawable placeholder, ImageView imageView, Uri uri) {
    Glide.with(context)
            .asBitmap() // some .jpeg files are actually gif
            .load(uri)
            .apply(new RequestOptions()
                    .override(resize, resize)
                    .placeholder(placeholder)
                    .centerCrop())
            .into(imageView);
}
 
Example #28
Source File: iOSVolumePanel.java    From Noyze with Apache License 2.0 5 votes vote down vote up
@Override public void setBackgroundColor(int backgroundColor) {
    super.setBackgroundColor(backgroundColor);
    Drawable background = makeBackground(getResources(), backgroundColor);
    background.setBounds(0, 0, root.getWidth(), root.getHeight());
    root.setBackground(background);
    boolean dark = ColorPreference.isColorDark(backgroundColor);
    if (!has(BACKGROUND)) {
        setTextColor((dark) ? Color.WHITE : Color.BLACK);
    }
}
 
Example #29
Source File: ViewUtils.java    From px-android with MIT License 5 votes vote down vote up
public static void resetDrawableBackgroundColor(@NonNull final View view) {
    final int transparentColor = ContextCompat.getColor(view.getContext(), R.color.px_transparent);
    final Drawable background = view.getBackground();

    if (background != null) {
        background.setColorFilter(transparentColor, PorterDuff.Mode.SRC);
    }
}
 
Example #30
Source File: IconUtils.java    From FireFiles with Apache License 2.0 5 votes vote down vote up
public static Drawable loadPackageIcon(Context context, String authority, int icon) {
    if (icon != 0) {
        if (authority != null) {
            final PackageManager pm = context.getPackageManager();
            final ProviderInfo info = pm.resolveContentProvider(authority, 0);
            if (info != null) {
                return pm.getDrawable(info.packageName, icon, info.applicationInfo);
            }
        } else {
            return ContextCompat.getDrawable(context, icon);
        }
    }
    return null;
}