与天地兮比寿,与日月兮齐光。这篇文章主要讲述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 { 14private Paint strokPaint = new Paint(); 15private Paint gradientPaint = new Paint(); 16 17public SGTextView(Context context) { 18this(context, null); 19} 20 21public SGTextView(Context context, AttributeSet attrs) { 22super(context, attrs); 23} 24 25public void setStyle(String strokeColor, String startColor, 26String endColor, float strokewidthDp, int gradientHeighDp) { 27setStyle( 28Color.parseColor(strokeColor), 29DimensUtils.dip2px(getContext(), strokewidthDp), 30new LinearGradient(0, 0, 0, DimensUtils.dip2px(getContext(), 31gradientHeighDp), new int[] { 32Color.parseColor(startColor), 33Color.parseColor(endColor) }, null, TileMode.CLAMP)); 34} 35 36public void setStyle(int strokeColor, float strokewidth, Shader shader) { 37 38strokPaint.setAntiAlias(true); 39// 设定是否使用图像抖动处理,会使绘制出来的图片颜色更加平滑和饱满,图像更加清晰 40strokPaint.setDither(true); 41// 如果该项设置为true,则图像在动画进行中会滤掉对Bitmap图像的优化操作,加快显示 42// 速度,本设置项依赖于dither和xfermode的设置 43strokPaint.setFilterBitmap(true); 44 45strokPaint.setStrokeWidth(strokewidth); 46strokPaint.setColor(strokeColor); 47// 设置绘制时各图形的结合方式,如平滑效果等 48strokPaint.setStrokeJoin(Paint.Join.ROUND); 49// 当画笔样式为STROKE或FILL_OR_STROKE时,设置笔刷的图形样式,如圆形样式 50// Cap.ROUND,或方形样式Cap.SQUARE 51strokPaint.setStrokeCap(Paint.Cap.ROUND); 52strokPaint.setStyle(Paint.Style.STROKE); 53 54gradientPaint.setAntiAlias(true); 55gradientPaint.setDither(true); 56gradientPaint.setFilterBitmap(true); 57gradientPaint.setShader(shader); 58gradientPaint.setStrokeJoin(Paint.Join.ROUND); 59gradientPaint.setStrokeCap(Paint.Cap.ROUND); 60gradientPaint.setStyle(Paint.Style.FILL_AND_STROKE); 61 62float textsize = getTextSize(); 63strokPaint.setTextSize(textsize); 64gradientPaint.setTextSize(textsize); 65 66} 67 68public void setShadowLayer(float radius, float dx, float dy, String color) { 69strokPaint.setShadowLayer(radius, dx, dy, Color.parseColor(color)); 70} 71 72@Override 73protected void onDraw(Canvas canvas) { 74 75String text = getText().toString(); 76int width = getMeasuredWidth(); 77if (width == 0) { 78measure(0, 0); 79width = (int) (getMeasuredWidth() + strokPaint.getStrokeWidth() * 2); 80setWidth(width); 81} 82 83float y = getBaseline(); 84float x = (width - strokPaint.measureText(text)) / 2; 85 86canvas.drawText(text, x, y, strokPaint); 87canvas.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 { 6private DimensUtils() { 7} 8 9public static int px2dip(Context context, float pxValue) { 10final float scale = context.getResources().getDisplayMetrics().density; 11return (int) (pxValue / scale + 0.5f); 12} 13 14public static int dip2px(Context context, float dipValue) { 15final float scale = context.getResources().getDisplayMetrics().density; 16return (int) (dipValue * scale + 0.5f); 17} 18 19public static int px2sp(Context context, float pxValue) { 20final float fontScale = context.getResources().getDisplayMetrics().scaledDensity; 21return (int) (pxValue / fontScale + 0.5f); 22} 23 24public static int sp2px(Context context, float spValue) { 25final float fontScale = context.getResources().getDisplayMetrics().scaledDensity; 26return (int) (spValue * fontScale + 0.5f); 27} 28 29 }
下面在布局中使用它
1< com.example.tv.view.SGTextView 2android:id="@+id/tv_tishis" 3android:layout_width="wrap_content" 4android:text="换台中" 5android:layout_height="match_parent" 6android:textSize="15sp" 7android:gravity="center" 8android:visibility="gone" 9android:layout_centerHorizontal="true" />
在代码中设置颜色以及阴影
1tvMainPeogressBar = (SGTextView) findViewById(R.id.tv_tishis); 2tvMainPeogressBar.setTextSize(43); //字体大小 3tvMainPeogressBar.setText("换台中……"); //文本内容 4tvMainPeogressBar.setStyle("#ded8cd", "#7d7a74", "#a1969d", 3, 9); //渐变颜色 5tvMainPeogressBar.setShadowLayer(2, 0, 2, "#000000"); //阴影
【Android学习笔记---自定义TextView实现阴影效果】
文章图片
推荐阅读
- Android(组件大全)
- Android6.0 Home定制
- Android基础(常用布局和数据存储)
- .net平台借助第三方推送服务在推送Android消息(极光推送)
- Android Studio 开发JNI应用
- 使用极光推送(www.jpush.cn)向安卓手机推送消息服务端向客户端主送推送C#语言
- Android中动态添加tab
- Gradle sync failed: /Applications/Android Studio.app/Contents/gradle/gradle-2.14.1/lib/plugins/gradl
- 基于Android的远程视频监控系统(含源码)