android图片填充颜色,android – 如何在特定区域填充图像中的颜色?

我找到了使用Flood填充算法的解决方案

private void FloodFill(Bitmap bmp, Point pt, int targetColor, int replacementColor){

Queue q = new LinkedList();

q.add(pt);

while (q.size() > 0) {

Point n = q.poll();

if (bmp.getPixel(n.x, n.y) != targetColor)

continue;

Point w = n, e = new Point(n.x + 1, n.y);

while ((w.x > 0) && (bmp.getPixel(w.x, w.y) == targetColor)) {

bmp.setPixel(w.x, w.y, replacementColor);

if ((w.y > 0) && (bmp.getPixel(w.x, w.y - 1) == targetColor))

q.add(new Point(w.x, w.y - 1));

if ((w.y < bmp.getHeight() - 1)

&& (bmp.getPixel(w.x, w.y + 1) == targetColor))

q.add(new Point(w.x, w.y + 1));

w.x--;

}

while ((e.x < bmp.getWidth() - 1)

&& (bmp.getPixel(e.x, e.y) == targetColor)) {

bmp.setPixel(e.x, e.y, replacementColor);

if ((e.y > 0) && (bmp.getPixel(e.x, e.y - 1) == targetColor))

q.add(new Point(e.x, e.y - 1));

if ((e.y < bmp.getHeight() - 1)

&& (bmp.getPixel(e.x, e.y + 1) == targetColor))

q.add(new Point(e.x, e.y + 1));

e.x++;

}

}}

洪水填满android:

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实现不规则封闭区域填充颜色,可以使用Canvas的drawPath()方法和Paint的setShader()方法来实现。具体步骤如下: 1. 创建一个自定义View类 ```java public class MyView extends View { private Path mPath; private Paint mPaint; public MyView(Context context) { super(context); init(); } public MyView(Context context, AttributeSet attrs) { super(context, attrs); init(); } private void init() { mPaint = new Paint(); mPaint.setStyle(Paint.Style.FILL); mPaint.setAntiAlias(true); mPath = new Path(); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); canvas.drawPath(mPath, mPaint); } @Override public boolean onTouchEvent(MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: mPath.reset(); mPath.moveTo(event.getX(), event.getY()); return true; case MotionEvent.ACTION_MOVE: mPath.lineTo(event.getX(), event.getY()); break; default: return false; } invalidate(); return true; } } ``` 在上面的代码,我们创建了一个自定义View类MyView,并在构造函数初始化了Paint和Path对象。在init()方法,我们设置Paint的样式为填充,并开启了抗锯齿。在onDraw()方法,我们使用Canvas的drawPath()方法来绘制Path对象。在onTouchEvent()方法,我们根据用户的触摸事件来更新Path对象,并调用invalidate()方法来刷新视图。 2. 在MainActivity添加自定义View ```java public class MainActivity extends AppCompatActivity { private MyView mView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mView = findViewById(R.id.my_view); } public void setFillColor(View view) { int color = ContextCompat.getColor(this, R.color.fill_color); mView.getPaint().setShader(new LinearGradient(0, 0, getWidth(), getHeight(), color, color, Shader.TileMode.REPEAT)); mView.invalidate(); } } ``` 在上面的代码,我们在MainActivity添加了一个自定义View,并在setFillColor()方法设置填充颜色。这里我们使用了ContextCompat.getColor()方法来获取颜色值,并使用LinearGradient来创建着色器。最后,我们调用invalidate()方法刷新视图,使填充颜色生效。 3. 在布局文件添加自定义View ```xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.fillcolor.MainActivity"> <com.example.fillcolor.MyView android:id="@+id/my_view" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#ffffff" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:layout_marginBottom="16dp" android:onClick="setFillColor" android:text="Set Fill Color" /> </RelativeLayout> ``` 在上面的布局文件,我们添加了一个RelativeLayout,并在其添加了一个自定义View和一个Button。自定义View的背景颜色设置为白色,以便更好地显示填充颜色。当用户点击Button时,将调用setFillColor()方法来设置填充颜色

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值