实现android启动界面字体的动画效果
16lz
2021-01-23
案例:
android手机系统在启动的时候,会有一段时间显示“Android”字样的动画,怎么样实现呢,其实很简单,就是在这几个字样加上阴影,实现方式如下:首先自定义一个显示启动几面字体的view。如下:
LauncherTextView.java
public class LauncherTextView extends View {private Paint txtPaint;private Shader shader;private float dx = 50;private long lastTime = System.currentTimeMillis();private boolean start = false;private float height, width;private void init() {txtPaint = new Paint();txtPaint.setColor(Color.BLUE);txtPaint.setAntiAlias(true);// txtPaint.setTextAlign(Paint.Align .RIGHT);height = this.getHeight();width = this.getWidth();shader = new LinearGradient(0, 0, 200, 0, new int[] { Color.BLUE,Color.GREEN, Color.WHITE }, new float[] { 0, 0.7f, 1 },TileMode.MIRROR);// shader = new LinearGradient(0, 0, 200, 0,// new int[]{Color.argb(255, 120, 120, 120), Color.argb(255, 120, 120,// 120), Color.WHITE}, new float[]{0, 0.7f, 1}, TileMode.MIRROR);txtPaint.setShader(shader);}public LauncherTextView(Context context) {super(context);init();}public LauncherTextView(Context context, AttributeSet attrs) {super(context, attrs);init();}public void setStart(boolean start) {this.start = start;invalidate();}@Overrideprotected void onSizeChanged(int w, int h, int oldw, int oldh) {width = w / 2f;height = h / 2f;Log.d("size", "onSizeChanged: width " + width + ", height " + height);super.onSizeChanged(w, h, oldw, oldh);}@Overrideprotected void onDraw(Canvas canvas) {long now = System.currentTimeMillis();float elapsed = (now - lastTime) / 4.5f;dx += elapsed;Matrix matrix = new Matrix();if (start) {matrix.setTranslate(dx, 0);invalidate();} else {matrix.setTranslate(0, 0);}txtPaint.setTextSize(height*1.2f);//txtPaint.setTextScaleX(width / height);txtPaint.setTextAlign(Paint.Align.CENTER);FontMetrics fm = txtPaint.getFontMetrics();shader.setLocalMatrix(matrix);canvas.drawText(getContext().getString(R.string.start), width ,height / 2 - (fm.ascent + fm.descent) / 2, txtPaint); lastTime = now;}}
在main.xml中这样定义显示的xml:
<com.igrs.client.ui.widget.LauncherTextView android:id="@+id/start" android:layout_width="wrap_content" android:layout_height="@dimen/title_height"></com.igrs.client.ui.widget.LauncherTextView>
而在主线程中只需要这样调用就可以:
((LauncherTextView)findViewById(R.id.igrs)).setStart(true);
更多相关文章
- Android主线程与子线程之沟通
- Android UI主线程与子线程
- android主线程中Looper.loop()为什么不会造成程序ANR
- android利用handler回到主线程
- android主线程和子线程的区别
- android 那些事---主线程是线程不安全的
- 开机动画(闪动的ANDROID字样的动画图片)
- Android,UI主线程与子线程