From f26802be798c1666139b299c8ee8128fc87b491a Mon Sep 17 00:00:00 2001 From: Ruben Date: Wed, 17 Aug 2016 13:17:04 +0200 Subject: [PATCH] preliminary animation of targets --- .../emotionhero/GamingActivity.java | 11 +- .../rubenvandeven/emotionhero/Scenario.java | 54 ++++++++- .../emotionhero/ScenarioView.java | 110 ++++++++++++++++++ app/src/main/res/layout/activity_gaming.xml | 7 ++ app/src/main/res/values/colors.xml | 1 + 5 files changed, 179 insertions(+), 4 deletions(-) create mode 100644 app/src/main/java/com/rubenvandeven/emotionhero/ScenarioView.java diff --git a/app/src/main/java/com/rubenvandeven/emotionhero/GamingActivity.java b/app/src/main/java/com/rubenvandeven/emotionhero/GamingActivity.java index 2490e2a..3db3504 100644 --- a/app/src/main/java/com/rubenvandeven/emotionhero/GamingActivity.java +++ b/app/src/main/java/com/rubenvandeven/emotionhero/GamingActivity.java @@ -32,7 +32,7 @@ import java.util.List; */ public class GamingActivity extends AppCompatActivity implements Detector.ImageListener, CameraDetector.CameraEventListener, Detector.FaceListener { - final String LOG_TAG = "EmotionHero"; + final static String LOG_TAG = "EmotionHero"; final int PERMISSIONS_REQUEST_CAMERA = 1; @@ -173,8 +173,17 @@ public class GamingActivity extends AppCompatActivity implements Detector.ImageL cameraPreview.setLayoutParams(params); videoLayout.addView(cameraPreview,0); + currentScenario = new ScenarioAnger(); + ScenarioView scenarioView; + scenarioView = new ScenarioView(this, currentScenario); + RelativeLayout.LayoutParams scenarioViewParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT); + videoLayout.addView(scenarioView, 1, scenarioViewParams); +// new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT) +// videoLayout.addView(scenarioView, 200, 100); + + // startDetector(); } diff --git a/app/src/main/java/com/rubenvandeven/emotionhero/Scenario.java b/app/src/main/java/com/rubenvandeven/emotionhero/Scenario.java index c6c1a2d..b8f7d0b 100644 --- a/app/src/main/java/com/rubenvandeven/emotionhero/Scenario.java +++ b/app/src/main/java/com/rubenvandeven/emotionhero/Scenario.java @@ -1,5 +1,9 @@ package com.rubenvandeven.emotionhero; +import android.graphics.Bitmap; +import android.graphics.Canvas; +import android.graphics.Color; +import android.graphics.Paint; import android.util.Log; import com.affectiva.android.affdex.sdk.detector.Face; @@ -16,6 +20,8 @@ abstract public class Scenario { float duration = 0; + long startTime = 0; + ArrayList targets = new ArrayList<>(); abstract void createScenario(); @@ -56,7 +62,7 @@ abstract public class Scenario { */ public void setTarget(Emotion emotion, float value, float timestamp) { - Log.e("SET", Float.toString(timestamp) + " " + Float.toString(duration)); +// Log.e(GamingActivity.LOG_TAG, "Set target:" + Float.toString(timestamp) + " " + Float.toString(duration)); if(timestamp > duration) { duration = timestamp; @@ -117,16 +123,58 @@ abstract public class Scenario { */ public boolean isWithinTime(float timestamp) { - Log.e("TEST", Float.toString(timestamp) + " " + Float.toString(duration)); return timestamp <= duration; } + public void drawOnCanvas(Canvas canvas) { +// canvas; + Paint paint = new Paint(); + paint.setColor(Color.RED); + float height = (float) canvas.getHeight(); + float pix_per_sec = height / duration; + + float sec_height = canvas.getHeight() * (float) 0.2; // each second is 20% of canvas height + + float diff_y; + // if not started, don't move the labels, if started, move them by diff_y + if(startTime == 0) { + diff_y = 0; + start(); + } else { + float diff_t = ((System.currentTimeMillis() - startTime)) / (float) 1000; + Log.d("TIME", Float.toString(diff_t)); + if(diff_t > duration) { // never larger than scenario duration + diff_t = duration; + } + diff_y = sec_height * diff_t; + } + +// bottom at 80%; + float bottomline_height = height * (float) 0.8; + + canvas.drawLine(0, bottomline_height, canvas.getWidth(), bottomline_height, paint); + canvas.drawLine(0, bottomline_height+1, canvas.getWidth(), bottomline_height+1, paint); + + for(Target target: targets) { + float y_pos = bottomline_height - (target.timestamp * sec_height); + String target_text = target.emotion.toString() + " " + target.value + "%"; + canvas.drawText(target_text, canvas.getWidth()/2, y_pos + diff_y , paint); + + } + + } + + public void start() + { + startTime = System.currentTimeMillis(); + } + } class ScenarioAnger extends Scenario{ void createScenario() { - Log.e("TESTING", "CREATE SCENARIO!!!!"); + Log.d(GamingActivity.LOG_TAG, "CREATE SCENARIO: anger"); setTarget(Emotion.ANGER, 10, 1); setTarget(Emotion.ANGER, 20, 2); setTarget(Emotion.ANGER, 40, 3); diff --git a/app/src/main/java/com/rubenvandeven/emotionhero/ScenarioView.java b/app/src/main/java/com/rubenvandeven/emotionhero/ScenarioView.java new file mode 100644 index 0000000..7448bc5 --- /dev/null +++ b/app/src/main/java/com/rubenvandeven/emotionhero/ScenarioView.java @@ -0,0 +1,110 @@ +package com.rubenvandeven.emotionhero; + +import android.content.Context; +import android.graphics.Canvas; +import android.graphics.Paint; +import android.graphics.PixelFormat; +import android.util.AttributeSet; +import android.util.Log; +import android.view.SurfaceHolder; +import android.view.SurfaceView; + +/** + * Created by ruben on 16/08/16. + */ + +public class ScenarioView extends SurfaceView implements SurfaceHolder.Callback { + + private PanelThread _thread; + + private Scenario _scenario; + + + // see: http://blog.danielnadeau.io/2012/01/android-canvas-beginners-tutorial.html + class PanelThread extends Thread { + private SurfaceHolder _surfaceHolder; + private ScenarioView _panel; + private boolean _run = false; + + + public PanelThread(SurfaceHolder surfaceHolder, ScenarioView panel) { + _surfaceHolder = surfaceHolder; + _panel = panel; + } + + + public void setRunning(boolean run) { //Allow us to stop the thread + _run = run; + } + + + @Override + public void run() { + Canvas c; + while (_run) { //When setRunning(false) occurs, _run is + c = null; //set to false and loop ends, stopping thread + + try { + c = _surfaceHolder.lockCanvas(null); + synchronized (_surfaceHolder) { + //Insert methods to modify positions of items in onDraw() + postInvalidate(); + } + } finally { + if (c != null) { + _surfaceHolder.unlockCanvasAndPost(c); + } + } + } + } + } + + public ScenarioView(Context context, Scenario s) { + super(context); + getHolder().addCallback(this); + _scenario = s; + } + + + @Override + public void onDraw(Canvas canvas) { + //do drawing stuff here. +// Log.e("TEST2", "Jaa!"); + _scenario.drawOnCanvas(canvas); + } + + + @Override + public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { + + + } + + @Override + public void surfaceCreated(SurfaceHolder holder) { + setWillNotDraw(false); //Allows us to use invalidate() to call onDraw() + + this.setZOrderOnTop(true); + holder.setFormat(PixelFormat.TRANSPARENT); + +// Log.e("TEST2", "Jaa2!"); + _thread = new PanelThread(getHolder(), this); //Start the thread that + _thread.setRunning(true); //will make calls to + _thread.start(); //onDraw() + } + + + @Override + public void surfaceDestroyed(SurfaceHolder holder) { + try { + if(_thread != null) { + _thread.setRunning(false); //Tells thread to stop + _thread.join(); //Removes thread from mem. + } + } catch (InterruptedException e) {} + } + + + +} + diff --git a/app/src/main/res/layout/activity_gaming.xml b/app/src/main/res/layout/activity_gaming.xml index 7f866ce..42f8651 100644 --- a/app/src/main/res/layout/activity_gaming.xml +++ b/app/src/main/res/layout/activity_gaming.xml @@ -30,6 +30,12 @@ + + + + + + + diff --git a/app/src/main/res/values/colors.xml b/app/src/main/res/values/colors.xml index a124528..30e8f1a 100644 --- a/app/src/main/res/values/colors.xml +++ b/app/src/main/res/values/colors.xml @@ -3,6 +3,7 @@ #0000ff #303F9F #FF4081 + #FF0000 #66000000