目前我正在对Android进行一些更改。它目前在纵向模式下工作得很好,但在横向模式下却不行。问题是,在横向模式下,我的画布上的文字超出了屏幕的高度(因此,它不可读)。如果画布大小超过屏幕大小,我需要在画布上添加滚动。 (我已经对.xml进行了更改以使布局成为scrollView,但它没有帮助,因为画布设置为屏幕的大小,而不是它的大小)
我会粘贴整个班级,但重要的部分是onDraw。此类用于主要活动,然后是主要活动。问题很简单。如果画布大于屏幕,有没有办法动态添加滚动?或者我应该使用另一个活动来编写结果和画布仅用于绘图?如果需要,这里链接到整个项目,并且有所有代码的zip(应用程序真的不是那么大): 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);
}
}