在我正在使用的应用程序中,我有自己的自定义视图。在此视图中,我在视图的onDraw()方法中使用canvas.drawRect()绘制了一个光标。这可以正常工作,但实际上是这样:我希望光标像大多数光标一样闪烁。如果是图像或某种视图,我可以轻松地使用AlphaAnimation并将重复计数设置为无限来执行此操作。但是,这将不起作用,因为我使用canvas.drawRect()绘制了光标,所以我的问题是:如何定期使光标以一种优雅而简单的方式出现和消失?
编辑:
使用blackbelt的输入,我创建了以下可运行的动画:
// Cursor blink animation
private Runnable cursorAnimation = new Runnable() {
public void run() {
// Switch the cursor visibility and set it
int newAlpha = (mCursorPaint.getAlpha() == 0) ? 255 : 0;
mCursorPaint.setAlpha(newAlpha);
// Call onDraw() to draw the cursor with the new Paint
invalidate();
// Wait 500 milliseconds before calling self again
postDelayed(cursorAnimation, 500);
}
};
在View的构造函数中,我调用post(cursorAnimation)
它来启动它。
的最后一个参数drawRect
是一个Paint
对象。您可以通过它更改rect alpha。您还可以View.postDelayed
用来决定如何更改Alpha值和使视图无效。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句