划伤屏幕,显示在android的图像 [英] Scratch the screen to display an image in android

查看:115
本文介绍了划伤屏幕,显示在android的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的工作我的任务一个Android项目。我试图做一个从无到有的图像应用程序,你知道它就像我们划伤屏幕摆脱阻挡层来显示图像。但问题是我不知道从哪里开始。

i am working on a android project for my assignment. i am trying to make a scratch image application, you know it's like we scratch the screen to get rid the blocking layer to display the image. but the problem is i don't know where to start.

我有搜索在与此相关的,但是这不是帮助计算器的问题。 从我的搜索那里,我发现了一个线索,为这个项目使用 Bitmap.getPixel(INT X,int y)对

i have searching in stackoverflow's questions that related to this but that's not help. from my search there, i found a clue for this project is using Bitmap.getPixel(int x, int y).

所以,在我的思想,我有从位图获得像素,并画到画布上。 但我不知道如何实现的呢? 或任何人有这更好的方法?

so, in my thought i have to get pixel from bitmap and paint it to canvas. but i don't know how to implement it? or anyone has a better method for this?

谁能帮帮我吗?对这种事情或相关主题的任何教程?

Could anyone please help me? Any tutorials on this kind of thing or related topics?

在此先感谢!

这是我的样品code:

here's my sample code:

@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
    super.onSizeChanged(w, h, oldw, oldh);
    tw = w;
    th = h;
    eraseableBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
    mCanvas = new Canvas(eraseableBitmap);
    Bitmap muteableBitmap = Bitmap.createBitmap(eraseableBitmap.getWidth(), eraseableBitmap.getHeight(), Bitmap.Config.ARGB_8888);
}

@Override
public boolean onTouchEvent(MotionEvent event) {
    static_x = event.getX();
    static_y = event.getY();

    if (event.getAction() == MotionEvent.ACTION_DOWN) {
        touch_start(static_x, static_y);

    } if (event.getAction() == MotionEvent.ACTION_MOVE) {
            touch_move(static_x, static_y);

    } if (event.getAction() == MotionEvent.ACTION_UP) {
            touch_up(); 
        }
    return true;
}


下面是我的项目的外观:


Here's the appearance of my project:

推荐答案

有趣的问题。我的理论方案:

Interesting question. My theoretical plan:

您有2位图,在可擦除位和隐性的位图。从现有的位图删除像素是不可能的,因为位图在Android的是不变。而不进行删除像素从可擦除位图来显示位图的下面,先画了可擦除位左右。然后创建一个空的,<一个href="http://developer.android.com/reference/android/graphics/Bitmap.html#createBitmap%28int,%20int,%20android.graphics.Bitmap.Config%29"相对=nofollow>可变位图。遍历所有在隐藏位图像素,显示只有在可擦除位图已被抹去。

You have 2 bitmaps, the 'erasable' bitmap and the 'hidden' bitmap. Erasing pixels from an existing bitmap is not possible because Bitmap's in Android are immutable. So instead of erasing pixels from the 'erasable' bitmap to reveal the bitmap underneath, first draw the 'erasable' bitmap. Then create an empty, mutable Bitmap. Loop over all of the pixels in the 'hidden' bitmap, displaying only where the 'erasable' bitmap has been 'erased'.

Bitmap mutableBitmap = Bitmap.create(erasableBitmap.getWidth(),erasableBitmap.getHeight(), Bitmap.Config.ARGB_8888);

for(Pixel erasedPixel : erasedList)
{
  mutableBitmap.setPixel(x,y, hiddenBitmap.getPixel(erasedPixel.x, erasedPixel.y));
}

...
// in a different file
class Pixel{
  int x, y;

}

你必须自己填写erasedList。

you'll have to fill the erasedList yourself.

一旦你做了,画到像这样的画布:

Once you are done, draw to the canvas like so:

canvas.drawBitmap(0,0,eraseableBitmap);
canvas.drawBitmap(0,0,mutableBitmap);

确保您绘制的擦除位图首先,使其上绘制新像素。

making sure that you draw the 'erasable' bitmap first so that it is drawn over with the new pixels.

如果你需要帮助搞清楚如何设置擦除像素,让我知道在评论,我会帮助你。

If you need help figuring out how to set the erased pixels, let me know in the comments and I'll help you out.

修改

要真正搞清楚哪些像素用户试图删除:在您看来,捕捉onTouch事件,你会得到的,其中用户触摸的屏幕上的坐标。 <打击>保存了一个某种形式的地图或哈希表的,你应该是好去。创建一个像素对象,并将其添加到全局列表的像素。

To actually figure out which pixels the user tried to erase: In your view, capture the onTouch event and you'll get a coordinate of where the user touched the screen. Save that to a some sort of map or hashtable and you should be good to go. Create a Pixel object and add it to a global List of pixels.

编辑2

要增加从零开始的大小,你所要做的就是有点复杂。你需要一些方法来创建一个在X,Y点感动,也算为消除周围区域。一个圆形将是理想的,但它会更容易使用方。

To increase the size of the "scratch" what you have to do is a little complicated. You need some way to create an area around the x,y point touched to also count as erased. A circle would be ideal but it will be easier to use a square.

for(Pixel erasedPixel: erasedList)
{
  //it's actually more complicated than this, since you need to check for boundary conditions.
  for(int i = -SQUARE_WIDTH/2; i < SQUARE_WIDTH/2; i++){
    for(int j = -SQUARE_WIDTH/2; j < SQUARE_WIDTH/2; j++){
      mutableBitmap.setPixel(erasedPixel.x+i, erasedPixel.y+j, hiddenBitmap.getPixel(erasedPixel.x+i, erasedPixel.y+j));
    }
  }
}

这篇关于划伤屏幕,显示在android的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆