简体   繁体   English

尝试在Android画布上进行聚光灯效果

[英]Trying to do a spotlight effect in android canvas

I have a canvas and a simple bitmap for background image, fills the whole screen. 我有一个画布和一个用于背景图像的简单位图,填充了整个屏幕。 I created a rect painted black and set it's alpha to 250 in order to make a "dark" effect on the background image. 我创建了一个涂成黑色的rect,并将其alpha设置为250,以便在背景图像上产生“深色”效果。 My aim to make a simple circle object that reveals the place it's hovering above. 我的目标是制作一个简单的圆形对象,以显示其上方悬停的位置。 I tried thinking in many ways how to excecute it and failed. 我试图以多种方式思考如何执行它,但失败了。

I think the best way is to create a simple circle that manages to decrease the darkness alpha on the position it hovers above, but I have no idea how to do it. 我认为最好的方法是创建一个简单的圆,以设法降低其悬停在上方的位置的暗度alpha,但我不知道该怎么做。

The relevant part of my code: 我的代码的相关部分:

private ColorFilter filter = new LightingColorFilter(Color.BLACK, 1);
private Paint darkPaint = new Paint(Color.BLACK), paint = new Paint(), paint2 = new Paint();//The style of the text and dark.

public DarkRoomView(Context context) {
        super(context);
        myChild = this;
        darkPaint.setColorFilter(filter);
                darkPaint.setAlpha(250);
        paint2.setAlpha(10);
        paint.setAlpha(50);
    }

private void loadGFX() {//Loads all of this view GFX file.
        backgroundImage = BitmapFactory.decodeResource(getResources(),                R.drawable.darkroomscreen);

        lightImage = BitmapFactory.decodeResource(getResources(), R.drawable.light);

    }

private void drawGFX(Canvas canvas) {
      canvas.drawBitmap(backgroundImage, 0, 0, paint2);//The backgeound image.
      canvas.drawRect(0, 0, WIDTH, HEIGHT, darkPaint);//The darkness.
      canvas.drawBitmap(lightImage, 50, 50, paint);//A spotlight.
}

Any ideas how I should get it done? 有什么想法我应该完成吗? Thanks! 谢谢!

For the spotlight, you could draw a circle of the original image over the darkness. 对于聚光灯下,你可以画黑暗中的原始图像的圆。 You'd simply need to find the correct rectangle of the original image (based on where your finger is), and then draw a circle of that particular rectangle over the darkness. 您只需要找到原始图像的正确矩形(基于手指的位置),然后在黑暗中绘制该特定矩形的圆即可。 Trying to look "through" the darkness won't really get you anywhere; 试图“穿越”黑暗不会真正带你到任何地方。 you need to place something over it. 您需要在上面放置一些东西。

By the time you draw the "spotlight", you've already darkened the image with the rectangle. 到绘制“聚光灯”时,您已经用矩形使图像变暗了。 It would be difficult to recover information lost during that draw. 恢复在抽签过程中丢失的信息将很困难。

A more flexible approach would be to draw a dark rectangle with a spotlight in a separate image (that is, compose the "darkness" and spotlight alpha and color mask image first), and then draw that mask image on top of the background as a separate step. 一种更灵活的方法是在单独的图像中绘制一个带有聚光灯的暗矩形(即,首先合成“暗度”,然后聚光alpha和颜色蒙版图像),然后将该蒙版图像作为背景绘制在背景顶部单独的步骤。 This would also let you easily do things like eg give the spotlight fuzzy borders. 这也可以让您轻松地执行诸如给聚光灯模糊边界的操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM