Android学习笔记---自定义TextView实现阴影效果
Posted 纯属浪费8818
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android学习笔记---自定义TextView实现阴影效果相关的知识,希望对你有一定的参考价值。
直接上代码
SGTextView.java
1 package com.example.tv.view; 2 3 import android.content.Context; 4 import android.graphics.Canvas; 5 import android.graphics.Color; 6 import android.graphics.LinearGradient; 7 import android.graphics.Paint; 8 import android.graphics.Shader; 9 import android.graphics.Shader.TileMode; 10 import android.util.AttributeSet; 11 import android.widget.TextView; 12 13 public class SGTextView extends TextView { 14 private Paint strokPaint = new Paint(); 15 private Paint gradientPaint = new Paint(); 16 17 public SGTextView(Context context) { 18 this(context, null); 19 } 20 21 public SGTextView(Context context, AttributeSet attrs) { 22 super(context, attrs); 23 } 24 25 public void setStyle(String strokeColor, String startColor, 26 String endColor, float strokewidthDp, int gradientHeighDp) { 27 setStyle( 28 Color.parseColor(strokeColor), 29 DimensUtils.dip2px(getContext(), strokewidthDp), 30 new LinearGradient(0, 0, 0, DimensUtils.dip2px(getContext(), 31 gradientHeighDp), new int[] { 32 Color.parseColor(startColor), 33 Color.parseColor(endColor) }, null, TileMode.CLAMP)); 34 } 35 36 public void setStyle(int strokeColor, float strokewidth, Shader shader) { 37 38 strokPaint.setAntiAlias(true); 39 // 设定是否使用图像抖动处理,会使绘制出来的图片颜色更加平滑和饱满,图像更加清晰 40 strokPaint.setDither(true); 41 // 如果该项设置为true,则图像在动画进行中会滤掉对Bitmap图像的优化操作,加快显示 42 // 速度,本设置项依赖于dither和xfermode的设置 43 strokPaint.setFilterBitmap(true); 44 45 strokPaint.setStrokeWidth(strokewidth); 46 strokPaint.setColor(strokeColor); 47 // 设置绘制时各图形的结合方式,如平滑效果等 48 strokPaint.setStrokeJoin(Paint.Join.ROUND); 49 // 当画笔样式为STROKE或FILL_OR_STROKE时,设置笔刷的图形样式,如圆形样式 50 // Cap.ROUND,或方形样式Cap.SQUARE 51 strokPaint.setStrokeCap(Paint.Cap.ROUND); 52 strokPaint.setStyle(Paint.Style.STROKE); 53 54 gradientPaint.setAntiAlias(true); 55 gradientPaint.setDither(true); 56 gradientPaint.setFilterBitmap(true); 57 gradientPaint.setShader(shader); 58 gradientPaint.setStrokeJoin(Paint.Join.ROUND); 59 gradientPaint.setStrokeCap(Paint.Cap.ROUND); 60 gradientPaint.setStyle(Paint.Style.FILL_AND_STROKE); 61 62 float textsize = getTextSize(); 63 strokPaint.setTextSize(textsize); 64 gradientPaint.setTextSize(textsize); 65 66 } 67 68 public void setShadowLayer(float radius, float dx, float dy, String color) { 69 strokPaint.setShadowLayer(radius, dx, dy, Color.parseColor(color)); 70 } 71 72 @Override 73 protected void onDraw(Canvas canvas) { 74 75 String text = getText().toString(); 76 int width = getMeasuredWidth(); 77 if (width == 0) { 78 measure(0, 0); 79 width = (int) (getMeasuredWidth() + strokPaint.getStrokeWidth() * 2); 80 setWidth(width); 81 } 82 83 float y = getBaseline(); 84 float x = (width - strokPaint.measureText(text)) / 2; 85 86 canvas.drawText(text, x, y, strokPaint); 87 canvas.drawText(text, x, y, gradientPaint); 88 } 89 }
DimensUtils.java
1 package com.example.tv.view; 2 3 import android.content.Context; 4 5 public final class DimensUtils { 6 private DimensUtils() { 7 } 8 9 public static int px2dip(Context context, float pxValue) { 10 final float scale = context.getResources().getDisplayMetrics().density; 11 return (int) (pxValue / scale + 0.5f); 12 } 13 14 public static int dip2px(Context context, float dipValue) { 15 final float scale = context.getResources().getDisplayMetrics().density; 16 return (int) (dipValue * scale + 0.5f); 17 } 18 19 public static int px2sp(Context context, float pxValue) { 20 final float fontScale = context.getResources().getDisplayMetrics().scaledDensity; 21 return (int) (pxValue / fontScale + 0.5f); 22 } 23 24 public static int sp2px(Context context, float spValue) { 25 final float fontScale = context.getResources().getDisplayMetrics().scaledDensity; 26 return (int) (spValue * fontScale + 0.5f); 27 } 28 29 }
下面在布局中使用它
1 <com.example.tv.view.SGTextView 2 android:id="@+id/tv_tishis" 3 android:layout_width="wrap_content" 4 android:text="换台中" 5 android:layout_height="match_parent" 6 android:textSize="15sp" 7 android:gravity="center" 8 android:visibility="gone" 9 android:layout_centerHorizontal="true" />
在代码中设置颜色以及阴影
1 tvMainPeogressBar = (SGTextView) findViewById(R.id.tv_tishis); 2 tvMainPeogressBar.setTextSize(43);//字体大小 3 tvMainPeogressBar.setText("换台中……");//文本内容 4 tvMainPeogressBar.setStyle("#ded8cd", "#7d7a74", "#a1969d", 3, 9);//渐变颜色 5 tvMainPeogressBar.setShadowLayer(2, 0, 2, "#000000");//阴影
以上是关于Android学习笔记---自定义TextView实现阴影效果的主要内容,如果未能解决你的问题,请参考以下文章