preliminary animation of targets

This commit is contained in:
Ruben 2016-08-17 13:17:04 +02:00
parent 57974468df
commit f26802be79
5 changed files with 179 additions and 4 deletions

View file

@ -32,7 +32,7 @@ import java.util.List;
*/ */
public class GamingActivity extends AppCompatActivity implements Detector.ImageListener, CameraDetector.CameraEventListener, Detector.FaceListener { 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; final int PERMISSIONS_REQUEST_CAMERA = 1;
@ -173,8 +173,17 @@ public class GamingActivity extends AppCompatActivity implements Detector.ImageL
cameraPreview.setLayoutParams(params); cameraPreview.setLayoutParams(params);
videoLayout.addView(cameraPreview,0); videoLayout.addView(cameraPreview,0);
currentScenario = new ScenarioAnger(); 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(); // startDetector();
} }

View file

@ -1,5 +1,9 @@
package com.rubenvandeven.emotionhero; 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 android.util.Log;
import com.affectiva.android.affdex.sdk.detector.Face; import com.affectiva.android.affdex.sdk.detector.Face;
@ -16,6 +20,8 @@ abstract public class Scenario {
float duration = 0; float duration = 0;
long startTime = 0;
ArrayList<Target> targets = new ArrayList<>(); ArrayList<Target> targets = new ArrayList<>();
abstract void createScenario(); abstract void createScenario();
@ -56,7 +62,7 @@ abstract public class Scenario {
*/ */
public void setTarget(Emotion emotion, float value, float timestamp) 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) if(timestamp > duration)
{ {
duration = timestamp; duration = timestamp;
@ -117,16 +123,58 @@ abstract public class Scenario {
*/ */
public boolean isWithinTime(float timestamp) public boolean isWithinTime(float timestamp)
{ {
Log.e("TEST", Float.toString(timestamp) + " " + Float.toString(duration));
return timestamp <= 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{ class ScenarioAnger extends Scenario{
void createScenario() void createScenario()
{ {
Log.e("TESTING", "CREATE SCENARIO!!!!"); Log.d(GamingActivity.LOG_TAG, "CREATE SCENARIO: anger");
setTarget(Emotion.ANGER, 10, 1); setTarget(Emotion.ANGER, 10, 1);
setTarget(Emotion.ANGER, 20, 2); setTarget(Emotion.ANGER, 20, 2);
setTarget(Emotion.ANGER, 40, 3); setTarget(Emotion.ANGER, 40, 3);

View file

@ -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) {}
}
}

View file

@ -30,6 +30,12 @@
<!-- This FrameLayout insets its children based on system windows using <!-- This FrameLayout insets its children based on system windows using
android:fitsSystemWindows. --> android:fitsSystemWindows. -->
<!--<view-->
<!--class="com.rubenvandeven.emotionhero.ScenarioView"-->
<!--android:layout_width="match_parent"-->
<!--android:id="@+id/scenarioView"-->
<!--android:layout_height="match_parent" />-->
<FrameLayout <FrameLayout
android:layout_width="match_parent" android:layout_width="match_parent"
@ -60,6 +66,7 @@
android:layout_width="1dp" android:layout_width="1dp"
android:layout_height="1dp" android:layout_height="1dp"
android:id="@+id/surfaceView" /> android:id="@+id/surfaceView" />
</FrameLayout> </FrameLayout>
</FrameLayout> </FrameLayout>

View file

@ -3,6 +3,7 @@
<color name="colorPrimary">#0000ff</color> <color name="colorPrimary">#0000ff</color>
<color name="colorPrimaryDark">#303F9F</color> <color name="colorPrimaryDark">#303F9F</color>
<color name="colorAccent">#FF4081</color> <color name="colorAccent">#FF4081</color>
<color name="colorScenarioLine">#FF0000</color>
<color name="black_overlay">#66000000</color> <color name="black_overlay">#66000000</color>
</resources> </resources>