更新时间:2022-06-04 07:46:41
你可以让一个完整的图像以实际的方式着色,当你用某种颜色填充某个区域时,它将替换该颜色指定的所有区域待填写.
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.
外行条款:
我希望这会有所帮助.
如果你想要一个例子,请随时发表评论,然后我可以用它更新答案,但我认为你可以从这里得到它.
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
那么作为开发人员,您必须做一些工作.在这里,您可以对大纲进行颜色编码.我们将结果称为 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.
COLORINGANDROIDACTIVITY.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 并相应地绘制大纲.但是,您可以制作非常有趣和互动的作品.
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
祝你好运