2011-06-16 180 views
0

是否可以用兩種顏色(前綠和藍)填充圖像,但顏色只能塗在黑色上,而綠色只能塗在某個座標上,其餘的圖像應該塗上藍色?下面是其中的黑色必須被替換爲圖像的例子綠色和藍色android用顏色填充圖像

enter image description here

final image

+0

是的,但我不能接受只有正確回答我的問題... – 2011-06-16 12:52:09

+0

那很好,我只是想告訴你如果你不知道:)更多的,我無法看到圖像的例子.. – Stuti 2011-06-16 12:53:33

+0

我已經上傳和最終的圖像... – 2011-06-16 13:01:31

回答

0

你將不得不玩的位圖,顏色轉換矩陣和Alpha通道。

這樣的(不具有代碼手頭copypaste): 1.創建從經由彩色矩陣的黑色像素的alpha通道位圖(黑 - >的α1) 2.桶填充綠色矩形超過alpha通道 3。桶填充藍色矩形超過alpha通道 4.拉伸比原始位圖

3

是的,這可以通過使用下面的代碼:

圖片用於填充:http://i.stack.imgur.com/7rita.jpg

main.xml中

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:gravity="center_horizontal" 
android:orientation="vertical" > 

<RelativeLayout 
    android:id="@+id/relative_layout" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_marginBottom="5dp" 
    android:layout_weight="1" > 

    <ImageView 
     android:id="@+id/coringImage" 
     android:layout_width="300dp" 
     android:layout_height="300dp" /> 
</RelativeLayout> 

<LinearLayout 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" > 

    <Button 
     android:id="@+id/btn_red" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="red" /> 

    <Button 
     android:id="@+id/btn_yellow" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="yellow" /> 

    <Button 
     android:id="@+id/btn_blue" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="blue" /> 
</LinearLayout> 

</LinearLayout> 

活動課

public class FillColorActivity extends Activity implements OnTouchListener { 
private RelativeLayout drawingLayout; 
private MyView myView; 
Button red, blue, yellow; 
Paint paint; 

/** Called when the activity is first created. */ 
/* 
* 
* private ImageView imageView; private Canvas cv; private Bitmap mask, 
* original, colored; private int r,g,b; private int sG, sR, sB; 
*/ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    myView = new MyView(this); 
    drawingLayout = (RelativeLayout) findViewById(R.id.relative_layout); 
    drawingLayout.addView(myView); 

    red = (Button) findViewById(R.id.btn_red); 
    blue = (Button) findViewById(R.id.btn_blue); 
    yellow = (Button) findViewById(R.id.btn_yellow); 

    red.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      paint.setColor(Color.RED); 
     } 
    }); 

    yellow.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      paint.setColor(Color.YELLOW); 
     } 
    }); 
    blue.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      paint.setColor(Color.BLUE); 
     } 
    }); 
} 

public class MyView extends View { 

    private Path path; 
    Bitmap mBitmap; 
    ProgressDialog pd; 
    final Point p1 = new Point(); 
    Canvas canvas; 

    // Bitmap mutableBitmap ; 
    public MyView(Context context) { 
     super(context); 

     paint = new Paint(); 
     paint.setAntiAlias(true); 
     pd = new ProgressDialog(context); 
     paint.setStyle(Paint.Style.STROKE); 
     paint.setStrokeJoin(Paint.Join.ROUND); 
     paint.setStrokeWidth(5f); 
     mBitmap = BitmapFactory.decodeResource(getResources(), 
       R.drawable.cartoon).copy(Bitmap.Config.ARGB_8888, true); 

     this.path = new Path(); 
    } 

    @Override 
    protected void onDraw(Canvas canvas) { 
     this.canvas = canvas; 
     paint.setColor(Color.GREEN); 
     canvas.drawBitmap(mBitmap, 0, 0, paint); 

    } 

    @Override 
    public boolean onTouchEvent(MotionEvent event) { 
     float x = event.getX(); 
     float y = event.getY(); 
     switch (event.getAction()) { 
     case MotionEvent.ACTION_DOWN: 

      p1.x = (int) x; 
      p1.y = (int) y; 
      final int sourceColor = mBitmap.getPixel((int) x, (int) y); 
      final int targetColor = paint.getColor(); 
      new TheTask(mBitmap, p1, sourceColor, targetColor).execute(); 
      invalidate(); 
     } 
     return true; 
    } 

    public void clear() { 
     path.reset(); 
     invalidate(); 
    } 

    public int getCurrentPaintColor() { 
     return paint.getColor(); 
    } 

    class TheTask extends AsyncTask<Void, Integer, Void> { 

     Bitmap bmp; 
     Point pt; 
     int replacementColor, targetColor; 

     public TheTask(Bitmap bm, Point p, int sc, int tc) { 
      this.bmp = bm; 
      this.pt = p; 
      this.replacementColor = tc; 
      this.targetColor = sc; 
      pd.setMessage("Filling...."); 
      pd.show(); 
     } 

     @Override 
     protected void onPreExecute() { 
      pd.show(); 

     } 

     @Override 
     protected void onProgressUpdate(Integer... values) { 

     } 

     @Override 
     protected Void doInBackground(Void... params) { 
      FloodFill f = new FloodFill(); 
      f.floodFill(bmp, pt, targetColor, replacementColor); 
      return null; 
     } 

     @Override 
     protected void onPostExecute(Void result) { 
      pd.dismiss(); 
      invalidate(); 
     } 
    } 
} 

// flood fill 

public class FloodFill { 
    public void floodFill(Bitmap image, Point node, int targetColor, 
      int replacementColor) { 
     int width = image.getWidth(); 
     int height = image.getHeight(); 
     int target = targetColor; 
     int replacement = replacementColor; 
     if (target != replacement) { 
      Queue<Point> queue = new LinkedList<Point>(); 
      do { 

       int x = node.x; 
       int y = node.y; 
       while (x > 0 && image.getPixel(x - 1, y) == target) { 
        x--; 

       } 
       boolean spanUp = false; 
       boolean spanDown = false; 
       while (x < width && image.getPixel(x, y) == target) { 
        image.setPixel(x, y, replacement); 
        if (!spanUp && y > 0 
          && image.getPixel(x, y - 1) == target) { 
         queue.add(new Point(x, y - 1)); 
         spanUp = true; 
        } else if (spanUp && y > 0 
          && image.getPixel(x, y - 1) != target) { 
         spanUp = false; 
        } 
        if (!spanDown && y < height - 1 
          && image.getPixel(x, y + 1) == target) { 
         queue.add(new Point(x, y + 1)); 
         spanDown = true; 
        } else if (spanDown && y < height - 1 
          && image.getPixel(x, y + 1) != target) { 
         spanDown = false; 
        } 
        x++; 
       } 
     } while ((node = queue.poll()) != null); 
     } 
    } 
} 

@Override 
public boolean onTouch(View v, MotionEvent event) { 
    // TODO Auto-generated method stub 
    return false; 
} 
} 
+0

謝謝它適用於我,但你能否請我提供示例代碼填充圖像用刷子? – 2014-07-07 03:31:56