简体   繁体   English

CustomView不会在scrollview中显示,但需要scrollview

[英]CustomView doesn't show in scrollview , but scrollview is needed

SOLUTION

the solution was to add the onMeasure function and rescale my drawing in my test app it didn't need it but because of the scrollview in the quiz app it was required. 解决方案是添加onMeasure功能并在我的测试应用程序中重新调整我的绘图它不需要它,但由于测验应用程序中的scrollview它是必需的。 this is the code i added: 这是我添加的代码:

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    setMeasuredDimension(widthMeasureSpec, resolveSize(800, heightMeasureSpec));

}

QUESTION

I'm making a quiz app and one of the questions has a map in it where the user needs to touch the stations he has to take, when pressed the stations get highlighted. 我正在制作一个测验应用程序,其中一个问题有一个地图,用户需要触摸他必须采取的电台,当按下电台时会突出显示。 I've made a customview for this in a test project. 我在一个测试项目中为此做了一个自定义视图。 There it works fine. 它工作得很好。 but now i want to get that custom view into my quiz app by programatically adding it to a linear layout, but the problem is that is doesn't show. 但现在我想通过以编程方式将其添加到线性布局中来将自定义视图放入我的测验应用程序中,但问题是它没有显示。

this is the custom view: 这是自定义视图:

public class BrushView extends View { 公共类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);
    }
}

and this is part of the activity: 这是活动的一部分:

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

the part were i try to add the custom view private void showStatementsAndAnswer(int id) { 该部分是我尝试添加自定义视图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());
    } 
}

im pretty new to android and never worked with customviews before so example code would help alot. 我很新的Android,从来没有使用自定义视图,所以示例代码将帮助很多。

thanks in advance!! 提前致谢!!

UPDATE : 更新

it looks like the scrollview in my xml is the thing that is causing the fact taht it isn't shown but i need that scrollview anyone any options or thought on how to solve that? 它看起来像我的xml中的scrollview是导致事实taht它没有显示,但我需要任何选项或思考如何解决这个问题的scrollview?

I found the solution myself but I'm leaving it on here for other people. 我自己找到了解决方案,但我将其留在这里供其他人使用。

SOLUTION

the solution was to add the onMeasure function and rescale my drawing in my test app it didn't need it but because of the scrollview in the quiz app it was required. 解决方案是添加onMeasure功能并在我的测试应用程序中重新调整我的绘图它不需要它,但由于测验应用程序中的scrollview它是必需的。 this is the code i added: 这是我添加的代码:

@Override
 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
 setMeasuredDimension(widthMeasureSpec, resolveSize(800, heightMeasureSpec));

}

have you added your container view to the custom view. 您是否已将容器视图添加到自定义视图中。 like addView(statementsLayout) after adding all view to statementsLayout. 在将所有视图添加到statementsLayout之后,就像addView(statementsLayout)一样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM