LinearGradient线型渐变效果
public LinearGradient(float x0, float y0, float x1, float y1, int[] colors, float[] positions, TileMode tile) { throw new RuntimeException("Stub!"); } public LinearGradient(float x0, float y0, float x1, float y1, int color0, int color1, TileMode tile) { throw new RuntimeException("Stub!"); }
x0,y0是起始 的渐变点坐标,x1 ,y1 是结束渐变点坐标,color0是起始颜色,color1是终止颜色,tile填充模式,colors是对应的颜色数值,position与渐变颜色相对应,取值是0到1 的float类型数据,表示每种颜色在整条渐变线中的百分比位置
示例代码:
package com.loaderman.customviewdemo; import android.content.Context; import android.graphics.*; import android.util.AttributeSet; import android.view.View; public class LinearGradientView extends View { private Paint mPaint; public LinearGradientView(Context context, AttributeSet attrs) { super(context, attrs); setLayerType(LAYER_TYPE_SOFTWARE, null); mPaint = new Paint(); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); /** * 双色渐变 */ // mPaint.setShader(new LinearGradient(0,getHeight()/2,getWidth(),getHeight()/2,0xffff0000,0xff00ff00, Shader.TileMode.CLAMP)); // canvas.drawRect(0,0,getWidth(),getHeight(),mPaint); /** * 多色渐变 */ int[] colors = {0xffff0000,0xff00ff00,0xff0000ff,0xffffff00,0xff00ffff}; float[] pos = {0f,0.2f,0.4f,0.6f,1.0f}; LinearGradient multiGradient = new LinearGradient(0,getHeight()/2,getWidth(),getHeight()/2,colors,pos, Shader.TileMode.CLAMP); mPaint.setShader(multiGradient); canvas.drawRect(0,0,getWidth(),getHeight(),mPaint); /** * 多色渐变文字 */ // int[] colors = {0xffff0000, 0xff00ff00, 0xff0000ff, 0xffffff00, 0xff00ffff}; // float[] pos = {0f, 0.2f, 0.4f, 0.6f, 1.0f}; // LinearGradient multiGradient = new LinearGradient(0, 0, getWidth()/2, getHeight()/2, colors, pos, Shader.TileMode.MIRROR); // mPaint.setShader(multiGradient); // mPaint.setTextSize(50); // canvas.drawText("欢迎学习", 0, getHeight()/2, mPaint); } }
package com.loaderman.customviewdemo; import android.animation.ValueAnimator; import android.content.Context; import android.graphics.*; import android.util.AttributeSet; import android.widget.TextView; public class ShimmerTextView extends TextView { private Paint mPaint; private int mDx; private LinearGradient mLinearGradient; public ShimmerTextView(Context context, AttributeSet attrs) { super(context, attrs); mPaint = getPaint(); int length = (int) mPaint.measureText(getText().toString()); createAnim(length); createLinearGradient(length); } private void createAnim(int length) { ValueAnimator animator = ValueAnimator.ofInt(0, 2 * length); animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { public void onAnimationUpdate(ValueAnimator animation) { mDx = (Integer) animation.getAnimatedValue(); postInvalidate(); } }); animator.setRepeatMode(ValueAnimator.RESTART); animator.setRepeatCount(ValueAnimator.INFINITE); animator.setDuration(2000); animator.start(); } private void createLinearGradient(int length) { mLinearGradient = new LinearGradient(-length, 0, 0, 0, new int[]{ getCurrentTextColor(), 0xff00ff00, getCurrentTextColor() }, new float[]{ 0, 0.5f, 1 }, Shader.TileMode.CLAMP ); } @Override protected void onDraw(Canvas canvas) { Matrix matrix = new Matrix(); matrix.setTranslate(mDx, 0); mLinearGradient.setLocalMatrix(matrix); mPaint.setShader(mLinearGradient); super.onDraw(canvas); } }
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_gravity="center" android:background="@android:color/white" android:gravity="center" android:orientation="vertical"> <com.loaderman.customviewdemo.LinearGradientView android:layout_width="match_parent" android:layout_height="200dp" /> <com.loaderman.customviewdemo.ShimmerTextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="嗯哈 loaderman" /> </LinearLayout>
效果:
最后,关注【码上加油站】微信公众号后,有疑惑有问题想加油的小伙伴可以码上加入社群,让我们一起码上加油吧!!!
posted on 2019-01-03 11:08 LoaderMan 阅读(1064) 评论(0) 编辑 收藏 举报