前一段时间用LinearGradient(线性渐变) 写了一个颜色变化闪动的文字控件和文字进度条http://blog.csdn.net/huweigoodboy/article/details/43088953,
最近在github上看到一个开源控件Titanic,特拿来分析一下,效果如下图:
private void createShader() { if (wave == null) { wave = getResources().getDrawable(R.drawable.wave); } int waveW = wave.getIntrinsicWidth(); int waveH = wave.getIntrinsicHeight(); Bitmap b = Bitmap.createBitmap(waveW, waveH, Bitmap.Config.ARGB_8888); Canvas c = new Canvas(b); c.drawColor(getCurrentTextColor()); wave.setBounds(0, 0, waveW, waveH); wave.draw(c); shader = new BitmapShader(b, Shader.TileMode.REPEAT, Shader.TileMode.CLAMP); //设置着色器的TileMode getPaint().setShader(shader); offsetY = (getHeight() - waveH) / 2; //计算偏移Y 默认放在textView的中间 }
@Override protected void onDraw(Canvas canvas) { // modify text paint shader according to sinking state if (sinking && shader != null) { // first call after sinking, assign it to our paint if (getPaint().getShader() == null) { getPaint().setShader(shader); } // translate shader accordingly to maskX maskY positions // maskY is affected by the offset to vertically center the wave shaderMatrix.setTranslate(maskX, maskY + offsetY); //平移变换,设置着色图片的坐标 ,maskX,maskY为shader的坐标变换值 // assign matrix to invalidate the shader shader.setLocalMatrix(shaderMatrix); } else { getPaint().setShader(null); } super.onDraw(canvas); }3,属性动画
// horizontal animation. 200 = wave.png width //x方向上的变化,变换范围是从0-waveW ObjectAnimator maskXAnimator = ObjectAnimator.ofFloat(textView, "maskX", 0, 200); maskXAnimator.setRepeatCount(ValueAnimator.INFINITE); //变换次数设为不限制 maskXAnimator.setDuration(1000); maskXAnimator.setStartDelay(0); int h = textView.getHeight(); // vertical animation // maskY = 0 -> wave vertically centered // repeat mode REVERSE to go back and forth //y方向是h/2 -> -h/2,折合偏移量,就是从textView底部到顶部。 ObjectAnimator maskYAnimator = ObjectAnimator.ofFloat(textView, "maskY", h/2, - h/2); maskYAnimator.setRepeatCount(ValueAnimator.INFINITE); maskYAnimator.setRepeatMode(ValueAnimator.REVERSE); maskYAnimator.setDuration(10000); maskYAnimator.setStartDelay(0);
原文地址:http://blog.csdn.net/huweigoodboy/article/details/45369123