案例:

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);


更多相关文章

  1. Android主线程与子线程之沟通
  2. Android UI主线程与子线程
  3. android主线程中Looper.loop()为什么不会造成程序ANR
  4. android利用handler回到主线程
  5. android主线程和子线程的区别
  6. android 那些事---主线程是线程不安全的
  7. 开机动画(闪动的ANDROID字样的动画图片)
  8. Android,UI主线程与子线程

随机推荐

  1. Android应用程序与SurfaceFlinger服务的
  2. Android应用程序请求SurfaceFlinger服务
  3. android打包apk流程
  4. android下的定时器在关闭屏幕后会自己停
  5. 在Android上实现WLAN的一点理解
  6. 关于Android的组件和进程的理解
  7. 采用XMPP协议实现Android推送
  8. 系出名门Android(1) - 在 Windows 下搭建
  9. Android应用程序与SurfaceFlinger服务的
  10. androidのEditTex详细使用