|
1 | 1 | package lakmalz.git.colouringimagefloodfill;
|
2 | 2 |
|
3 |
| -import android.support.v7.app.AppCompatActivity; |
| 3 | +import android.graphics.Bitmap; |
| 4 | +import android.graphics.BitmapFactory; |
| 5 | +import android.graphics.Color; |
| 6 | +import android.graphics.Rect; |
| 7 | +import android.graphics.drawable.BitmapDrawable; |
4 | 8 | import android.os.Bundle;
|
| 9 | +import android.support.v4.content.ContextCompat; |
| 10 | +import android.support.v7.app.AppCompatActivity; |
| 11 | +import android.util.Log; |
| 12 | +import android.view.MotionEvent; |
| 13 | +import android.view.View; |
| 14 | + |
| 15 | +import org.xdty.preference.colorpicker.ColorPickerDialog; |
| 16 | +import org.xdty.preference.colorpicker.ColorPickerSwatch; |
| 17 | + |
| 18 | +import butterknife.BindView; |
| 19 | +import butterknife.ButterKnife; |
| 20 | +import butterknife.OnClick; |
| 21 | + |
| 22 | +public class ColorActivity extends BaseActivity { |
| 23 | + @BindView(R.id.ivImage) |
| 24 | + TouchImageView ivImage; |
| 25 | + |
| 26 | + private int mSelectedColor; |
| 27 | + private Bitmap currentBitmap; |
| 28 | + private Bitmap originalBitmap; |
| 29 | + private int currentX, currentY, currentTolerance = 40; |
| 30 | + |
| 31 | + static { |
| 32 | + System.loadLibrary("jnibitmap"); |
| 33 | + } |
5 | 34 |
|
6 |
| -public class ColorActivity extends AppCompatActivity { |
| 35 | + public static native void floodFill(Bitmap bitmap, int x, int y, int fillColor, int targetColor, int tolerance); |
| 36 | + |
| 37 | + @Override |
| 38 | + protected int getLayoutId() { |
| 39 | + return R.layout.activity_main; |
| 40 | + } |
7 | 41 |
|
8 | 42 | @Override
|
9 | 43 | protected void onCreate(Bundle savedInstanceState) {
|
10 | 44 | super.onCreate(savedInstanceState);
|
11 |
| - setContentView(R.layout.activity_main); |
| 45 | + |
| 46 | + |
| 47 | + ButterKnife.bind(this); |
| 48 | + BitmapFactory.Options o = new BitmapFactory.Options(); |
| 49 | + o.inScaled = false; |
| 50 | + originalBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.four, o); |
| 51 | + ivImage.setImageBitmap(originalBitmap); |
| 52 | + currentBitmap = originalBitmap.copy(originalBitmap.getConfig(), true); |
| 53 | + |
| 54 | + ivImage.setOnTouchListener(new View.OnTouchListener() { |
| 55 | + @Override |
| 56 | + public boolean onTouch(View v, MotionEvent event) { |
| 57 | + switch (event.getAction()) { |
| 58 | + case MotionEvent.ACTION_DOWN: |
| 59 | + currentX = (int) event.getX()/4 ; |
| 60 | + currentY = (int) event.getY()/4; |
| 61 | + //-------------------------- |
| 62 | + |
| 63 | + //-------------------------- |
| 64 | + |
| 65 | + BitmapDrawable drawable = (BitmapDrawable) ((TouchImageView)v).getDrawable(); |
| 66 | + Rect imageBounds = new Rect(); |
| 67 | + ivImage.getDrawingRect(imageBounds); |
| 68 | + // original height and width of the bitmap |
| 69 | + int intrinsicHeight = drawable.getIntrinsicHeight(); |
| 70 | + int intrinsicWidth = drawable.getIntrinsicWidth(); |
| 71 | + |
| 72 | + // height and width of the visible (scaled) image |
| 73 | + int scaledHeight = imageBounds.height(); |
| 74 | + int scaledWidth = imageBounds.width(); |
| 75 | + |
| 76 | + // Find the ratio of the original image to the scaled image |
| 77 | + // Should normally be equal unless a disproportionate scaling |
| 78 | + // (e.g. fitXY) is used. |
| 79 | + float heightRatio = (float) intrinsicHeight / scaledHeight; |
| 80 | + float widthRatio = (float) intrinsicWidth / scaledWidth; |
| 81 | + |
| 82 | + // do whatever magic to get your touch point |
| 83 | + // MotionEvent event; |
| 84 | + |
| 85 | + // get the distance from the left and top of the image bounds |
| 86 | + float scaledImageOffsetX = event.getX() - imageBounds.left; |
| 87 | + float scaledImageOffsetY = event.getY() - imageBounds.top; |
| 88 | + |
| 89 | + // scale these distances according to the ratio of your scaling |
| 90 | + // For example, if the original image is 1.5x the size of the scaled |
| 91 | + // image, and your offset is (10, 20), your original image offset |
| 92 | + // values should be (15, 30). |
| 93 | + int originalImageOffsetX = Math.round(scaledImageOffsetX * widthRatio); |
| 94 | + int originalImageOffsetY = Math.round(scaledImageOffsetY * heightRatio); |
| 95 | + |
| 96 | + //-------------------------- |
| 97 | + Log.e("cxz", "xxx" + currentX + " " + currentY + " " + originalBitmap.getWidth() + " " + originalBitmap.getHeight()); |
| 98 | + currentBitmap = originalBitmap.copy(originalBitmap.getConfig(), true); |
| 99 | + floodFill(currentBitmap, currentX, currentY, mSelectedColor/*Color.YELLOW*/, Color.BLACK, 50); |
| 100 | + ivImage.setImageBitmap(currentBitmap); |
| 101 | + break; |
| 102 | + } |
| 103 | + return true; |
| 104 | + } |
| 105 | + }); |
12 | 106 | }
|
| 107 | + |
| 108 | + @OnClick(R.id.btn_select_color) |
| 109 | + public void onClickTest() { |
| 110 | + //Toast.makeText(this, MainActivity.getTestString(), Toast.LENGTH_LONG).show(); |
| 111 | + mSelectedColor = ContextCompat.getColor(this, R.color.flamingo); |
| 112 | + int[] mColors = getResources().getIntArray(R.array.default_rainbow); |
| 113 | + |
| 114 | + ColorPickerDialog dialog = ColorPickerDialog.newInstance(R.string.color_picker_default_title, |
| 115 | + mColors, |
| 116 | + mSelectedColor, |
| 117 | + 5, // Number of columns |
| 118 | + ColorPickerDialog.SIZE_SMALL); |
| 119 | + |
| 120 | + dialog.setOnColorSelectedListener(new ColorPickerSwatch.OnColorSelectedListener() { |
| 121 | + |
| 122 | + @Override |
| 123 | + public void onColorSelected(int color) { |
| 124 | + mSelectedColor = color; |
| 125 | + } |
| 126 | + |
| 127 | + }); |
| 128 | + |
| 129 | + dialog.show(getFragmentManager(), "color_dialog_test"); |
| 130 | + } |
| 131 | + |
13 | 132 | }
|
0 commit comments