工作中需要依据不同的银行卡显示不同背景,由于银行很多,不可能用图片,想到了用Shape,但数量多,也不可能写死在shape的XML文件中,
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" > <solid android:color="#971417" ></solid> <corners android:topLeftRadius="10dp" android:topRightRadius="10dp" android:bottomLeftRadius="0dp" android:bottomRightRadius="0dp"/> <!-- <stroke android:width="1dp" android:color="#971417" />--> </shape>
最后决定用代码方式
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);
相关推荐
android 用shape画出来的颜色做背景时,动态的改变它的颜色属性。
Android中使用Shape自定义形状
android 代码实现shape GradientDrawable
主要介绍了android shape的使用及渐变色、分割线、边框、半透明阴影,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
android-shape-imageview - 图片圆角、三角、五角、圆形、多角
主要介绍了Android实现动态改变shape.xml中图形的颜色,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
遇到个动态改变SeekBar进度条颜色与滑块颜色的需求,有的是根据不同进度改变成不同颜色。 对于这个怎么做呢?大家都知道设置下progressDrawable与thumb即可,但是这样设置好就是确定的了,要动态更改需要在代码里...
利用shape优化android UI
android shape的使用
Android控件美化之Shape的使用 里面事例非常好的。
Android shape属性详解
android中shape的使用: http://blog.csdn.net/luohai859/article/details/41038443
android shape可以帮助我们实现很多的效果,比如:圆角、虚线边框、部分圆角、颜色的渐变等效果,同时源文件给出了android4.0系统下虚线会变成实线的解决办法。
Android-shape标签的使用,让我们的界面更加高大上档次
Android Shape源码实例,通过shape可以为为视图设置圆角背景,既可以通过在xml文件中直接为视图设置背景,又可以通过在代码中动态设置。
Android shape 属性深入用法 我的博客:http://blog.csdn.net/xiaoyuan511
探索Android中selector和shape的结合使用
Android:shape的使用详解(2)http://blog.csdn.net/wangyanguiyiyang/article/details/50060829
详细信息可见博客,Android:shape开发详解-UI设计