Android 自定义View 开发 入门与实践3 shadowlayer 阴影 BlurMaskFilter 发光 LinearGradient

本文主要是介绍Android 自定义View 开发 入门与实践3 shadowlayer 阴影 BlurMaskFilter 发光 LinearGradient,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

7.2 setShadowLayer与阴影效果

public class MyTextView extends TextView {private Paint mPaint;private Bitmap bitmap;public MyTextView(Context context) {this(context, null);}public MyTextView(Context context, @Nullable AttributeSet attrs) {this(context, attrs, 0);}public MyTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr);mPaint = new Paint();mPaint.setAntiAlias(true);mPaint.setDither(true);mPaint.setStyle(Paint.Style.STROKE);mPaint.setTextSize(ConvertUtils.sp2px(22));//radius : 模糊半径,参数越大,越模糊,越小,越清晰,为0则阴影消失//dx : 阴影的横向偏移距离 ,正值向右偏移,负值向左偏移//dy : 阴影的纵向偏移距离 ,正值向下偏移,负值向上偏移//shadowColor :绘制阴影的画笔颜色,对图片阴影无效mPaint.setShadowLayer(1, 10, 10, Color.RED);bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.dog);}@SuppressLint("DrawAllocation")@Overrideprotected void onDraw(Canvas canvas) {super.onDraw(canvas);canvas.drawColor(Color.WHITE);canvas.drawText("文字阴影", 100, 100, mPaint);canvas.drawCircle(300, 200, 50, mPaint);Rect rect = new Rect(300, 300, bitmap.getWidth(), bitmap.getHeight());canvas.drawBitmap(bitmap, null, rect, mPaint);}}

清除阴影

@SuppressLint("AppCompatCustomView")
public class MyTextView extends TextView {private Paint mPaint;private Bitmap bitmap;// 是否绘制阴影private boolean isShowShadow;public void setShowShadow(boolean showShadow) {isShowShadow = showShadow;postInvalidate();}public MyTextView(Context context) {this(context, null);}public MyTextView(Context context, @Nullable AttributeSet attrs) {this(context, attrs, 0);}public MyTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr);mPaint = new Paint();mPaint.setAntiAlias(true);mPaint.setDither(true);mPaint.setStyle(Paint.Style.STROKE);mPaint.setTextSize(ConvertUtils.sp2px(22));//radius : 模糊半径,参数越大,越模糊,越小,越清晰,为0则阴影消失//dx : 阴影的横向偏移距离 ,正值向右偏移,负值向左偏移//dy : 阴影的纵向偏移距离 ,正值向下偏移,负值向上偏移//shadowColor :绘制阴影的画笔颜色,对图片阴影无效
//        mPaint.setShadowLayer(1, 10, 10, Color.RED);bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.dog);}@SuppressLint("DrawAllocation")@Overrideprotected void onDraw(Canvas canvas) {super.onDraw(canvas);if (isShowShadow) {mPaint.setShadowLayer(1, 10, 10, Color.RED);} else {mPaint.clearShadowLayer();}canvas.drawColor(Color.WHITE);canvas.drawText("文字阴影", 100, 100, mPaint);canvas.drawCircle(300, 200, 50, mPaint);Rect rect = new Rect(300, 300, bitmap.getWidth(), bitmap.getHeight());canvas.drawBitmap(bitmap, null, rect, mPaint);}}
public class MainActivity extends BaseActivity<ActivityMainBinding> {private boolean isShow = false;@Overrideprotected int getLayoutId() {return R.layout.activity_main;}@Overrideprotected void getBundleExtras(Bundle extras) {}@Overrideprotected void initView() {mViewBinding.tv.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {mViewBinding.myview.setShowShadow(isShow);isShow = !isShow;}});}@Overrideprotected void initData() {}@Overrideprotected void initListener() {}}

7.2.3给文字添加阴影

可以直接在XML 里设置

<TextViewandroid:id="@+id/tv"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="过江千尺浪"android:textSize="30sp"android:shadowColor="@color/black"android:shadowDx="10"android:shadowDy="10"android:shadowRadius="10"/>

代码

mViewBinding.tv.setShadowLayer(10, 10, 10, Color.BLACK);

这个是textview 的set 方法里有这个参数


7.3BlurMaskFilter 发光效果与图片阴影

定义:发光效果只能算是高斯模糊效果,只会影响边缘效果,不会影响内部

        发光颜色不能指定,边缘啥颜色就是就是啥颜色

@SuppressLint("AppCompatCustomView")
public class MyTextView extends TextView {private Paint mPaint;public MyTextView(Context context) {this(context, null);}public MyTextView(Context context, @Nullable AttributeSet attrs) {this(context, attrs, 0);}public MyTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr);mPaint = new Paint();mPaint.setAntiAlias(true);mPaint.setDither(true);mPaint.setStyle(Paint.Style.FILL);mPaint.setColor(Color.BLACK);}@SuppressLint("DrawAllocation")@Overrideprotected void onDraw(Canvas canvas) {super.onDraw(canvas);//设置发光//radius用来定义模糊半径//style 发光样式//      BlurMaskFilter.Blur.INNER  :内发光//      BlurMaskFilter.Blur.SOLID  :外发光//      BlurMaskFilter.Blur.NORMAL :内外发光//      BlurMaskFilter.Blur.OUTER  :仅显示发光效果float radius = 50;mPaint.setMaskFilter(new BlurMaskFilter(radius, BlurMaskFilter.Blur.INNER));canvas.drawCircle(200, 200, 100, mPaint);mPaint.setMaskFilter(new BlurMaskFilter(radius, BlurMaskFilter.Blur.SOLID));canvas.drawCircle(420, 200, 100, mPaint);mPaint.setMaskFilter(new BlurMaskFilter(radius, BlurMaskFilter.Blur.NORMAL));canvas.drawCircle(620, 200, 100, mPaint);mPaint.setMaskFilter(new BlurMaskFilter(radius, BlurMaskFilter.Blur.OUTER));canvas.drawCircle(820, 200, 100, mPaint);}}

7.3.2给图片添加纯色阴影

@SuppressLint("AppCompatCustomView")
public class MyTextView extends TextView {private Paint mPaint;private Bitmap bitmap;private Bitmap alphaBitmap;public MyTextView(Context context) {this(context, null);}public MyTextView(Context context, @Nullable AttributeSet attrs) {this(context, attrs, 0);}public MyTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr);setLayerType(LAYER_TYPE_SOFTWARE, null);mPaint = new Paint();//        mPaint.setAntiAlias(true);
//        mPaint.setDither(true);
//        mPaint.setStyle(Paint.Style.FILL);
//        mPaint.setColor(Color.BLACK);bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.bikabika);alphaBitmap = this.bitmap.extractAlpha();}@SuppressLint("DrawAllocation")@Overrideprotected void onDraw(Canvas canvas) {super.onDraw(canvas);int width = bitmap.getWidth();int height = width * alphaBitmap.getWidth() / alphaBitmap.getHeight();//绘制灰色阴影mPaint.setColor(Color.GRAY);canvas.drawBitmap(alphaBitmap, null, new Rect(10, 10, width, height), mPaint);//绘制黑色阴影canvas.translate(0, height);mPaint.setColor(Color.BLACK);canvas.drawBitmap(alphaBitmap, null, new Rect(10, 10, width, height), mPaint);//黑色再加上发光canvas.translate(0, height);mPaint.setMaskFilter(new BlurMaskFilter(50, BlurMaskFilter.Blur.SOLID));canvas.drawBitmap(alphaBitmap, null, new Rect(10, 10, width, height), mPaint);}}

给图片画上影子的效果

public class MyTextView extends TextView {private Paint mPaint;private Bitmap bitmap;private Bitmap alphaBitmap;public MyTextView(Context context) {this(context, null);}public MyTextView(Context context, @Nullable AttributeSet attrs) {this(context, attrs, 0);}public MyTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr);setLayerType(LAYER_TYPE_SOFTWARE, null);mPaint = new Paint();//        mPaint.setAntiAlias(true);
//        mPaint.setDither(true);
//        mPaint.setStyle(Paint.Style.FILL);
//        mPaint.setColor(Color.BLACK);bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.bikabika);alphaBitmap = this.bitmap.extractAlpha();}@SuppressLint("DrawAllocation")@Overrideprotected void onDraw(Canvas canvas) {super.onDraw(canvas);int width = bitmap.getWidth();int height = width * alphaBitmap.getWidth() / alphaBitmap.getHeight();//绘制阴影mPaint.setColor(Color.GRAY);mPaint.setMaskFilter(new BlurMaskFilter(10, BlurMaskFilter.Blur.NORMAL));canvas.drawBitmap(alphaBitmap, null, new RectF(20, 20, width, height), mPaint);//绘制原图像  也就是盖在阴影上 向左偏移一点canvas.translate(-10, -10);mPaint.setMaskFilter(null);canvas.drawBitmap(bitmap, null, new Rect(20, 20 , width, height), mPaint);}}

7.4Shader 与 BitmapShader

着色器

TileMode

  Clamp:用边缘颜色来填充多余空间

  Repeat:重复原图像来填充多余空间

  Mirror:重复使用镜像模式的图像 来填充多余空间

public class MyTextView extends TextView {private Paint mPaint;private Bitmap bitmap;public MyTextView(Context context) {this(context, null);}public MyTextView(Context context, @Nullable AttributeSet attrs) {this(context, attrs, 0);}public MyTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr);setLayerType(LAYER_TYPE_SOFTWARE, null);mPaint = new Paint();bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.bikabika);mPaint.setShader(new BitmapShader(bitmap, Shader.TileMode.REPEAT, Shader.TileMode.REPEAT));}@SuppressLint("DrawAllocation")@Overrideprotected void onDraw(Canvas canvas) {super.onDraw(canvas);//canvas.drawRect(0, 0, getWidth(), getHeight(), mPaint);}}

Repeat:

CLAMP

MIRROR

 

Repeat + Mirror 结合

如果可以固定他的宽高那就好了

需要拿原图尺寸进行设置

canvas.drawRect(0, 0, bitmap.getWidth(), bitmap.getHeight()*2, mPaint);

MIRROR+Repeat
canvas.drawRect(0, 0, bitmap.getWidth()*2, bitmap.getHeight(), mPaint);


只画图片的一部分

@SuppressLint("DrawAllocation")@Overrideprotected void onDraw(Canvas canvas) {super.onDraw(canvas);int width = bitmap.getWidth();int height = bitmap.getHeight();int left = width / 3;int top = height / 3;int right = width * 2 / 3;int bottom = height * 2 / 3;//使用原图尺寸canvas.drawRect(left, top, right, bottom, mPaint);}

望远镜效果:

因为书上和百度各种效果都是重复的所以,我设置了图片只有一个

xml 设置 wrap

public class MyTextView extends TextView {private Paint mPaint;private Bitmap bitmap;private int mDx;private int mDy;private Bitmap mBitmapBg;public MyTextView(Context context) {this(context, null);}public MyTextView(Context context, @Nullable AttributeSet attrs) {this(context, attrs, 0);}public MyTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr);setLayerType(LAYER_TYPE_SOFTWARE, null);mPaint = new Paint();bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.dog);}@Overrideprotected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {setMeasuredDimension(bitmap.getWidth(),bitmap.getHeight());}@SuppressLint("DrawAllocation")@Overrideprotected void onDraw(Canvas canvas) {super.onDraw(canvas);if (mBitmapBg == null) {//背景  也就是原图mBitmapBg = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.ARGB_8888);//第二画笔Canvas canvasBg = new Canvas(mBitmapBg);canvasBg.drawBitmap(mBitmapBg, null, new RectF(0, 0, getWidth(), getHeight()), mPaint);}if (mDx != -1 && mDy != -1) {//利用上一次的shader 阴影花一部分来实现望远镜的效果mPaint.setShader(new BitmapShader(bitmap, Shader.TileMode.REPEAT, Shader.TileMode.REPEAT));canvas.drawCircle(mDx, mDy, 150, mPaint);}}@Overridepublic boolean onTouchEvent(MotionEvent event) {int action = event.getAction();switch (action) {case MotionEvent.ACTION_DOWN:mDx = (int) event.getX();mDy = (int) event.getY();postInvalidate();return true;case MotionEvent.ACTION_MOVE:mDx = (int) event.getX();mDy = (int) event.getY();break;case MotionEvent.ACTION_CANCEL:case MotionEvent.ACTION_UP:mDx = -1;mDy = -1;break;default:}postInvalidate();return super.onTouchEvent(event);}
}

看到后边才知道有方法

xml随便写

@SuppressLint("AppCompatCustomView")
public class MyTextView extends TextView {private Paint mPaint;private Bitmap bitmap;private int mDx;private int mDy;private Bitmap mBitmapBg;public MyTextView(Context context) {this(context, null);}public MyTextView(Context context, @Nullable AttributeSet attrs) {this(context, attrs, 0);}public MyTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr);setLayerType(LAYER_TYPE_SOFTWARE, null);mPaint = new Paint();bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.dog);}//    @Override
//    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
//        setMeasuredDimension(bitmap.getWidth(),bitmap.getHeight());
//    }@SuppressLint("DrawAllocation")@Overrideprotected void onDraw(Canvas canvas) {super.onDraw(canvas);if (mBitmapBg == null) {//背景  也就是原图mBitmapBg = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.ARGB_8888);//第二画笔Canvas canvasBg = new Canvas(mBitmapBg);canvasBg.drawBitmap(mBitmapBg, null, new RectF(0, 0, getWidth(), getHeight()), mPaint);}if (mDx != -1 && mDy != -1) {//利用上一次的shader 阴影花一部分来实现望远镜的效果Matrix matrix = new Matrix();float scaleW = (float) getWidth() / bitmap.getWidth();float scaleH = (float) getHeight() / bitmap.getHeight();matrix.setScale(scaleW,scaleH);BitmapShader bitmapShader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);bitmapShader.setLocalMatrix(matrix);mPaint.setShader(bitmapShader);canvas.drawCircle(mDx, mDy, 150, mPaint);}}@Overridepublic boolean onTouchEvent(MotionEvent event) {int action = event.getAction();switch (action) {case MotionEvent.ACTION_DOWN:mDx = (int) event.getX();mDy = (int) event.getY();postInvalidate();return true;case MotionEvent.ACTION_MOVE:mDx = (int) event.getX();mDy = (int) event.getY();break;case MotionEvent.ACTION_CANCEL:case MotionEvent.ACTION_UP:mDx = -1;mDy = -1;break;default:}postInvalidate();return super.onTouchEvent(event);}
}

不规则头像

圆形圆角带框

 @Overrideprotected void onDraw(Canvas canvas) {super.onDraw(canvas);Matrix matrix = new Matrix();float scaleWidth = (float) getWidth() / (bitmap.getWidth());float scaleHeight = (float) getHeight() / bitmap.getHeight();matrix.setScale(scaleWidth, scaleHeight);bitmapShader.setLocalMatrix(matrix);mPaint.setShader(bitmapShader);
//        int half = getWidth() / 2;
//        canvas.drawCircle(half, half, half, mPaint);//        RectF rect = new RectF(30, 30, getWidth() - 30, getHeight() - 30);
//        canvas.drawRoundRect(rect, 30, 30, mPaint);//需要边框就加上这个
//        Paint borderpaint = new Paint();
//        borderpaint.setStyle(Paint.Style.STROKE);
//        borderpaint.setStrokeWidth(5);
//        borderpaint.setColor(Color.BLACK);
//
//        canvas.drawRoundRect(rect, 30, 30, borderpaint);}

我想加个网络请求的来着,后来做不出来,就是我们现在使用的Bitmap 可以在View:Extends ImageView 的时候,通过getDrawable()获得,需要先判空,然后在转成bitmap 拿着用就行了,总之是做不出来,既然是网络传的图片了,那就用框架把,glide还有结合的Transform


7.5.Shader 之LinearGradient

float x0  y0 起始渐变坐标

float x1  y1 结束渐变坐标

colors[]  用于指定渐变的颜色值数组

positions[] 取值0-1 float 表示每种颜色在整条渐变线中的百分比位置

最后一个填充模式 和 上边一样

public class LinearGradientView extends View {private Paint mPaint;public LinearGradientView(Context context) {this(context, null);}public LinearGradientView(Context context, @Nullable AttributeSet attrs) {this(context, attrs, 0);}public LinearGradientView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr);setLayerType(LAYER_TYPE_SOFTWARE, null);mPaint = new Paint();}@Overrideprotected 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, 0xffffff00, 0xffffffff, 0xff00ffff};float[] pos = {0.0f, 0.25f, 0.5f, 0.75f, 1.0f};mPaint.setShader(new LinearGradient(0, getHeight() / 2,getWidth(), getHeight() / 2,colors, pos,Shader.TileMode.CLAMP));canvas.drawRect(0, 0, getWidth(), getHeight(), mPaint);

如果要斜着那就改起始点和终点

写文字

@Overrideprotected void onDraw(Canvas canvas) {super.onDraw(canvas);int[] colors = {0xffff0000, 0xff00ff00, 0xffffff00, 0xffffffff, 0xff00ffff};float[] pos = {0.0f, 0.25f, 0.5f, 0.75f, 1.0f};mPaint.setShader(new LinearGradient(0, getHeight() / 2,getWidth(), getHeight() / 2,colors, pos,Shader.TileMode.CLAMP));mPaint.setTextSize(ConvertUtils.sp2px(24));Paint.FontMetrics fm = mPaint.getFontMetrics();//文字基准线的下部距离-文字基准线的上部距离 = 文字高度float height= fm.descent - fm.ascent;canvas.drawText("张无敌嘤嘤嘤", 0, height, mPaint);}

从上到下

int[] colors = {0xffff0000, 0xff00ff00, 0xffffff00, 0xffffffff, 0xff00ffff};float[] pos = {0.0f, 0.25f, 0.5f, 0.75f, 1.0f};mPaint.setTextSize(ConvertUtils.sp2px(24));mPaint.setShader(new LinearGradient(getWidth() / 2, 0,getWidth() / 2, mPaint.descent() - mPaint.ascent(),colors, pos,Shader.TileMode.CLAMP));canvas.drawText("张无敌嘤嘤嘤", 0, mPaint.descent() - mPaint.ascent(), mPaint);

 这俩种都有填充模式

public class LinearGradientView extends View {private Paint mPaint;public LinearGradientView(Context context) {this(context, null);}public LinearGradientView(Context context, @Nullable AttributeSet attrs) {this(context, attrs, 0);}public LinearGradientView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr);setLayerType(LAYER_TYPE_SOFTWARE, null);mPaint = new Paint();}@Overrideprotected void onDraw(Canvas canvas) {super.onDraw(canvas);int[] colors = {0xffff0000, 0xff00ff00, 0xffffff00, 0xffffffff, 0xff00ffff};float[] pos = {0.0f, 0.25f, 0.5f, 0.75f, 1.0f};mPaint.setTextSize(ConvertUtils.sp2px(24));Rect minRect = new Rect();//通过这个方法得到的位置在上方 需要重新设置YmPaint.getTextBounds("张无敌嘤嘤嘤", 0, "张无敌嘤嘤嘤".length(), minRect);int baseLineY = minRect.bottom - minRect.top;int minTop = minRect.top + baseLineY;int minBottom = minRect.bottom + baseLineY;mPaint.setShader(new LinearGradient(getWidth() / 2, minTop,getWidth() / 2, minBottom,colors, pos,Shader.TileMode.REPEAT));canvas.drawText("张无敌嘤嘤嘤", 0, baseLineY, mPaint);}
}

这个可能有点问题,专门搞了一篇新的,粘的别人的


闪光文字效果

@SuppressLint("AppCompatCustomView")
public class ShimmerTextView extends TextView {private Paint mPaint;private int mLength;/*** 位移的变化*/private int mDx;private LinearGradient mLinearGradient;public ShimmerTextView(Context context) {this(context, null);}public ShimmerTextView(Context context, @Nullable AttributeSet attrs) {this(context, attrs, 0);}public ShimmerTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr);mPaint = getPaint();
//        mPaint.setAntiAlias(true);
//        mPaint.setDither(true);mLength = (int) mPaint.measureText(getText().toString());createAnim(mLength);creatLinearGradient(mLength);}/*** 着色** @param mLength*/private void creatLinearGradient(int mLength) {// 需要注意的  : 开始的颜色 和结束的颜色 最好是一个颜色int[] colors = {0xffff0000, 0xff00ff00, 0xffffff00, 0xffffffff, 0xffff0000};float[] pos = {0.0f, 0.25f, 0.5f, 0.75f, 1.0f};mLinearGradient = new LinearGradient(-mLength, 0, 0, 0, colors, pos, Shader.TileMode.CLAMP);}/*** 绘制路径 文字长度 动画** @param mLength*/private void createAnim(int mLength) {ValueAnimator mValueAnimator = ValueAnimator.ofInt(0, mLength * 2);mValueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {@Overridepublic void onAnimationUpdate(ValueAnimator animation) {mDx = (int) animation.getAnimatedValue();postInvalidate();}});mValueAnimator.setRepeatMode(ValueAnimator.RESTART);mValueAnimator.setRepeatCount(ValueAnimator.INFINITE);mValueAnimator.setDuration(2000);mValueAnimator.start();}@Overrideprotected void onDraw(Canvas canvas) {Matrix mMatrix = new Matrix();mMatrix.setTranslate(mDx, 0);mLinearGradient.setLocalMatrix(mMatrix);mPaint.setShader(mLinearGradient);super.onDraw(canvas);}
}

双色渐变

public class RadialGradientView extends View {private Paint mPaint;private RadialGradient mRadialGradient;public RadialGradientView(Context context) {this(context, null);}public RadialGradientView(Context context, @Nullable AttributeSet attrs) {this(context, attrs, 0);}public RadialGradientView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr);setLayerType(LAYER_TYPE_SOFTWARE, null);mPaint = new Paint();}@SuppressLint("DrawAllocation")@Overrideprotected void onDraw(Canvas canvas) {super.onDraw(canvas);if (mRadialGradient == null) {int mRadius = getWidth() / 2;int[] colors = {0xff000000, 0xff00ff00, 0xffffff00, 0xffffffff, 0xffff0000};float[] pos = {0.0f, 0.25f, 0.5f, 0.75f, 1.0f};mRadialGradient = new RadialGradient(getWidth() / 2,getHeight() / 2,mRadius,colors,pos,Shader.TileMode.REPEAT);mPaint.setShader(mRadialGradient);canvas.drawCircle(getWidth() / 2, getHeight() / 2, mRadius, mPaint);}}
}

 

if (mRadialGradient == null) {int mRadius = getWidth() / 2;int[] colors = {0xff000000, 0xff00ff00, 0xffffff00, 0xffffffff, 0xffff0000};float[] pos = {0.0f, 0.25f, 0.5f, 0.75f, 1.0f};mRadialGradient = new RadialGradient(getWidth() / 2,getHeight() / 2,mRadius,colors,pos,Shader.TileMode.MIRROR);mPaint.setShader(mRadialGradient);canvas.drawRect(0,0,getWidth(),getHeight(), mPaint);}

CLAMP

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

这篇关于Android 自定义View 开发 入门与实践3 shadowlayer 阴影 BlurMaskFilter 发光 LinearGradient的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/304850

相关文章

Oracle查询优化之高效实现仅查询前10条记录的方法与实践

《Oracle查询优化之高效实现仅查询前10条记录的方法与实践》:本文主要介绍Oracle查询优化之高效实现仅查询前10条记录的相关资料,包括使用ROWNUM、ROW_NUMBER()函数、FET... 目录1. 使用 ROWNUM 查询2. 使用 ROW_NUMBER() 函数3. 使用 FETCH FI

在C#中获取端口号与系统信息的高效实践

《在C#中获取端口号与系统信息的高效实践》在现代软件开发中,尤其是系统管理、运维、监控和性能优化等场景中,了解计算机硬件和网络的状态至关重要,C#作为一种广泛应用的编程语言,提供了丰富的API来帮助开... 目录引言1. 获取端口号信息1.1 获取活动的 TCP 和 UDP 连接说明:应用场景:2. 获取硬

Java内存泄漏问题的排查、优化与最佳实践

《Java内存泄漏问题的排查、优化与最佳实践》在Java开发中,内存泄漏是一个常见且令人头疼的问题,内存泄漏指的是程序在运行过程中,已经不再使用的对象没有被及时释放,从而导致内存占用不断增加,最终... 目录引言1. 什么是内存泄漏?常见的内存泄漏情况2. 如何排查 Java 中的内存泄漏?2.1 使用 J

基于Python开发电脑定时关机工具

《基于Python开发电脑定时关机工具》这篇文章主要为大家详细介绍了如何基于Python开发一个电脑定时关机工具,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录1. 简介2. 运行效果3. 相关源码1. 简介这个程序就像一个“忠实的管家”,帮你按时关掉电脑,而且全程不需要你多做

Java中的Opencv简介与开发环境部署方法

《Java中的Opencv简介与开发环境部署方法》OpenCV是一个开源的计算机视觉和图像处理库,提供了丰富的图像处理算法和工具,它支持多种图像处理和计算机视觉算法,可以用于物体识别与跟踪、图像分割与... 目录1.Opencv简介Opencv的应用2.Java使用OpenCV进行图像操作opencv安装j

Linux中Curl参数详解实践应用

《Linux中Curl参数详解实践应用》在现代网络开发和运维工作中,curl命令是一个不可或缺的工具,它是一个利用URL语法在命令行下工作的文件传输工具,支持多种协议,如HTTP、HTTPS、FTP等... 目录引言一、基础请求参数1. -X 或 --request2. -d 或 --data3. -H 或

基于Qt开发一个简单的OFD阅读器

《基于Qt开发一个简单的OFD阅读器》这篇文章主要为大家详细介绍了如何使用Qt框架开发一个功能强大且性能优异的OFD阅读器,文中的示例代码讲解详细,有需要的小伙伴可以参考一下... 目录摘要引言一、OFD文件格式解析二、文档结构解析三、页面渲染四、用户交互五、性能优化六、示例代码七、未来发展方向八、结论摘要

Docker集成CI/CD的项目实践

《Docker集成CI/CD的项目实践》本文主要介绍了Docker集成CI/CD的项目实践,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学... 目录一、引言1.1 什么是 CI/CD?1.2 docker 在 CI/CD 中的作用二、Docke

Android数据库Room的实际使用过程总结

《Android数据库Room的实际使用过程总结》这篇文章主要给大家介绍了关于Android数据库Room的实际使用过程,详细介绍了如何创建实体类、数据访问对象(DAO)和数据库抽象类,需要的朋友可以... 目录前言一、Room的基本使用1.项目配置2.创建实体类(Entity)3.创建数据访问对象(DAO

在 VSCode 中配置 C++ 开发环境的详细教程

《在VSCode中配置C++开发环境的详细教程》本文详细介绍了如何在VisualStudioCode(VSCode)中配置C++开发环境,包括安装必要的工具、配置编译器、设置调试环境等步骤,通... 目录如何在 VSCode 中配置 C++ 开发环境:详细教程1. 什么是 VSCode?2. 安装 VSCo