Android渲染器Shader:梯度渐变扫描渲染器SweepGradient(二)
附录文章1介绍了线性渐变渲染器。
Android的SweepGradient梯度渐变扫描,重点是在构造SweepGradient的中心点选择。我写一个例子。
package zhangphil.demo; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.SweepGradient; import android.util.AttributeSet; import android.view.View; /** * Created by Phil on 2016/7/25. */ public class SweepGradientView extends View { private Paint mPaint = null; // 梯度渐变扫描渲染 private SweepGradient mSweepGradient = null; public SweepGradientView(Context context, AttributeSet attrs) { super(context, attrs); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); mSweepGradient = new SweepGradient(this.getWidth() / 2, this.getHeight() / 2, new int[]{Color.TRANSPARENT, Color.RED, Color.TRANSPARENT, Color.YELLOW, Color.BLUE}, null); mPaint = new Paint(); mPaint.setAntiAlias(true); mPaint.setShader(mSweepGradient); canvas.drawCircle(this.getWidth() / 2, this.getHeight() / 2, 300, mPaint); } }
SweepGradientView直接像附录文章1那样在上层的activity里面new出来一个对象,然后setContentView进去即可。
代码运行结果:
附录文章:
1,《Android渲染器Shader:LinearGradient(一)》链接:http://blog.csdn.net/zhangphil/article/details/52004027