Android StateListDrawable - top ranked examples from Open Source projects

These code examples were ranked by Codota’s semantic indexing as the best open source examples for Android StateListDrawable class.

This code example shows how to use the following methods:setBounds, addState
    } 
 
    public Drawable createIconDrawable(Drawable src) {
        Bitmap scaled = createIconBitmap(src);
 
        StateListDrawable result = new StateListDrawable();
 
        result.addState(new int[] { android.R.attr.state_focused },
                new BitmapDrawable(createSelectedBitmap(scaled, false)));
        result.addState(new int[] { android.R.attr.state_pressed },
                new BitmapDrawable(createSelectedBitmap(scaled, true)));
        result.addState(new int[0], new BitmapDrawable(scaled));
 
        result.setBounds(0, 0, mIconTextureWidth, mIconTextureHeight);
        return result;
    } 
 
    /** 
     * Returns a bitmap suitable for the all apps view.  The bitmap will be a power 
     * of two sized ARGB_8888 bitmap that can be used as a gl texture. 
8
CodeRank
This code example shows how to use the following methods:addState
            } 
        }; 
 
        ColorDrawable color = new ColorDrawable(colorValue);
 
        StateListDrawable stld = new StateListDrawable();
        stld.addState(new int[] { android.R.attr.state_pressed }, color);
        stld.addState(new int[] { android.R.attr.state_enabled }, d);
        return stld;
    } 
 
} 
8
CodeRank
This code example shows how to use the following methods:addState
    public static StateListDrawable customButton(int tl, int tr, int br, int bl, int onColor, int offColor, int borderColor, int strokeWidth) {
        Shape shape = new RoundRectShape(new float[] { tl, tl, tr, tr, br, br, bl, bl}, null, null);
        ShapeDrawable sdOn = new CustomBorderDrawable(shape, onColor, borderColor, strokeWidth);
        ShapeDrawable sdOff = new CustomBorderDrawable(shape, offColor, borderColor, strokeWidth);
 
        StateListDrawable stld = new StateListDrawable();
        stld.addState(new int[] { android.R.attr.state_pressed }, sdOn);
        stld.addState(new int[] { android.R.attr.state_checked }, sdOn);
        stld.addState(new int[] { android.R.attr.state_enabled }, sdOff);
        return stld;
    } 
} 
8
CodeRank
This code example shows how to use the following methods:addState
		// TODO Auto-generated constructor stub 
	} 
	private static Drawable newSelector(Context context){
		GradientDrawable mDrawPressed;
		GradientDrawable mDrawSelected;
		StateListDrawable drawable=new StateListDrawable();
		int selectedColor=AlmostNexusSettingsHelper.getHighlightsColorFocus(context);
		int pressedColor=AlmostNexusSettingsHelper.getHighlightsColor(context);
		int stateFocused = android.R.attr.state_focused;
		int statePressed = android.R.attr.state_pressed;
		int stateWindowFocused = android.R.attr.state_window_focused;
		  
		mDrawSelected = new GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM,
		        new int[] { 0x77FFFFFF, selectedColor,selectedColor,selectedColor,selectedColor, 0x77000000 });
		mDrawSelected.setShape(GradientDrawable.RECTANGLE);
		mDrawSelected.setGradientRadius((float)(Math.sqrt(2) * 60));
		mDrawSelected.setCornerRadius(8);
		mDrawPressed = new GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM,
				new int[] { 0x77FFFFFF, pressedColor,pressedColor,pressedColor,pressedColor, 0x77000000 });
		mDrawPressed.setShape(GradientDrawable.RECTANGLE);

7
CodeRank
This code example shows how to use the following methods:setBounds, addState
    } 
 
    public Drawable createIconDrawable(Drawable src) {
        Bitmap scaled = createIconBitmap(src);
 
        StateListDrawable result = new StateListDrawable();
 
        result.addState(new int[] { android.R.attr.state_focused },
                new BitmapDrawable(createSelectedBitmap(scaled, false)));
        result.addState(new int[] { android.R.attr.state_pressed },
                new BitmapDrawable(createSelectedBitmap(scaled, true)));
        result.addState(new int[0], new BitmapDrawable(scaled));
 
        result.setBounds(0, 0, mIconTextureWidth, mIconTextureHeight);
        return result;
    } 
 
    /** 
     * Returns a bitmap suitable for the all apps view.  The bitmap will be a power 
     * of two sized ARGB_8888 bitmap that can be used as a gl texture. 
7
CodeRank
This code example shows how to use the following methods:setBounds, draw
 
    @Override 
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (stateList != null) {
            stateList.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
            stateList.draw(canvas);
        } 
    } 
 
    /**************************/ 
    /** Checkable **/ 
    /**************************/ 
 
    @Override 
    public boolean isChecked() { 
        return mChecked;
    } 
 
    @Override 
6
CodeRank
This code example shows how to use the following methods:addState
            Bitmap original = createOriginalImage(v, mTempCanvas);
            Bitmap outline = createPressImage(v, mTempCanvas);
            FastBitmapDrawable originalD = new FastBitmapDrawable(original);
            FastBitmapDrawable outlineD = new FastBitmapDrawable(outline);
 
            StateListDrawable states = new StateListDrawable();
            states.addState(new int[] {android.R.attr.state_pressed}, outlineD);
            states.addState(new int[] {android.R.attr.state_focused}, outlineD);
            states.addState(new int[] {}, originalD);
            v.setImageDrawable(states);
        } 
    } 
 
    /** 
     * Invalidates the pressed/focused states. 
     */ 
    void invalidatePressedFocusedStates(ImageView v) {
        mStatesUpdated = false;
        if (v != null) {
            v.invalidate();
7
CodeRank
This code example shows how to use the following methods:addState
        if (!mStatesUpdated && v != null) {
            mStatesUpdated = true;
            Bitmap outline = createPressImage(v, mTempCanvas);
            FastBitmapDrawable d = new FastBitmapDrawable(outline);
 
            StateListDrawable states = new StateListDrawable();
            states.addState(new int[] {android.R.attr.state_pressed}, d);
            states.addState(new int[] {android.R.attr.state_focused}, d);
            states.addState(new int[] {}, v.getDrawable());
            v.setImageDrawable(states);
        } 
    } 
 
    /** 
     * Invalidates the pressed/focused states. 
     */ 
    void invalidatePressedFocusedStates(ImageView v) {
        mStatesUpdated = false;
        if (v != null) {
            v.invalidate();
7
CodeRank
This code example shows how to use the following methods:addState
        if(focused == null) {
            focused = pressed;
        } 
        StateListDrawable std = null;
        if(pressed != null && focused != null && normal != null) {
            std = new StateListDrawable();
            std.addState(new int[] {android.R.attr.state_pressed}, pressed);
            std.addState(new int[] {android.R.attr.state_focused}, focused);
            std.addState(new int[] {}, normal);
        } 
         
        if(std != null) {
            UtilityWrapper.getInstance().setBackgroundDrawable(v, std);
        } 
    } 
     
 
    public void applyBackgroundStateListSelectableDrawable(View v, String prefix) {
        Drawable pressed = getDrawableResource(prefix+"_press");
        Drawable focused = getDrawableResource(prefix+"_focus");
6
CodeRank
This code example shows how to use the following methods:setState
    } 
 
    public void setState(int [] state) {
        if (mDrawable instanceof StateListDrawable) {
            StateListDrawable d = (StateListDrawable) mDrawable;
            d.setState(state);
        } 
    } 
 
    public boolean hasState(int [] state) {
        if (mDrawable instanceof StateListDrawable) {
            StateListDrawable d = (StateListDrawable) mDrawable;
            // TODO: this doesn't seem to work 
            return d.getStateDrawableIndex(state) != -1;
        } 
        return false; 
    } 
 
    /** 
     * Returns true if the drawable is a StateListDrawable and is in the focused state. 
6
CodeRank

Related Android StateListDrawable Questions & Answers:

See Code Examples for Android StateListDrawable Methods: