[译]Multi-Gradient shape

来源:互联网 发布:cocos2dx mac环境搭建 编辑:程序博客网 时间:2024/06/03 21:44

原文链接:https://stackoverflow.com/questions/4381033/multi-gradient-shapes

需求:
给定n个颜色,每种颜色需要按照比例设置为view的背景。

解决:

Button theButton = (Button)findViewById(R.id.thebutton);ShapeDrawable.ShaderFactory sf = new ShapeDrawable.ShaderFactory() {    @Override    public Shader resize(int width, int height) {        LinearGradient lg = new LinearGradient(0, 0, 0, theButton.getHeight(),            new int[] {                 Color.LIGHT_GREEN,                 Color.WHITE,                 Color.MID_GREEN,                 Color.DARK_GREEN }, //substitute the correct colors for these            new float[] {                0, 0.45f, 0.55f, 1 },            Shader.TileMode.REPEAT);         return lg;    }};PaintDrawable p = new PaintDrawable();p.setShape(new RectShape());p.setShaderFactory(sf);theButton.setBackground((Drawable)p);
原创粉丝点击