How to implement a Scroll in a Custom View

0

I created a custom view that needs to have a vertical scroll to show all items that are drawn using the "onDraw" method. Can anyone help me with this problem?

public class MinhaView extends View {

private Paint paintLinha, paintHora, paintEstacao, paintTraco, paintCirculoFill, paintCirculoStroke;

private int x = 100;
private int y = 50;

public static int eixoYinic = 50;

private int direcaoX = 1;
private int direcaoY = 1;

private int raioCirculo = 16;

private int tamanhoLinha;

private ArrayList<Paragens> paragensList;

private boolean isInit;


public MinhaView(Context context, @Nullable AttributeSet attrs) {
    super(context, attrs);

    paintLinha = new Paint();
    paintLinha.setAntiAlias(true);
    paintLinha.setColor(ContextCompat.getColor(context, R.color.verde_1));
    paintLinha.setStyle(Paint.Style.STROKE);
    paintLinha.setStrokeWidth(10f);

    paintTraco = new Paint();
    paintTraco.setAntiAlias(true);
    paintTraco.setColor(ContextCompat.getColor(context, R.color.preto_1));
    paintTraco.setStyle(Paint.Style.STROKE);
    paintTraco.setStrokeWidth(2f);

    paintHora = new Paint();
    paintHora.setAntiAlias(true);
    paintHora.setTextSize(20);
    paintHora.setColor(ContextCompat.getColor(context, R.color.preto_1));

    paintEstacao = new Paint();
    paintEstacao.setAntiAlias(true);
    paintEstacao.setTextSize(20);
    paintEstacao.setColor(ContextCompat.getColor(context, R.color.preto_1));

    paintCirculoFill = new Paint();
    paintCirculoFill.setAntiAlias(true);
    paintCirculoFill.setColor(ContextCompat.getColor(context, R.color.branco_1));
    paintCirculoFill.setStyle(Paint.Style.FILL);

    paintCirculoStroke = new Paint();
    paintCirculoStroke.setAntiAlias(true);
    paintCirculoStroke.setColor(ContextCompat.getColor(context, R.color.colorAccent));
    paintCirculoStroke.setStyle(Paint.Style.STROKE);
    paintCirculoStroke.setStrokeWidth(2f);
}

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    if (isInit) {

        // Desenha as Linhas Horizontais e as Paragens:
        for (int i = 0; i < paragensList.size(); i++) {

            canvas.drawLine(80, paragensList.get(i).getParagemEixoY(), 120, paragensList.get(i).getParagemEixoY(), paintTraco);

            canvas.drawText(paragensList.get(i).getHoraPartida(), 20, paragensList.get(i).getParagemEixoY() + 8,  paintHora);

            canvas.drawText(paragensList.get(i).getNomeEstacao(), 140, paragensList.get(i).getParagemEixoY() + 8,  paintEstacao);
        }

        // Desenha a Linha Vertical:
        canvas.drawLine(100, 50, 100, tamanhoLinha, paintLinha);

        // Incrementa o eixo Y (vertical):
        y = y + direcaoY;

        // Desenha o Circulo:
        canvas.drawCircle(x, y, raioCirculo, paintCirculoFill);
        canvas.drawCircle(x, y, raioCirculo, paintCirculoStroke);
    }

    if (y < tamanhoLinha) {
        invalidate();
    }
}

public void setQuantidadeParagens(ArrayList<Paragens> paragensList) {

    this.paragensList = paragensList;

    tamanhoLinha = eixoYinic * paragensList.size();

    isInit = true;
}

}

public class MainActivity extends AppCompatActivity {

private MinhaView minhaView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    minhaView = (MinhaView) findViewById(R.id.minhaView1);

    ArrayList<Paragens> paragensList = new ArrayList<>();

    int paragemEixoY = MinhaView.eixoYinic;

    for (int i = 0; i < 40; i++) {

        paragensList.add(new Paragens("09:30","Paragem_" + i,  paragemEixoY));

        paragemEixoY += MinhaView.eixoYinic;
    }

    minhaView.setQuantidadeParagens(paragensList);
}

}

public class Paragens {

private String horaPartida;
private String nomeEstacao;
private int paragemEixoY;

public Paragens(String horaPartida, String nomeEstacao, int paragemEixoY) {
    this.horaPartida = horaPartida;
    this.nomeEstacao = nomeEstacao;
    this.paragemEixoY = paragemEixoY;
}

public String getHoraPartida() {
    return horaPartida;
}

public void setHoraPartida(String horaPartida) {
    this.horaPartida = horaPartida;
}

public String getNomeEstacao() {
    return nomeEstacao;
}

public void setNomeEstacao(String nomeEstacao) {
    this.nomeEstacao = nomeEstacao;
}

public int getParagemEixoY() {
    return paragemEixoY;
}

public void setParagemEixoY(int paragemEixoY) {
    this.paragemEixoY = paragemEixoY;
}

}

<?xml version="1.0" encoding="utf-8"?>

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <com.example.menda.testes_canvas.MinhaView
            android:id="@+id/minhaView1"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

    </LinearLayout>

</ScrollView>

    
asked by anonymous 05.04.2018 / 18:20

0 answers