Android:如何做这种框架画图? [英] Android: How to do this framing paint?

查看:79
本文介绍了Android:如何做这种框架画图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些静态图像,如下所示:

I Have Some static images like below:

现在,我要的是,当我触摸脸部或手部时,所选的颜色应该填充在该皮肤部分上.

Now, I want is, when i touch on the face or hand, then the selected color should be fill on that skin portion.

请参见下面的结果图片:

See below image of result:

那么如何获得上面的结果呢? 重做和撤消功能也应该存在.

So how to get the result like above ?? Redo and Undo Functionality Should be also there.

我尝试过使用FloodFill颜色,但这样做只能对垂直部分进行着色.因为FloodFill仅填充颜色,直到出现相同的pixwl颜色为止.如果触摸位置的像素颜色发生变化,它将不会在其上填充颜色.

I have try with the FloodFill color but doing that i can only able to do color in to the perticular portion. as FloodFill only fill the color till the same pixwl color comes. If the touch place pixel color get change the it will not fill color on it.

因此,Usinf FloodFill得到了如下图所示的结果,如果我按手,则只有手部分会填充颜色,而不是我想为另一只手和脸部填充颜色.

So Usinf FloodFill i got the result like below image, If i press on the hand, then only hand portion will fill with color, instead of it i want to fill color to the other hand and face also.

所以在这种情况下请帮助我.

So Please help me in this case.

已编辑

回复后,我得到了这一个解决方案.

但是仍然存在内存问题.绘制颜色会占用大量内存.所以,请问有人可以帮我吗?

But still there is a memory issue. It consume lots of memory to draw the color. So please can anyone help me for it ?

推荐答案

您可以使完整的图像以实际的方式着色,并且当您用某种颜色填充特定区域时,它将替换该颜色指定的所有区域填写.

You can have a complete image colored the actual way and when you fill a certain region with a color, it will replace all the regions that is specified by that color to be filled in.

Layman的条款:

Layman's terms:

  1. 用户将点击大纲"上的手
  2. 该点击位置将与另一幅具有完美颜色编码区域的图像进行核对.在这种情况下,我们称其为MASK.所有皮肤区域将具有相同的颜色.衬衫区域将是另一种颜色.
  3. 无论用户单击什么位置,用户选择的颜色都将应用于在MASK中具有相似颜色的每个像素,但是您不必直接在MASK上绘画,而是在OUTLINE的像素上绘画.

我希望这会有所帮助.

如果您想举一个例子,请随时发表评论,然后我可以用它来更新答案,但我认为您可以从这里获得答案.

Feel free to comment if you want an example and then I can update the answer with that, but I think you can get it from here.

基本上从一个简单的图像开始,像这样.我们可以称其为 OUTLINE

Basically start off with a simple image like this. This we can call as OUTLINE

然后,作为开发人员,您必须做一些工作.在这里,您为 OUTLINE 进行颜色编码.结果我们称为 MASK .为此,请对所需区域使用相同的颜色进行颜色编码.这可以在油漆或其他任何东西上完成.我曾经用过Photoshop太酷了:D.

Then as the developer, you have to do some work. Here, you color code the OUTLINE. The result we call a MASK. To make this we, color code the regions with the same color that you want. This can be done on paint or whatever. I used Photoshop to be cool lol :D.

然后有算法使其可以在手机上运行.在阅读代码之前,请查看此变量.

Then there is the ALGORITHM to get it working on the phone. Before you read the code, look at this variable.

int ANTILAISING_TOLERANCE = 70; //Larger better coloring, reduced sensing

如果您放大图像时特别注意边框的黑色区域,则实际上可以看到,有时计算机会稍微混合一些颜色.为了解决这一变化,我们使用此公差值.

If you zoom up on the image specifically noting the black regions of the border, you can actually see that sometimes, the computer blends the colors a little bit. In order to account for that change, we use this tolerance value.

着色和仿生.JAVA

package mk.coloring;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.view.View.OnTouchListener;

public class ColoringAndroidActivity extends Activity implements OnTouchListener{
    /** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    findViewById(R.id.imageView1).setOnTouchListener(this);
}

int ANTILAISING_TOLERANCE = 70;
public boolean onTouch(View arg0, MotionEvent arg1) {
    Bitmap mask = BitmapFactory.decodeResource(getResources(), R.drawable.mask);
    int selectedColor = mask.getPixel((int)arg1.getX(),(int)arg1.getY());           
    int sG = (selectedColor & 0x0000FF00) >> 8;
    int sR = (selectedColor & 0x00FF0000) >> 16;
    int sB = (selectedColor & 0x000000FF);

    Bitmap original = BitmapFactory.decodeResource(getResources(), R.drawable.empty);       
    Bitmap colored = Bitmap.createBitmap(mask.getWidth(), mask.getHeight(), Config.ARGB_8888);
    Canvas cv = new Canvas(colored);
    cv.drawBitmap(original, 0,0, null);

    for(int x = 0; x<mask.getWidth();x++){
        for(int y = 0; y<mask.getHeight();y++){
            int g = (mask.getPixel(x,y) & 0x0000FF00) >> 8;
            int r = (mask.getPixel(x,y) & 0x00FF0000) >> 16;
            int b = (mask.getPixel(x,y) & 0x000000FF);
            if(Math.abs(sR - r) < ANTILAISING_TOLERANCE && Math.abs(sG - g) < ANTILAISING_TOLERANCE && Math.abs(sB - b) < ANTILAISING_TOLERANCE)
                colored.setPixel(x, y, (colored.getPixel(x, y) & 0xFF000000) | 0x00458414);
        }
    }
    ((ImageView)findViewById(R.id.imageView1)).setImageBitmap(colored);

    return true;
}

}

此代码不能为用户提供很多颜色选择.相反,如果用户触摸某个区域,它将查看 MASK (遮罩)并相应地绘制 OUTLINE .但是,您可以进行真正有趣的互动.

This code doesn't provide the user with much of color choices. Instead, if the user touches a region, it will look at the MASK and paint the OUTLINE accordingly. But, you can make really interesting and interactive.

结果

当我触摸男人的头发时,它不仅给头发染上了颜色,而且给他的衬衫和手染上了相同的颜色.将其与 MASK (面具)进行比较,可以很好地了解发生了什么.

When I touched the man's hair, it not only colored the hair, but colored his shirt and hand with the same color. Compare it with the MASK to get a good idea of what happened.

这只是一个基本想法.我创建了多个位图,但实际上并不需要.我已将其用于测试目的并占用了不必要的内存.而且您无需在每次单击时都重新创建蒙版.

This is just a basic idea. I have created multiple Bitmaps but there is not really a need for that. I had used it for testing purposes and takes up unnecessary memory. And you don't need to recreate the mask on every click, etc.

我希望这对您有帮助:D

I hope this helps you :D

祝你好运

这篇关于Android:如何做这种框架画图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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