Skip to content

Commit 62a928a

Browse files
author
Lakmal Weerasekara
committed
add ReadMe description and flood fill algorithm
1 parent f856e86 commit 62a928a

File tree

4 files changed

+153
-18
lines changed

4 files changed

+153
-18
lines changed

app/build.gradle

+13-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
apply plugin: 'com.android.application'
2+
apply plugin: 'android-apt'
23

34
android {
45
compileSdkVersion 25
56
buildToolsVersion "25.0.2"
67
defaultConfig {
78
applicationId "lakmalz.git.colouringimagefloodfill"
8-
minSdkVersion 19
9+
minSdkVersion 17
910
targetSdkVersion 25
1011
versionCode 1
1112
versionName "1.0"
@@ -17,6 +18,12 @@ android {
1718
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
1819
}
1920
}
21+
22+
externalNativeBuild {
23+
ndkBuild {
24+
path 'src/main/cpp/Android.mk'
25+
}
26+
}
2027
}
2128

2229
dependencies {
@@ -26,4 +33,9 @@ dependencies {
2633
})
2734
compile 'com.android.support:appcompat-v7:25.1.0'
2835
testCompile 'junit:junit:4.12'
36+
37+
38+
compile 'com.jakewharton:butterknife:8.2.1'
39+
apt 'com.jakewharton:butterknife-compiler:8.2.1'
40+
compile 'org.xdty.preference:color-picker:0.0.4'
2941
}
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,132 @@
11
package lakmalz.git.colouringimagefloodfill;
22

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;
48
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+
}
534

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+
}
741

842
@Override
943
protected void onCreate(Bundle savedInstanceState) {
1044
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+
});
12106
}
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+
13132
}
+17-13
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,22 @@
11
<?xml version="1.0" encoding="utf-8"?>
2-
<RelativeLayout
2+
<LinearLayout
33
xmlns:android="http://schemas.android.com/apk/res/android"
4-
xmlns:tools="http://schemas.android.com/tools"
5-
android:id="@+id/activity_main"
64
android:layout_width="match_parent"
75
android:layout_height="match_parent"
8-
android:paddingBottom="@dimen/activity_vertical_margin"
9-
android:paddingLeft="@dimen/activity_horizontal_margin"
10-
android:paddingRight="@dimen/activity_horizontal_margin"
11-
android:paddingTop="@dimen/activity_vertical_margin"
12-
tools:context="lakmalz.git.colouringimagefloodfill.ColorActivity">
6+
android:orientation="vertical">
137

14-
<TextView
15-
android:layout_width="wrap_content"
16-
android:layout_height="wrap_content"
17-
android:text="Hello World!"/>
18-
</RelativeLayout>
8+
<lakmalz.git.colouringimagefloodfill.TouchImageView
9+
android:id="@+id/ivImage"
10+
android:layout_width="match_parent"
11+
android:layout_height="match_parent"
12+
android:layout_weight="1"
13+
android:src="@drawable/one"/>
14+
15+
<Button
16+
android:id="@+id/btn_select_color"
17+
android:layout_gravity="center"
18+
android:layout_width="216dp"
19+
android:visibility="visible"
20+
android:text="Select color"
21+
android:layout_height="48dp"/>
22+
</LinearLayout>

build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ buildscript {
66
}
77
dependencies {
88
classpath 'com.android.tools.build:gradle:2.2.3'
9-
9+
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
1010
// NOTE: Do not place your application dependencies here; they belong
1111
// in the individual module build.gradle files
1212
}

0 commit comments

Comments
 (0)