Currently I'm making some changes in app for android. It currently works nicely in portrait mode, but not in landscape mode. The thing is that when in landscape mode, text on my canvas exceeds height of screen (therefore, it's not readable). I need to add scrolls on canvas if canvas size exceeds screen size. (I've already made changes to .xml to make layout scrollView but it doesn't help since canvas is set to be the size of the screen, and not the size of it's content)
Ill paste the entire class, but the important part is onDraw. This class is used from main activity, and then main activity. Question is pretty straight forward. Is there a way to dynamically add scrolls if canvas is bigger than screens? Or should I use another activity for writing results and canvas only for drawing? If need be, here's link to entire project and there is zip with all the code (app is really not that big): http://www.yorku.ca/mack/FittsLawSoftware/
package ca.yorku.cse.mack.fittstouch;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;
import android.widget.Toast;
public class ExperimentPanel extends View
{
final int START_TEXT_SIZE = 20; // may need to fiddle with this, depending on device
final int START_CICLE_DIAMETER = 53; // x pixelDensity = one-third inch
final int GAP_BETWEEN_LINES = 6;
Target[] targetSet;
Target toTarget; // the target to select
Target fromTarget; // the source target from where the trial began
Target startCircle;
float panelWidth;
float panelHeight;
float d; // diameter of start circle (also used for positioning circle and text)
float textSize;
float gap;
boolean waitStartCircleSelect, done;
String mode;
Paint targetPaint, targetRimPaint, normalPaint, startPaint;
String[] resultsString = {"Tap to begin"};
public ExperimentPanel(Context contextArg)
{
super(contextArg);
initialize(contextArg);
}
public ExperimentPanel(Context contextArg, AttributeSet attrs)
{
super(contextArg, attrs);
initialize(contextArg);
}
public ExperimentPanel(Context contextArg, AttributeSet attrs, int defStyle)
{
super(contextArg, attrs, defStyle);
initialize(contextArg);
}
// things that can be initialized from within this View
private void initialize(Context c)
{
this.setBackgroundColor(Color.LTGRAY);
float pixelDensity = c.getResources().getDisplayMetrics().density;
d = START_CICLE_DIAMETER * pixelDensity;
startCircle = new Target(Target.CIRCLE, d, d, d, d, Target.NORMAL);
textSize = START_TEXT_SIZE * pixelDensity;
gap = GAP_BETWEEN_LINES * pixelDensity;
targetPaint = new Paint();
targetPaint.setColor(0xffffaaaa);
targetPaint.setStyle(Paint.Style.FILL);
targetPaint.setAntiAlias(true);
targetRimPaint = new Paint();
targetRimPaint.setColor(Color.RED);
targetRimPaint.setStyle(Paint.Style.STROKE);
targetRimPaint.setStrokeWidth(2);
targetRimPaint.setAntiAlias(true);
normalPaint = new Paint();
normalPaint.setColor(0xffff9999); // lighter red (to minimize distraction)
normalPaint.setStyle(Paint.Style.STROKE);
normalPaint.setStrokeWidth(2);
normalPaint.setAntiAlias(true);
startPaint = new Paint();
startPaint.setColor(0xff0000ff);
startPaint.setStyle(Paint.Style.FILL);
startPaint.setAntiAlias(true);
startPaint.setTextSize(textSize);
}
@Override
protected void onDraw(Canvas canvas)
{
int tmp1=0;
float tmp2 =0;
if (waitStartCircleSelect) // draw start circle and prompt/results string
{
canvas.drawCircle(startCircle.xCenter, startCircle.yCenter, startCircle.width / 2f,
startPaint);
for (int i = 0; i < resultsString.length; ++i){
canvas.drawText(resultsString[i], d / 2, d / 2 + 2 * startCircle.width / 2f + (i + 1) * (textSize + gap), startPaint);
tmp1=i;
}
} else if (!done) // draw task targets
{
for (Target value : targetSet)
{
if (mode.equals("1D"))
canvas.drawRect(value.r, normalPaint);
else // 2D
canvas.drawOval(value.r, normalPaint);
}
// draw target to select last (so it is on top of any overlapping targets)
if (mode.equals("1D"))
{
canvas.drawRect(toTarget.r, targetPaint);
canvas.drawRect(toTarget.r, targetRimPaint);
} else // 2D
{
canvas.drawOval(toTarget.r, targetPaint);
canvas.drawOval(toTarget.r, targetRimPaint);
}
}
invalidate(); // will cause onDraw to run again immediately
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
setMeasuredDimension((int) panelWidth, (int) panelHeight);
}
}