當前位置: 首頁>>代碼示例>>Java>>正文


Java Drawable.ConstantState方法代碼示例

本文整理匯總了Java中android.graphics.drawable.Drawable.ConstantState方法的典型用法代碼示例。如果您正苦於以下問題:Java Drawable.ConstantState方法的具體用法?Java Drawable.ConstantState怎麽用?Java Drawable.ConstantState使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在android.graphics.drawable.Drawable的用法示例。


在下文中一共展示了Drawable.ConstantState方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: getActivityIconWithCache

import android.graphics.drawable.Drawable; //導入方法依賴的package包/類
/**
 * Gets the activity or application icon for an activity.
 * Uses the local icon cache for fast repeated lookups.
 *
 * @param component Name of an activity.
 * @return A drawable, or {@code null} if neither the activity nor the application
 *         has an icon set.
 */
private Drawable getActivityIconWithCache(ComponentName component) {
    // First check the icon cache
    String componentIconKey = component.flattenToShortString();
    // Using containsKey() since we also store null values.
    if (mOutsideDrawablesCache.containsKey(componentIconKey)) {
        Drawable.ConstantState cached = mOutsideDrawablesCache.get(componentIconKey);
        return cached == null ? null : cached.newDrawable(mProviderContext.getResources());
    }
    // Then try the activity or application icon
    Drawable drawable = getActivityIcon(component);
    // Stick it in the cache so we don't do this lookup again.
    Drawable.ConstantState toCache = drawable == null ? null : drawable.getConstantState();
    mOutsideDrawablesCache.put(componentIconKey, toCache);
    return drawable;
}
 
開發者ID:treasure-lau,項目名稱:CSipSimple,代碼行數:24,代碼來源:SuggestionsAdapter.java

示例2: testCreateMultiStateDrawable

import android.graphics.drawable.Drawable; //導入方法依賴的package包/類
/**
 * Tests the {@link Coloring#createMultiStateDrawable(Drawable, Drawable, Drawable, boolean)} method.
 * <p>
 * Unfortunately some Drawable properties are not shadowed by Robolectric yet, so we can test only the basic stuff here.
 */
@Test
public final void testCreateMultiStateDrawable() {
    // noinspection deprecation - can't enforce Lollipop here
    final BitmapDrawable normal = (BitmapDrawable) mActivityContext.getResources().getDrawable(android.R.drawable.btn_star_big_on);
    assertNotNull("Normal drawable is null", normal);

    // noinspection deprecation - can't enforce Lollipop here
    final BitmapDrawable clicked = (BitmapDrawable) mActivityContext.getResources().getDrawable(android.R.drawable.btn_star_big_off);
    assertNotNull("Clicked drawable is null", clicked);

    // noinspection deprecation - can't enforce Lollipop here
    final BitmapDrawable checked = (BitmapDrawable) mActivityContext.getResources().getDrawable(android.R.drawable.star_off);
    assertNotNull("Checked drawable is null", checked);

    final StateListDrawable stateList = Coloring.createMultiStateDrawable(normal, clicked, checked, true);
    assertNotNull("Contrast state drawable is null", stateList);
    assertTrue("Contrast state drawable is not stateful", stateList.isStateful());
    final Drawable.ConstantState constantState = stateList.getConstantState();
    assertNotNull("Constant state is null", constantState);
}
 
開發者ID:milosmns,項目名稱:silly-android,代碼行數:26,代碼來源:ColoringTest.java

示例3: testReturnsNewDrawableOnGet

import android.graphics.drawable.Drawable; //導入方法依賴的package包/類
@Test
public void testReturnsNewDrawableOnGet() {
  GifDrawable expected = mock(GifDrawable.class);
  Drawable.ConstantState constantState = mock(Drawable.ConstantState.class);
  when(constantState.newDrawable()).thenReturn(expected);
  when(drawable.getConstantState()).thenReturn(constantState);

  assertEquals(expected, resource.get());

  verify(drawable).getConstantState();
  verify(constantState).newDrawable();
}
 
開發者ID:weiwenqiang,項目名稱:GitHub,代碼行數:13,代碼來源:DrawableResourceTest.java

示例4: copyDrawable

import android.graphics.drawable.Drawable; //導入方法依賴的package包/類
public static Drawable copyDrawable(final ImageView imageView) {
    Drawable result = null, drawable = imageView == null ? null : imageView.getDrawable();
    if (drawable != null) {
        Drawable.ConstantState constantState = drawable.mutate().getConstantState();
        if (constantState != null) {
            result = constantState.newDrawable();
        }
    }
    return result;
}
 
開發者ID:angcyo,項目名稱:RLibrary,代碼行數:11,代碼來源:ImagePickerImageView.java

示例5: canSafelyMutateDrawable

import android.graphics.drawable.Drawable; //導入方法依賴的package包/類
/**
 * Some drawable implementations have problems with mutation. This method returns false if
 * there is a known issue in the given drawable's implementation.
 */
public static boolean canSafelyMutateDrawable(@NonNull Drawable drawable) {
    if (Build.VERSION.SDK_INT < 15 && drawable instanceof InsetDrawable) {
        return false;
    }  else if (Build.VERSION.SDK_INT < 15 && drawable instanceof GradientDrawable) {
        // GradientDrawable has a bug pre-ICS which results in mutate() resulting
        // in loss of color
        return false;
    } else if (Build.VERSION.SDK_INT < 17 && drawable instanceof LayerDrawable) {
        return false;
    }

    if (drawable instanceof DrawableContainer) {
        // If we have a DrawableContainer, let's traverse its child array
        final Drawable.ConstantState state = drawable.getConstantState();
        if (state instanceof DrawableContainer.DrawableContainerState) {
            final DrawableContainer.DrawableContainerState containerState =
                    (DrawableContainer.DrawableContainerState) state;
            for (final Drawable child : containerState.getChildren()) {
                if (!canSafelyMutateDrawable(child)) {
                    return false;
                }
            }
        }
    } else if (drawable instanceof android.support.v4.graphics.drawable.DrawableWrapper) {
        return canSafelyMutateDrawable(
                ((android.support.v4.graphics.drawable.DrawableWrapper) drawable)
                        .getWrappedDrawable());
    } /*else if (drawable instanceof android.support.v7.graphics.drawable.DrawableWrapper) {
        return canSafelyMutateDrawable(
                ((android.support.v7.graphics.drawable.DrawableWrapper) drawable)
                        .getWrappedDrawable());
    }*/ else if (drawable instanceof ScaleDrawable) {
        return canSafelyMutateDrawable(((ScaleDrawable) drawable).getDrawable());
    }

    return true;
}
 
開發者ID:commonsguy,項目名稱:cwac-crossport,代碼行數:42,代碼來源:DrawableUtils.java

示例6: checkIconCache

import android.graphics.drawable.Drawable; //導入方法依賴的package包/類
private Drawable checkIconCache(String resourceUri) {
    Drawable.ConstantState cached = mOutsideDrawablesCache.get(resourceUri);
    if (cached == null) {
        return null;
    }
    if (DBG) Log.d(LOG_TAG, "Found icon in cache: " + resourceUri);
    return cached.newDrawable();
}
 
開發者ID:treasure-lau,項目名稱:CSipSimple,代碼行數:9,代碼來源:SuggestionsAdapter.java

示例7: icon

import android.graphics.drawable.Drawable; //導入方法依賴的package包/類
private Drawable icon(Drawable drawable) {
    if (drawable != null) {
        Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();
        @SuppressWarnings("SuspiciousNameCombination")
        Drawable resizeIcon = new BitmapDrawable(context.getResources(), Bitmap.createScaledBitmap(bitmap, leftIcon, leftIcon, true));
        Drawable.ConstantState state = resizeIcon.getConstantState();
        resizeIcon = DrawableCompat.wrap(state == null ? resizeIcon : state.newDrawable().mutate());
        return resizeIcon;
    }
    return null;
}
 
開發者ID:yangchong211,項目名稱:YCDialog,代碼行數:12,代碼來源:DialogAdapter.java

示例8: onBindViewHolder

import android.graphics.drawable.Drawable; //導入方法依賴的package包/類
@Override
protected void onBindViewHolder(final RecyclerView.ViewHolder holder, Cursor cursor) {
    if (holder instanceof CaptureViewHolder) {
        CaptureViewHolder captureViewHolder = (CaptureViewHolder) holder;
        Drawable[] drawables = captureViewHolder.mHint.getCompoundDrawables();
        TypedArray ta = holder.itemView.getContext().getTheme().obtainStyledAttributes(
                new int[]{R.attr.capture_textColor});
        int color = ta.getColor(0, 0);
        ta.recycle();

        for (int i = 0; i < drawables.length; i++) {
            Drawable drawable = drawables[i];
            if (drawable != null) {
                final Drawable.ConstantState state = drawable.getConstantState();
                if (state == null) {
                    continue;
                }

                Drawable newDrawable = state.newDrawable().mutate();
                newDrawable.setColorFilter(color, PorterDuff.Mode.SRC_IN);
                newDrawable.setBounds(drawable.getBounds());
                drawables[i] = newDrawable;
            }
        }
        captureViewHolder.mHint.setCompoundDrawables(drawables[0], drawables[1], drawables[2], drawables[3]);
    } else if (holder instanceof MediaViewHolder) {
        MediaViewHolder mediaViewHolder = (MediaViewHolder) holder;

        final Item item = Item.valueOf(cursor);
        mediaViewHolder.mMediaGrid.preBindMedia(new MediaGrid.PreBindInfo(
                getImageResize(mediaViewHolder.mMediaGrid.getContext()),
                mPlaceholder,
                mSelectionSpec.countable,
                holder
        ));
        mediaViewHolder.mMediaGrid.bindMedia(item);
        mediaViewHolder.mMediaGrid.setOnMediaGridClickListener(this);
        setCheckStatus(item, mediaViewHolder.mMediaGrid);
    }
}
 
開發者ID:zhihu,項目名稱:Matisse,代碼行數:41,代碼來源:AlbumMediaAdapter.java

示例9: setIcon

import android.graphics.drawable.Drawable; //導入方法依賴的package包/類
@Override
public void setIcon(Drawable icon) {
    if (icon != null) {
        Drawable.ConstantState state = icon.getConstantState();
        icon = DrawableCompat.wrap(state == null ? icon : state.newDrawable()).mutate();
        DrawableCompat.setTintList(icon, mIconTint);
    }
    mIcon.setImageDrawable(icon);
}
 
開發者ID:Trumeet,項目名稱:MiPushFramework,代碼行數:10,代碼來源:BottomNavigationItemView.java

示例10: colorUnknownDrawable

import android.graphics.drawable.Drawable; //導入方法依賴的package包/類
/**
 * Tries to clone and simply color-filter the drawable. Uses {@link PorterDuff.Mode#SRC_ATOP}.
 * <b>Note</b>: Use this when you don't know which drawable you have.
 *
 * @param drawable Which drawable to color
 * @param color    Which color to use
 * @return A colored drawable ready for use
 */
@NonNull
public static Drawable colorUnknownDrawable(@NonNull final Drawable drawable, @ColorInt final int color) {
    // check if this is a drawable wrapper, then do coloring by drawable wrapping
    if (drawable instanceof DrawableWrapper || drawable instanceof android.support.v7.graphics.drawable.DrawableWrapper) {
        final Drawable wrapResult = colorDrawableWrapped(drawable, color);
        if (Build.VERSION.SDK_INT == Build.VERSION_CODES.JELLY_BEAN_MR2) {
            // there is a bug for JellyBean MR2 when this won't work, so.. set the tint filter manually
            wrapResult.setColorFilter(new PorterDuffColorFilter(color, PorterDuff.Mode.SRC_ATOP));
        }
        return wrapResult;
    }

    // wrapping failed, do a plain constant state clone
    try {
        final Drawable.ConstantState state = drawable.getConstantState();
        if (state == null) {
            // well done android.
            throw new IllegalStateException("Constant state is unavailable");
        }
        final Drawable copy = drawable.getConstantState().newDrawable().mutate();
        copy.setColorFilter(color, PorterDuff.Mode.SRC_ATOP);
        return copy;
    } catch (Exception ignored) {
        return drawable;
    }
}
 
開發者ID:milosmns,項目名稱:silly-android,代碼行數:35,代碼來源:Coloring.java

示例11: canSafelyMutateDrawable

import android.graphics.drawable.Drawable; //導入方法依賴的package包/類
/**
 * Some drawable implementations have problems with mutation. This method returns false if
 * there is a known issue in the given drawable's implementation.
 */
public static boolean canSafelyMutateDrawable(@NonNull Drawable drawable) {
    if (Build.VERSION.SDK_INT < 15 && drawable instanceof InsetDrawable) {
        return false;
    }  else if (Build.VERSION.SDK_INT < 15 && drawable instanceof GradientDrawable) {
        // GradientDrawable has a bug pre-ICS which results in mutate() resulting
        // in loss of color
        return false;
    } else if (Build.VERSION.SDK_INT < 17 && drawable instanceof LayerDrawable) {
        return false;
    }

    if (drawable instanceof DrawableContainer) {
        // If we have a DrawableContainer, let's traverse it's child array
        final Drawable.ConstantState state = drawable.getConstantState();
        if (state instanceof DrawableContainer.DrawableContainerState) {
            final DrawableContainer.DrawableContainerState containerState =
                    (DrawableContainer.DrawableContainerState) state;
            for (final Drawable child : containerState.getChildren()) {
                if (!canSafelyMutateDrawable(child)) {
                    return false;
                }
            }
        }
    } else if (drawable instanceof android.support.v4.graphics.drawable.DrawableWrapper) {
        return canSafelyMutateDrawable(
                ((android.support.v4.graphics.drawable.DrawableWrapper) drawable)
                        .getWrappedDrawable());
    } else if (drawable instanceof android.support.v7.graphics.drawable.DrawableWrapper) {
        return canSafelyMutateDrawable(
                ((android.support.v7.graphics.drawable.DrawableWrapper) drawable)
                        .getWrappedDrawable());
    } else if (drawable instanceof ScaleDrawable) {
        return canSafelyMutateDrawable(((ScaleDrawable) drawable).getDrawable());
    }

    return true;
}
 
開發者ID:ximsfei,項目名稱:Android-skin-support,代碼行數:42,代碼來源:SkinCompatDrawableUtils.java

示例12: testCreateRippleDrawableSingle

import android.graphics.drawable.Drawable; //導入方法依賴的package包/類
/**
 * Tests the {@link Coloring#createRippleDrawable(int)} method.
 * <p>
 * Unfortunately {@link android.graphics.drawable.LayerDrawable}'s constructor is not shadowed by Robolectric yet.
 */
@Test
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public final void testCreateRippleDrawableSingle() {
    try {
        final RippleDrawable ripple = Coloring.createRippleDrawable(Color.GREEN);
        assertNotNull("RippleDrawable is null", ripple);
        final Drawable.ConstantState constantState = ripple.getConstantState();
        assertNotNull("Constant state is null", constantState);
    } catch (NullPointerException ignored) {
        // Robolectric shadowing error, expected in the current version
    }
}
 
開發者ID:milosmns,項目名稱:silly-android,代碼行數:18,代碼來源:ColoringTest.java

示例13: testCreateResponsiveDrawableBorderless

import android.graphics.drawable.Drawable; //導入方法依賴的package包/類
/**
 * Tests the {@link Coloring#createResponsiveDrawable(Context, int, int, int, boolean, int)} method.
 * <p>
 * Unfortunately some Drawable properties are not shadowed by Robolectric yet, so we can test only the basic stuff here.
 */
@Test
public final void testCreateResponsiveDrawableBorderless() {
    final Drawable drawable = Coloring.createResponsiveDrawable(mActivityContext, Color.WHITE, Color.GRAY, Color.GREEN, true, 20);
    assertNotNull("Responsive drawable is null", drawable);
    assertTrue("Responsive drawable is of unknown type: " + drawable.getClass().getCanonicalName(), drawable instanceof StateListDrawable || drawable
            instanceof RippleDrawable);
    final Drawable.ConstantState constantState = drawable.getConstantState();
    assertNotNull("Constant state is null", constantState);
}
 
開發者ID:milosmns,項目名稱:silly-android,代碼行數:15,代碼來源:ColoringTest.java

示例14: testCreateResponsiveDrawableBorders

import android.graphics.drawable.Drawable; //導入方法依賴的package包/類
/**
 * Tests the {@link Coloring#createResponsiveDrawable(Context, int, int, int, boolean, int, Rect)} method.
 * <p>
 * Unfortunately some Drawable properties are not shadowed by Robolectric yet, so we can test only the basic stuff here.
 */
@Test
public final void testCreateResponsiveDrawableBorders() {
    final Drawable drawable = Coloring.createResponsiveDrawable(mActivityContext, Color.WHITE, Color.GRAY, Color.GREEN, true, 20, new Rect(0, 0, 20, 20));
    assertNotNull("Responsive drawable is null", drawable);
    assertTrue("Responsive drawable is of unknown type: " + drawable.getClass().getCanonicalName(), drawable instanceof StateListDrawable || drawable
            instanceof RippleDrawable);
    final Drawable.ConstantState constantState = drawable.getConstantState();
    assertNotNull("Constant state is null", constantState);
}
 
開發者ID:milosmns,項目名稱:silly-android,代碼行數:15,代碼來源:ColoringTest.java

示例15: testCreateContrastStateDrawable

import android.graphics.drawable.Drawable; //導入方法依賴的package包/類
/**
 * Tests the {@link Coloring#createContrastStateDrawable(Context, int, int, boolean, Drawable)} method.
 * <p>
 * Unfortunately some Drawable properties are not shadowed by Robolectric yet, so we can test only the basic stuff here.
 */
@Test
public final void testCreateContrastStateDrawable() {
    // noinspection deprecation - can't enforce Lollipop here
    final BitmapDrawable original = (BitmapDrawable) mActivityContext.getResources().getDrawable(android.R.drawable.btn_star_big_on);
    assertNotNull("Original drawable is null", original);

    final StateListDrawable stateList = Coloring.createContrastStateDrawable(mActivityContext, Color.WHITE, Color.BLACK, true, original);
    assertNotNull("Contrast state drawable is null", stateList);
    assertTrue("Contrast state drawable is not stateful", stateList.isStateful());
    final Drawable.ConstantState constantState = stateList.getConstantState();
    assertNotNull("Constant state is null", constantState);
}
 
開發者ID:milosmns,項目名稱:silly-android,代碼行數:18,代碼來源:ColoringTest.java


注:本文中的android.graphics.drawable.Drawable.ConstantState方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。