get Layer Drawable - Android Graphics

Android examples for Graphics:Drawable Operation

Description

get Layer Drawable

Demo Code


//package com.java2s;

import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
import android.graphics.drawable.LayerDrawable;

public class Main {

    public static Drawable getLayerDrawable(int fillColor,
            int leftStrokeWidth, int topStrokeWidth, int rightStrokeWidth,
            int bottomStrokeWidth, int strokeColor) {
        return getLayerDrawable(0, 0, 0, 0, fillColor, leftStrokeWidth,
                topStrokeWidth, rightStrokeWidth, bottomStrokeWidth,
                strokeColor);//from  ww  w  .  jav a  2s .  c o  m
    }

    public static Drawable getLayerDrawable(int topLeft, int topRight,
            int bottomRight, int bottomLeft, int fillColor,
            int leftStrokeWidth, int topStrokeWidth, int rightStrokeWidth,
            int bottomStrokeWidth, int strokeColor) {
        Drawable fillDrawable = getDrawable(topLeft, topRight, bottomRight,
                bottomLeft, fillColor);//??drawable
        float[] outer = new float[] { topLeft, topLeft, topRight, topRight,
                bottomRight, bottomRight, bottomLeft, bottomLeft };
        GradientDrawable strokeDrawable = new GradientDrawable();//??drawable
        strokeDrawable.setColor(strokeColor);
        strokeDrawable.setCornerRadii(outer);
        LayerDrawable layerDrawable = new LayerDrawable(new Drawable[] {
                strokeDrawable, fillDrawable });

        layerDrawable.setLayerInset(1, leftStrokeWidth, topStrokeWidth,
                rightStrokeWidth, bottomStrokeWidth);
        return layerDrawable;
    }

    public static Drawable getLayerDrawable(int radius, int fillColor,
            int leftStrokeWidth, int topStrokeWidth, int rightStrokeWidth,
            int bottomStrokeWidth, int strokeColor) {
        return getLayerDrawable(radius, radius, radius, radius, fillColor,
                leftStrokeWidth, topStrokeWidth, rightStrokeWidth,
                bottomStrokeWidth, strokeColor);
    }

    public static Drawable getDrawable(int topLeft, int topRight,
            int bottomRight, int bottomLeft, int fillColor) {
        float[] outer = new float[] { topLeft, topLeft, topRight, topRight,
                bottomRight, bottomRight, bottomLeft, bottomLeft };
        GradientDrawable drawable = new GradientDrawable();
        drawable.setCornerRadii(outer);
        drawable.setColor(fillColor);
        return drawable;
    }


    public static Drawable getDrawable(int topLeft, int topRight,
            int bottomRight, int bottomLeft, int fillColor,
            int strokeWidth, int strokeColor) {
        GradientDrawable drawable = (GradientDrawable) getDrawable(topLeft,
                topRight, bottomRight, bottomLeft, fillColor);
        drawable.setStroke(strokeWidth, strokeColor);
        return drawable;
    }

}

Related Tutorials