【问题标题】:android canvas drawBitMap and switch between images on certain intervalandroid canvas drawBitMap 并以一定的间隔在图像之间切换
【发布时间】:2016-07-16 18:37:28
【问题描述】:

使用表面视图和位图的应用程序的相机活动是使用 onDraw() 方法内部的 canvas.drawBitMap() 方法绘制的(白色矩形)表面视图。 我使用以下代码来实现这一点:

canvas.drawBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.corner_top_left), x1, y1, null);
canvas.drawBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.corner_top_right), x2, y1, null);
canvas.drawBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.corner_bottom_left), x1, y2, null);
canvas.drawBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.corner_bottom_right), x2, y2, null);

截图:

但我想要通过在 2 个位图之间切换来获得动画效果,应该如下所示:

我试图找出几个解决方案(即使在 stackoverflow 上),但发现只是冗长且成本高昂。请提出一些干净且成本更低的方法。

我正在使用画布在 SurfaceView 中执行所有这些操作。

【问题讨论】:

    标签: android canvas android-animation surfaceview ondraw


    【解决方案1】:

    我通过在surfaceview的onDraw()中调用postInvalidateDelayed来做到这一点。

    postInvalidateDelayed(500, left,top,right,bottom);
    

    这就是它的工作原理,onDraw() 方法每 500 毫秒调用一次,时间会改变位图(如果是白色则为红色,如果为红色则为白色)。图像之间的切换可以使用简单的标志来完成。

        if(rectBlink){
                canvas.drawBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.corner_top_left_white), x1, y1, null);
                canvas.drawBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.corner_top_right_white), x2, y1, null);
                canvas.drawBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.corner_bottom_left_white), x1, y2, null);
                canvas.drawBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.corner_bottom_right_white), x2, y2, null);
            }
            else{
                canvas.drawBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.corner_top_left_red), x1, y1, null);
                canvas.drawBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.corner_top_right_red), x2, y1, null);
                canvas.drawBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.corner_bottom_left_red), x1, y2, null);
                canvas.drawBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.corner_bottom_right_red), x2, y2, null);
            }
    

    然后使用下面的代码将标志状态表从 true 更改为 false 或将 false 更改为 true:

    rectBlink=!rectBlink;
    

    这一切都完成了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多