解决方案
解决方案是添加onMeasure功能并在我的测试应用程序中重新调整我的绘图它不需要它,但由于测验应用程序中的scrollview它是必需的。这是我添加的代码:
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
setMeasuredDimension(widthMeasureSpec, resolveSize(800, heightMeasureSpec));
}
问题
我正在制作一个测验应用程序,其中一个问题中有一个地图,用户需要触摸他必须接受的电台,当按下电台时会突出显示。我在一个测试项目中为此做了一个自定义视图。它工作得很好。但现在我想通过以编程方式将其添加到线性布局中,将自定义视图放入我的测验应用程序,但问题是没有显示。
这是自定义视图:
公共类BrushView扩展了View {
private Paint brush = new Paint();
private Path path = new Path();
public Button btnEraseAll;
public LayoutParams params;
private Canvas mcanvas;
float mX;
float mY;
TextView mTVCoordinates;
ArrayList<Coordinates> coordinates = new ArrayList<Coordinates>();
Coordinates coord;
boolean alreadySelected = false;
int pos;
@SuppressLint("WrongCall")
public BrushView(Context context) {
super(context);
brush.setAntiAlias(true);
brush.setColor(Color.BLUE);
brush.setStyle(Paint.Style.STROKE);
brush.setStrokeJoin(Paint.Join.ROUND);
brush.setStrokeWidth(10f);
/* params = new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT);*/
}
@Override
public boolean onTouchEvent(MotionEvent event) {
float pointX = event.getX();
float pointY = event.getY();
// Checks for the event that occurs
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
path.moveTo(pointX, pointY);
mX = event.getX();
mY = event.getY();
Log.d("coordinates","x="+mX+ " y="+mY);
if (mTVCoordinates != null) {
mTVCoordinates.setText("X :" + mX + " , " + "Y :" + mY);
}
if ((mX > 200 && mX < 240) && (mY > 210 && mY < 235)) {
// Draw the circle at (x,y) with radius 15
mX = 230;
mY = 220;
if (coordinates.isEmpty()) {
brush.setColor(Color.GREEN);
path.addCircle(mX, mY, 15, Path.Direction.CW);
coord = new Coordinates(mX, mY, path, Color.GREEN);
coordinates.add(coord);
path = new Path();
} else {
for (int i = 0; i < coordinates.size(); i++) {
Coordinates c = coordinates.get(i);
if (c.getX() == mX && c.getY() == mY) {
alreadySelected = true;
pos = i;
}
}
if (alreadySelected) {
coordinates.remove(pos);
} else{
brush.setColor(Color.GREEN);
path.addCircle(mX, mY, 15, Path.Direction.CW);
coord = new Coordinates(mX, mY, path, Color.GREEN);
coordinates.add(coord);
path = new Path();
}
}
}
if ((mX > 330 && mX < 360) && (mY > 125 && mY < 150)) {
// Draw the circle at (x,y) with radius 15
mX = 340;
mY = 140;
if (coordinates.isEmpty()) {
brush.setColor(Color.GREEN);
path.addCircle(mX, mY, 15, Path.Direction.CW);
coord = new Coordinates(mX, mY, path, Color.GREEN);
coordinates.add(coord);
path = new Path();
} else {
for (int i = 0; i < coordinates.size(); i++) {
Coordinates c = coordinates.get(i);
if (c.getX() == mX && c.getY() == mY) {
alreadySelected = true;
pos = i;
}
}
if (alreadySelected) {
coordinates.remove(pos);
} else{
brush.setColor(Color.GREEN);
path.addCircle(mX, mY, 15, Path.Direction.CW);
coord = new Coordinates(mX, mY, path, Color.GREEN);
coordinates.add(coord);
path = new Path();
}
}
}
return true;
case MotionEvent.ACTION_UP:
postInvalidate();
break;
default:
return false;
}
// Force a view to draw again
postInvalidate();
return false;
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
alreadySelected = false;
Bitmap srcBitmapLocal = GlobalStatic.img;
canvas.drawBitmap(srcBitmapLocal, 0, 0, brush);
for (Coordinates c : coordinates) {
brush.setColor(c.getC());
canvas.drawPath(c.getP(), brush);
}
}
这是活动的一部分:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
ctx = getBaseContext();
activity = this;
statementsLayout = (LinearLayout) findViewById(R.id.linearlayoutUp);
btn_next = (Button) findViewById(R.id.btn_next);
btn_previous = (Button) findViewById(R.id.btn_previous);
btn_next.setOnClickListener(new ButtonHandler());
btn_previous.setOnClickListener(new ButtonHandler());
GlobalStatic.text = (TextView) this.findViewById(R.id.timer);
countDownTimer = new MyCountDownTimer(startTime, interval);
GlobalStatic.text.setText(GlobalStatic.text.getText() + String.valueOf(startTime / 1000));
countDownTimer.start();
setScreen(teller);
}
该部分是我尝试添加自定义视图 private void showStatementsAndAnswer(int id){
String statement_text;
Bitmap statement_img;
int statement_displayType;
ArrayList<Statements> statementPerQuestion;
ArrayList<Answer> answersPerStatement;
statementPerQuestion = DbAccess.getStatementsPerQuestions(id);
totalStatementsPerQuestion = statementPerQuestion.size();
s = statementPerQuestion.get(statementcount); // get statement at position b
statement_text = s.getText();
statement_img = s.getImage();
statement_displayType = s.getDisplaytype();
// show the elements that are not null
if (statement_text != null) {
TextView S_text = new TextView(this);
S_text.setText(statement_text);
LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
params.setMargins(0, 40, 0, 35);
params.gravity = Gravity.CENTER;
S_text.setLayoutParams(params);
S_text.setTypeface(Typeface.create("sans-serif", Typeface.BOLD));
S_text.setTextSize(18);
statementsLayout.addView(S_text);
}
if (statement_img != null && statement_displayType != 5) {
ImageView S_img = new ImageView(this);
S_img.setImageBitmap(statement_img);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
layoutParams.gravity = Gravity.CENTER;
layoutParams.bottomMargin = 25;
S_img.setLayoutParams(layoutParams);
statementsLayout.addView(S_img);
}
//here is where i try to get my custom view shown!!!
if(statement_img != null && statement_displayType == 5){
GlobalStatic.img = statement_img;
BrushView S_map = new BrushView(this);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
S_map.setLayoutParams(layoutParams);
statementsLayout.addView(S_map);
}
statementId = s.getId(); // add the statementID's to an array to get the answers later
answersPerStatement = DbAccess.getAnswerPerStatement(s.getId()); // get possible answers per statement
// show answer depending on what the displaytype is.
if (statement_displayType == 1) {
makeSpinner(1, answersPerStatement);
} else if (statement_displayType == 3) {
makeRadioButtons(answersPerStatement);
} else if (statement_displayType == 2) {
TextView[] views = new TextView[answersPerStatement.size()];
for(int i = 0; i < answersPerStatement.size(); i++)
{
views[i] = new TextView(this);
a = answersPerStatement.get(i);
views[i].setText(a.getText());
views[i].setId(a.getId()); // give the rb the same id as the answer in the db
views[i].setTypeface(Typeface.create("sans-serif", Typeface.NORMAL));
views[i].setTextSize(20);
}
populateText(statementsLayout, views, getBaseContext());
}
}
我对Android很新,从未使用自定义视图,因此示例代码可以帮助很多。
提前感谢!!
更新:
看起来像我的xml中的scrollview是导致事实没有显示的事情,但我需要任何滚动视图任何选项或想法如何解决?
答案 0 :(得分:2)
我自己找到了解决方案,但我将其留在这里供其他人使用。
的解强> 的
解决方案是添加onMeasure功能并在我的测试应用程序中重新调整我的绘图它不需要它,但由于测验应用程序中的scrollview它是必需的。这是我添加的代码:
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
setMeasuredDimension(widthMeasureSpec, resolveSize(800, heightMeasureSpec));
}
答案 1 :(得分:0)
您是否已将容器视图添加到自定义视图中。 像将所有视图添加到statementsLayout之后的addView(statementsLayout)。