Add onion skin with dialog to configure it
authorDralagen <dralagen@dralagen.fr>
Wed, 8 Apr 2015 03:38:05 +0000 (05:38 +0200)
committerDralagen <dralagen@dralagen.fr>
Wed, 8 Apr 2015 03:38:05 +0000 (05:38 +0200)
app/src/main/java/org/alma/rotoscope/DrawingActivity.java
app/src/main/res/layout/activity_drawing.xml
app/src/main/res/layout/dialog_onion_settings.xml [new file with mode: 0644]
app/src/main/res/values/strings.xml

index a94da6a..5db6c88 100644 (file)
@@ -3,12 +3,15 @@ package org.alma.rotoscope;
 import android.animation.Animator;
 import android.animation.AnimatorListenerAdapter;
 import android.app.Activity;
+import android.app.AlertDialog;
 import android.app.ProgressDialog;
 import android.content.DialogInterface;
 import android.content.Intent;
 import android.content.SharedPreferences;
 import android.content.pm.ActivityInfo;
 import android.graphics.Bitmap;
+import android.graphics.Canvas;
+import android.graphics.Paint;
 import android.graphics.Point;
 import android.graphics.drawable.BitmapDrawable;
 import android.media.MediaMetadataRetriever;
@@ -21,6 +24,7 @@ import android.support.v4.content.FileProvider;
 import android.util.Log;
 import android.view.MotionEvent;
 import android.view.View;
+import android.widget.EditText;
 import android.widget.TextView;
 import org.alma.rotoscope.colorpicker.ColorPickerDialog;
 import org.jcodec.api.SequenceEncoder;
@@ -80,6 +84,16 @@ public class DrawingActivity extends Activity implements View.OnTouchListener {
   private List<Bitmap> layers;
 
   /**
+   * Number of onion skin
+   */
+  private int nbOnion;
+
+  /**
+   * Frequency of onion skin
+   */
+  private int freqOnion;
+
+  /**
    * A color picker dialog to change the paint color
    */
   private ColorPickerDialog colorPicker;
@@ -208,6 +222,8 @@ public class DrawingActivity extends Activity implements View.OnTouchListener {
       drawingArea.setOnTouchListener(this);
       currentPicture = 0;
       showBackground = true;
+      nbOnion = 0;
+      freqOnion = 1;
 
       setLayer();
 
@@ -311,7 +327,21 @@ public class DrawingActivity extends Activity implements View.OnTouchListener {
     DrawingArea drawingArea = (DrawingArea) findViewById(R.id.drawingAreaView);
     drawingArea.setLayer(layers.get(currentPicture));
     if (showBackground) {
-      drawingArea.setBackground(new BitmapDrawable(getResources(), currentFrame));
+      Bitmap background = Bitmap.createBitmap(currentFrame);
+      Canvas canvasBackground = new Canvas(background);
+      Paint backgroundPaint = new Paint(Paint.DITHER_FLAG);
+      backgroundPaint.setAlpha(100);
+
+      // Draw onion skin on background
+      int firstIndex = Math.max(currentPicture - nbOnion*freqOnion, 0);
+
+      for (int i = currentPicture - freqOnion ; i >= firstIndex; i-=freqOnion) {
+        // opacity between [100,200]
+        backgroundPaint.setAlpha((100/nbOnion*freqOnion)*(i-firstIndex) + 100);
+        canvasBackground.drawBitmap(layers.get(i), 0, 0, backgroundPaint);
+      }
+
+      drawingArea.setBackground(new BitmapDrawable(getResources(), background));
     }
 
     ((TextView)findViewById(R.id.numFrameLabel)).setText(currentPicture + 1 + "/" + layers.size());
@@ -519,6 +549,53 @@ public class DrawingActivity extends Activity implements View.OnTouchListener {
   }
 
   /**
+   * Open dialog to set nbOnion and set freqOnion
+   *
+   * @param view android view
+   */
+  public void onionSkinSettings(View view) {
+    final AlertDialog.Builder onionDialogBuilder = new AlertDialog.Builder(this);
+
+    View onionDialogView = getLayoutInflater().inflate(R.layout.dialog_onion_settings, null);
+    onionDialogBuilder.setView(onionDialogView);
+
+    ((EditText) onionDialogView.findViewById(R.id.nbOnionInput)).setText(String.valueOf(nbOnion));
+
+    ((EditText) onionDialogView.findViewById(R.id.freqOnionInput)).setText(String.valueOf(freqOnion));
+
+    onionDialogBuilder
+        .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
+          @Override
+          public void onClick(DialogInterface dialog, int which) {
+            AlertDialog d = (AlertDialog) dialog;
+            try {
+              nbOnion = Math.max(Integer.parseInt(((EditText) d.findViewById(R.id.nbOnionInput)).getText().toString()), 0);
+            } catch (NumberFormatException e) {
+              nbOnion = 0;
+            }
+
+            try {
+              freqOnion = Math.max(Integer.parseInt(((EditText) d.findViewById(R.id.freqOnionInput)).getText().toString()), 1);
+            } catch (NumberFormatException e) {
+              freqOnion = 1;
+            }
+
+            setLayer();
+          }
+        })
+        .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
+          @Override
+          public void onClick(DialogInterface dialog, int which) {
+            dialog.dismiss();
+          }
+        });
+
+    onionDialogBuilder.setTitle("Onion Skin Settings");
+
+    onionDialogBuilder.create().show();
+  }
+
+  /**
    * Copy the content of src into dest file
    *
    * @param src source file
index aa8ebd0..21e50e5 100644 (file)
@@ -6,7 +6,7 @@
 
   <view class="org.alma.rotoscope.DrawingArea" android:layout_width="fill_parent" android:layout_height="fill_parent"
         android:id="@+id/drawingAreaView" android:background="@android:color/background_dark"/>
-  <RelativeLayout android:layout_width="fill_parent" android:layout_height="50pt"
+  <RelativeLayout android:layout_width="fill_parent" android:layout_height="25pt"
                   android:layout_gravity="center_vertical|bottom"
                   android:orientation="horizontal" android:id="@+id/navigationLayout"
                   android:animateLayoutChanges="true" android:layout_alignParentBottom="true"
@@ -50,7 +50,7 @@
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:id="@+id/shareButton" android:src="@android:drawable/ic_menu_share" android:contentDescription="@string/button_share"
-        android:onClick="shareVideo" android:layout_marginBottom="16pt" android:clickable="true"/>
+        android:onClick="shareVideo" android:layout_marginBottom="8pt" android:clickable="true"/>
     <ImageView
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
     <ImageView
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
+        android:id="@+id/onionSkinButton" android:layout_marginBottom="4pt" android:clickable="true"
+        android:contentDescription="@string/button_onionSkin" android:src="@android:drawable/ic_menu_report_image"
+        android:onClick="onionSkinSettings"/>
+    <ImageView
+        android:layout_width="match_parent"
+        android:layout_height="wrap_content"
         android:id="@+id/eraseButton" android:src="@android:drawable/ic_menu_delete" android:onClick="erasePicture"
         android:contentDescription="@string/button_clearLayer" android:layout_marginBottom="4pt"
         android:clickable="true"/>
diff --git a/app/src/main/res/layout/dialog_onion_settings.xml b/app/src/main/res/layout/dialog_onion_settings.xml
new file mode 100644 (file)
index 0000000..a6eb3ed
--- /dev/null
@@ -0,0 +1,40 @@
+<?xml version="1.0" encoding="utf-8"?>
+<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
+              android:orientation="vertical"
+              android:layout_width="wrap_content"
+              android:layout_height="wrap_content">
+
+  <LinearLayout
+      android:orientation="vertical"
+      android:layout_width="wrap_content"
+      android:layout_height="wrap_content" android:layout_gravity="center">
+    <TextView
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"
+        android:text="@string/nbOnionLabel"
+        android:id="@+id/nbOnionLabel" android:labelFor="@+id/nbOnionInput" android:layout_gravity="center_vertical"/>
+    <EditText
+        android:layout_width="fill_parent"
+        android:layout_height="wrap_content"
+        android:inputType="number"
+        android:ems="10"
+        android:id="@+id/nbOnionInput" android:layout_gravity="center_vertical"/>
+  </LinearLayout>
+  <LinearLayout
+      android:orientation="vertical"
+      android:layout_width="wrap_content"
+      android:layout_height="wrap_content" android:layout_gravity="center">
+    <TextView
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"
+        android:text="@string/freqOnionLabel"
+        android:id="@+id/freqOnionLabel" android:labelFor="@+id/freqOnionInput"
+        android:layout_gravity="center_vertical"/>
+    <EditText
+        android:layout_width="fill_parent"
+        android:layout_height="wrap_content"
+        android:inputType="number"
+        android:ems="10"
+        android:id="@+id/freqOnionInput" android:layout_gravity="center_vertical"/>
+  </LinearLayout>
+</LinearLayout>
\ No newline at end of file
index 8b5b088..5175443 100644 (file)
@@ -19,6 +19,9 @@
     <string name="send_to">Share your video</string>
     <string name="button_toggleBackground">Toggle Background</string>
     <string name="button_play">Play</string>
+    <string name="button_onionSkin">Number of Onion Skin</string>
+    <string name="nbOnionLabel">Number of Onion Skin</string>
+    <string name="freqOnionLabel">Frequency of Onion Skin</string>
 
 
 </resources>