自定义View之自定义文字闪动效果
来源:互联网 发布:拉普拉斯矩阵 编辑:程序博客网 时间:2024/05/20 11:49
自定义View之自定义文字闪动效果,代码是学习《Android群英会》中的例子略有改动
效果如图
直接上自定义控件的代码
package com.cheng.cc.customcontrols.views;import android.content.Context;import android.graphics.Canvas;import android.graphics.Color;import android.graphics.LinearGradient;import android.graphics.Matrix;import android.graphics.Paint;import android.graphics.Shader;import android.util.AttributeSet;import android.widget.TextView;/** * @author Created by cc on 17/6/2. * @fileName LightningTextView * @githublink https://github.com/cc0819 * @csdnlink http://blog.csdn.net/qq_25404567 */public class LightningTextView extends TextView { private int mViewWidth; private int mTranslate; private Paint mPaint; private LinearGradient mLinearGradient; private Matrix mGradientMatrix; public LightningTextView(Context context) { super(context); } public LightningTextView(Context context, AttributeSet attrs) { super(context, attrs); } public LightningTextView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } @Override protected void onSizeChanged(int w, int h, int oldw, int oldh) { super.onSizeChanged(w, h, oldw, oldh); if (mViewWidth == 0){ mViewWidth = getMeasuredWidth(); if (mViewWidth > 0){ mPaint = getPaint(); mLinearGradient = new LinearGradient(0,0,mViewWidth,0, new int[]{Color.BLUE,Color.YELLOW,Color.RED}, null, Shader.TileMode.CLAMP);//设置线性渲染的颜色 // CLAMP表示重复最后一种颜色直到该View结束的地方 // REPEAT表示着色器在水平或者垂直方向上对控件进行重复着色 // MIRROR模式会在水平方向或者垂直方向上以镜像的方式进行渲染 mPaint.setShader(mLinearGradient); mGradientMatrix = new Matrix(); } } } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); if (mGradientMatrix != null){ mTranslate += mViewWidth / 5 ;//已控件宽度的五分之一移动渲染的矩阵达到闪动效果 if (mTranslate > 2*mViewWidth){ mTranslate = -mViewWidth; } mGradientMatrix.setTranslate(mTranslate,0); mLinearGradient.setLocalMatrix(mGradientMatrix); postInvalidateDelayed(80); } }}
布局中引用
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:gravity="center" tools:context="com.cheng.cc.customcontrols.activity.TextLightning_Activity"> <com.cheng.cc.customcontrols.views.LightningTextView android:id="@+id/lightning" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center"/></LinearLayout>
文字内容和大小是在代码中动态设置的
lightning.setText("LIGHTNING TEXTVIEW");lightning.setTextSize(50);
github下载地址:https://github.com/cc0819/CustomControls
阅读全文
0 0
- 自定义View之自定义文字闪动效果
- 自定义View之TextView的闪动效果
- 自定义View之TextView的闪动效果
- 【Android】自定义View-为文字添加动态闪动效果
- Android自定义View之实现一个动态的文字闪动效果
- Android自定义View之绘制闪动的文字
- 自定义控件之——文字闪动
- Android自定义View之闪动的TextView
- 【Android UI】自定义TextView实现动态的文字闪动效果
- 继承TextView实现自定义动态文字闪动效果
- 自定义控件,闪动文字FlickeringTextView
- 自定义view之倒影效果
- 自定义View之文字游乐场(一)
- 自定义View之文字游乐场(二)
- 自定义View进阶-Canvas之图片文字
- Android 自定义View学习之文字绘制
- 自定义View起步:Canvas之绘制文字
- 自定义View绘制文字
- 加速度计和陀螺仪
- 从Eclipse切换到IDEA后需要做的事情
- SharedPreferences基本介绍
- 第二十六天 jsp+servlet+mysql写的一个登录验证
- Directx11教程四十二上之SkeletalAnimation(骨骼动画)的原理
- 自定义View之自定义文字闪动效果
- 0526 CF#96A&G2n G- Football
- 2017华为软件精英挑战赛-我们组的解法
- 鼠标拖动来修改JFreeChart 节点上的值
- python 制作图表 matplotlib
- 第一次的博客
- ViewPager简单介绍
- 串口通讯常识介绍
- IT转型之路(一) 迷茫、困惑