如何在Linearlayout中创建半影效果?

时间:2014-01-31 05:44:35

标签: android

我正在尝试创建一个动态线性布局,如下图所示: -

enter image description here

我正在使用以下代码来实现它: -

package com.example.buttondesigns;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
import android.graphics.drawable.GradientDrawable.Orientation;
import android.graphics.drawable.LayerDrawable;
import android.view.Gravity;
import android.widget.LinearLayout;

public class CardLayout extends LinearLayout
{

    public CardLayout(Context context, String color) {
        super(context);
        init(color);
    }

    private void init(String color) {

        setGravity(Gravity.CENTER);
        LayerDrawable backgroundDrawable = setLayerShadow(color);
        setBackground(backgroundDrawable);
        setPadding(15, 6, 15, 6);

        LayoutParams params = new LayoutParams(new LayoutParams(
                700,
                500));
        params.setMargins(75, 75, 5, 5);
        setLayoutParams(params);

    }

    private LayerDrawable setLayerShadow(String color) {
        GradientDrawable shadow;
        try{
            int[] colors1 = {Color.parseColor(color), Color.parseColor("#FFFFFF")};
            shadow = new GradientDrawable(Orientation.TOP_BOTTOM, colors1);
            shadow.setCornerRadius(2);
        }
        catch(Exception e){
            int[] colors1 = {Color.parseColor("#419ED9"), Color.parseColor("#419ED9")};
            shadow = new GradientDrawable(Orientation.TOP_BOTTOM, colors1);
            shadow.setCornerRadius(2);
            e.printStackTrace();
        }


        int[] colors = {Color.parseColor("#D8D8D8"), Color.parseColor("#E5E3E4")};

        GradientDrawable backColor = new GradientDrawable(Orientation.BL_TR, colors);
        backColor.setCornerRadius(2);
        backColor.setStroke(5, Color.parseColor("#D8D8D8"));

        //finally c.reate a layer list and set them as background.    
        Drawable[] layers = new Drawable[2];
        layers[0] = backColor;
        layers[1] = shadow;

        LayerDrawable layerList = new LayerDrawable(layers);
        layerList.setLayerInset(0, 0, 0, 0, 0);
        layerList.setLayerInset(1, 0, 0, 5, 5);
        return layerList;     


    }
}

我创建了一个动态线性布局,其中包含动态生成的图层列表。通过这个,我实现了这个目标: -

enter image description here

我无法在布局的左上角创建半橙色阴影效果。 有关如何实现这一目标的任何建议吗?

0 个答案:

没有答案