From a51972667c93e74315fef3b21130a3015e098126 Mon Sep 17 00:00:00 2001 From: Sun Date: Tue, 21 Aug 2018 16:11:57 +0800 Subject: [PATCH] use butterknife to bind view and modify the click methods. --- app/build.gradle | 15 ++--- .../example/android/emojify/MainActivity.java | 60 ++++++++++--------- app/src/main/res/layout/activity_main.xml | 5 +- build.gradle | 4 +- gradle/wrapper/gradle-wrapper.properties | 4 +- 5 files changed, 47 insertions(+), 41 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 169a6833..c6d01e7c 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -1,12 +1,11 @@ apply plugin: 'com.android.application' android { - compileSdkVersion 25 - buildToolsVersion "25.0.2" + compileSdkVersion 27 defaultConfig { applicationId "com.example.android.emojify" minSdkVersion 15 - targetSdkVersion 25 + targetSdkVersion 27 versionCode 1 versionName "1.0" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" @@ -24,9 +23,11 @@ dependencies { androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { exclude group: 'com.android.support', module: 'support-annotations' }) - compile 'com.android.support:design:25.2.0' - compile 'com.android.support:appcompat-v7:25.2.0' - compile 'com.google.android.gms:play-services-vision:10.2.0' - // TODO (1): Add Butterknife library and annotations dependency + implementation 'com.android.support:design:27.1.1' + implementation 'com.android.support:appcompat-v7:27.1.1' + implementation 'com.google.android.gms:play-services-vision:10.2.0' + // (1): Add Butterknife library and annotations dependency + implementation 'com.jakewharton:butterknife:8.8.1' + annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1' testCompile 'junit:junit:4.12' } diff --git a/app/src/main/java/com/example/android/emojify/MainActivity.java b/app/src/main/java/com/example/android/emojify/MainActivity.java index b0952892..b54a7bd0 100644 --- a/app/src/main/java/com/example/android/emojify/MainActivity.java +++ b/app/src/main/java/com/example/android/emojify/MainActivity.java @@ -39,23 +39,36 @@ import java.io.File; import java.io.IOException; +import butterknife.BindView; +import butterknife.ButterKnife; +import butterknife.OnClick; + public class MainActivity extends AppCompatActivity { - // TODO (2): Replace all View declarations with Butterknife annotations + // (2): Replace all View declarations with Butterknife annotations private static final int REQUEST_IMAGE_CAPTURE = 1; private static final int REQUEST_STORAGE_PERMISSION = 1; private static final String FILE_PROVIDER_AUTHORITY = "com.example.android.fileprovider"; - private ImageView mImageView; + @BindView(R.id.image_view) + ImageView mImageView; + + @BindView(R.id.emojify_button) + Button mEmojifyButton; + + @BindView(R.id.share_button) + FloatingActionButton mShareFab; - private Button mEmojifyButton; - private FloatingActionButton mShareFab; - private FloatingActionButton mSaveFab; - private FloatingActionButton mClearFab; + @BindView(R.id.save_button) + FloatingActionButton mSaveFab; - private TextView mTitleTextView; + @BindView(R.id.clear_button) + FloatingActionButton mClearFab; + + @BindView(R.id.title_text_view) + TextView mTitleTextView; private String mTempPhotoPath; @@ -66,15 +79,10 @@ public class MainActivity extends AppCompatActivity { protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); - - // TODO (3): Replace the findViewById calls with the Butterknife data binding + ButterKnife.bind(this); + // (3): Replace the findViewById calls with the Butterknife data binding // Bind the views - mImageView = (ImageView) findViewById(R.id.image_view); - mEmojifyButton = (Button) findViewById(R.id.emojify_button); - mShareFab = (FloatingActionButton) findViewById(R.id.share_button); - mSaveFab = (FloatingActionButton) findViewById(R.id.save_button); - mClearFab = (FloatingActionButton) findViewById(R.id.clear_button); - mTitleTextView = (TextView) findViewById(R.id.title_text_view); + } /** @@ -100,7 +108,7 @@ public void emojifyMe(View view) { @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, - @NonNull int[] grantResults) { + @NonNull int[] grantResults) { // Called when you request permission to read and write to external storage switch (requestCode) { case REQUEST_STORAGE_PERMISSION: { @@ -183,7 +191,7 @@ private void processAndSetImage() { // Resample the saved image to fit the ImageView mResultsBitmap = BitmapUtils.resamplePic(this, mTempPhotoPath); - + // Detect the faces and overlay the appropriate emoji mResultsBitmap = Emojifier.detectFacesandOverlayEmoji(this, mResultsBitmap); @@ -193,13 +201,13 @@ private void processAndSetImage() { } - // TODO (4): Replace OnClick methods with Butterknife annotations for OnClicks + // (4): Replace OnClick methods with Butterknife annotations for OnClicks + /** * OnClick method for the save button. - * - * @param view The save button. */ - public void saveMe(View view) { + @OnClick(R.id.save_button) + public void saveMe() { // Delete the temporary image file BitmapUtils.deleteImageFile(this, mTempPhotoPath); @@ -209,10 +217,9 @@ public void saveMe(View view) { /** * OnClick method for the share button, saves and shares the new bitmap. - * - * @param view The share button. */ - public void shareMe(View view) { + @OnClick(R.id.share_button) + public void shareMe() { // Delete the temporary image file BitmapUtils.deleteImageFile(this, mTempPhotoPath); @@ -225,10 +232,9 @@ public void shareMe(View view) { /** * OnClick for the clear button, resets the app to original state. - * - * @param view The clear button. */ - public void clearImage(View view) { + @OnClick(R.id.clear_button) + public void clearImage() { // Clear the image and toggle the view visibility mImageView.setImageResource(0); mEmojifyButton.setVisibility(View.VISIBLE); diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 32c534b5..288f754e 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -13,7 +13,7 @@ See the License for the specific language governing permissions and limitations under the License.--> - + @@ -94,7 +92,6 @@ android:layout_marginBottom="@dimen/fab_margins" android:layout_marginLeft="@dimen/fab_margins" android:layout_marginStart="@dimen/fab_margins" - android:onClick="shareMe" android:src="@drawable/ic_share" android:visibility="gone" app:backgroundTint="@android:color/white" /> diff --git a/build.gradle b/build.gradle index 74b2ab0d..7e01a0aa 100644 --- a/build.gradle +++ b/build.gradle @@ -3,9 +3,10 @@ buildscript { repositories { jcenter() + google() } dependencies { - classpath 'com.android.tools.build:gradle:2.2.3' + classpath 'com.android.tools.build:gradle:3.0.1' // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files @@ -15,6 +16,7 @@ buildscript { allprojects { repositories { jcenter() + google() } } diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 04e285f3..bc31fda6 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,6 +1,6 @@ -#Mon Dec 28 10:00:20 PST 2015 +#Tue Aug 21 15:57:55 CST 2018 distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-2.14.1-all.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-4.1-all.zip