Androidダイナミックでshape塗り色を変更

1799 ワード

仕事の中で異なる銀行カードによって異なる背景を表示する必要があります.銀行が多いため、画像を使うことはできません.Shapeを使うことを考えましたが、数が多く、shapeのXMLファイルに書くこともできません.



    

    
   

最終決定用コード方式
 public static Drawable getBgDrawableByBankNo(Context ctx, String bankNo){
       // int strokeWidth = 5; // 0dp     
        //int roundRadius=TypedValue.applyDimension(); // 10dp     
       // int strokeColor = Color.parseColor("#2E3135");//    
        int fillColor = Color.parseColor("#971417");//      
        int topLeftRadius= DisplayUtil.dip2px(ctx,10);
        int topRightRadius=topLeftRadius;
        int bottomRightRadius=0;
        int bottomLeftRadius=0;

        GradientDrawable gd = new GradientDrawable();//  drawable
        gd.setGradientType(GradientDrawable.RECTANGLE);
         gd.setColor(fillColor);
       // gd.setCornerRadius(roundRadius);
        //1、2         ,3、4     ,5、6     ,7、8     
        gd.setCornerRadii(new float[] { topLeftRadius,
                topLeftRadius, topRightRadius, topRightRadius,
                bottomRightRadius, bottomRightRadius, bottomLeftRadius,
                bottomLeftRadius });
       // gd.setStroke(strokeWidth, strokeColor);

        return gd;
    }

使用
iv.setBackgroundDrawable(xxx);