java - Android Canvas : make a drawn line fade/alphaAnimation away on touch

标签 java android android-canvas

我有一个Canvas,我在onTouchEvent()中的点之间绘制线条。当用户对放置线条的位置做出无效选择时,我会绘制一条红线而不是黑色线。不过我希望红线在接下来的 500-1000 毫秒内消失。

我还不知道如何做到这一点,甚至不知道是否可以在 Canvas 内为单行设置动画。

我已将点、有效黑线和无效红线分割在我的 3 个不同位图之间:

public void onDraw(Canvas canvas) {
    // Fade Red Line
    canvas.drawBitmap(fBitmap, 0, 0, null);
    // Lines
    canvas.drawBitmap(mBitmap, 0, 0, null);
    // Dots
    canvas.drawBitmap(iBitmap, 0, 0, null); 
}

在我的 onTouch 函数中是我的 drawLines()

我想知道是否可以使用 AlphaAnimation 来实现此目的,或者是否必须使用计时器系统来循环 fBitmap 以及我想要随时间淡出的红线。

有什么想法吗?

编辑:好吧,这是我一年后回来后最终解决它的方法:

我实现了一个SurfaceView并创建了一个线程来不断更新Canvas。

@Override
public void run() {
    while (isRunning) {
        if (!surfaceHolder.getSurface().isValid()) {
            continue;
        }

        decreaseLineOpacity();
        drawLinesInvalid();

        /* Other things to draw*/

        Canvas canvas = surfaceHolder.lockCanvas();

        canvas.drawBitmap(fBitmap, centerPointWidth, centerPointHeight, null);
        // mBitmap is the middleground with the line that fades away.
        // and is bound to mCanvas for drawing
        canvas.drawBitmap(mBitmap, centerPointWidth, centerPointHeight, null);
        canvas.drawBitmap(iBitmap, centerPointWidth, centerPointHeight, null);

        surfaceHolder.unlockCanvasAndPost(canvas);
    }
}

然后是负责更改不透明度和绘制线条的方法,我将绘制的每条线存储在队列中,直到其不透明度达到 0,然后将其删除,以便仅需要绘制线条剩下动画:

    private void drawLinesInvalid() {
        Iterator<int[]> iterator = invalidLines.iterator();
        // Loop through each element in the Queue, delete it from the bitmap with the porter
        // duff mode to keep the transparancy of the layer. Then draw line with the given opacity
        while (iterator.hasNext()) {
            int[] invalidLine = iterator.next();
            float[] line = {invalidLine[1], invalidLine[2], invalidLine[3], invalidLine[4]};
            Xfermode originalXfermode = paint.getXfermode();
            paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
            mCanvas.drawLines(line, paint);
            paint.setXfermode(originalXfermode);

            paint.setColor(Color.RED);
            paint.setAlpha(invalidLine[0]);

            mCanvas.drawLines(line, paint);

        }
    }

// Call this function in the onTouch method
    private void addLineToInvalidList(float[] line) {
        // Store each line a queue with its current opacity
        int[] lineWithOpacity = new int[5];
        lineWithOpacity[0] = 250;
        lineWithOpacity[1] = (int) line[0];
        lineWithOpacity[2] = (int) line[1];
        lineWithOpacity[3] = (int) line[2];
        lineWithOpacity[4] = (int) line[3];
        invalidLines.add(lineWithOpacity);
    }

    private void decreaseLineOpacity() {
        Iterator<int[]> iterator = invalidLines.iterator();
        // Remove all lines that are finished animating (opacity = 0)
        while (iterator.hasNext()) {
            int[] line = iterator.next();
            if (line[0] == 0) {
                iterator.remove();
            }
        }

        // Decrease opacity on lines that are left
        iterator = invalidLines.iterator();
        while (iterator.hasNext()) {
            int[] line = iterator.next();
            line[0] -= 10;
        }
    }

最佳答案

考虑到您要在同一 View 上绘制所有三个位图,使用 AlphaAnimation 可能有点棘手。

我建议生成一个线程,随着时间的推移更改无效红线位图的 alpha 值(使用 Thread.sleep hibernate 设定的时间间隔)。但是,为了使其表现良好,您应该考虑使用 SurfaceView,因为多次调用 postInvalidate 的成本可能相当高(除非它在具有硬件加速的 Android 3.0+ 上运行) .

关于java - Android Canvas : make a drawn line fade/alphaAnimation away on touch,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7891109/

相关文章:

android - 按钮将背景设置为透明色

android - 如何绘制平滑的位图路径,没有任何中断

android - 在 Android 中通过 Canvas 创建一个空位图并进行绘图

java - 安卓 native Activity 。如何在其他线程中做一些工作?

java - 带 LayeredBarRenderer 的标签

java - 将 RuntimeBeanRefrence 用于对象列表时出现 ConversionNotSupportedException

java - 向下转换对象的时间复杂度是多少?

android - 如何通过Android中的编程实现On()MIC

android - 自定义 ttf 字体在 Android 4.4 KitKat 上的 TextView 中显示不正确

android - Android 应用程序中的奇怪图形伪影