如何在主要活动中使用自定义视图

时间:2018-08-25 13:36:54

标签: android android-view

我正在开发一个随机显示形状的游戏,我得到了这个类,但是我不知道如何从我的GameFragment中调用自定义视图。我想知道是否有人可以帮助我实现目标。

CustomView.java

public class CustomViews extends View {

    private final int SQUARE_SIZE = 500;
    private RectF rectF;
    private Paint paint;
    private boolean CIRCLE = false;
    private boolean RECTANGLE = false;

    public CustomViews(Context context) {
        super(context);

        init(null);
    }

    public CustomViews(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(attrs);
    }

    public CustomViews(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(attrs);
    }

    public CustomViews(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init(attrs);
    }

    private void init(@Nullable AttributeSet set) {

        rectF = new RectF();
        paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        randomColorGenerator();
    }

    public void rect(Canvas canvas) {
        int height = canvas.getHeight() / 2;
        int width = canvas.getWidth() / 2;
        rectF.set(width - 200, height - 200, width + 200, height + 200);
    }

    private void circle(Canvas canvas) {

        float cx;
        float cy;
        float radius = 200;

        cx = canvas.getWidth() / 2;
        cy = canvas.getWidth() / 2;
        canvas.drawCircle(cx, cy, radius, paint);

    }

    private void randomColorGenerator() {
        Random random = new Random();
        int choices = 3;

        switch (random.nextInt(choices)) {
            case 0:
                paint.setColor(Color.GREEN);
                break;
            case 1:
                paint.setColor(Color.RED);
                break;
            case 2:
                paint.setColor(Color.BLACK);
                break;
        }
    }

    public void randomShapeGenerator(Canvas canvas) {
        Random random = new Random();
        int numberOfMethods = 2;

        switch (random.nextInt(numberOfMethods)) {
            case 0:
                rect(canvas);
                rectSelected();
                break;
            case 1:
                circle(canvas);
                circleSelected();
                break;

        }
    }

    public boolean circleSelected(){
        RECTANGLE = false;
        return CIRCLE = true;
    }

     public boolean rectSelected( ) {
        CIRCLE = false;
        return RECTANGLE = true;
    }

    @Override
    public void onDraw(Canvas canvas) {

        randomShapeGenerator(canvas);
        canvas.drawRect(rectF, paint);
    }
}

现在我需要从我的GameFragment中调用它,那里有一些按钮,每当您单击它们时,都必须生成一个新的随机形状。有人知道吗?

1 个答案:

答案 0 :(得分:1)

假设您的片段中有一个ID为LinearLayout的{​​{1}},那么这可能有助于您以编程方式添加自定义视图:

main_content

layoutParams可能还会根据您的需要进行调整。

您必须考虑要在何处显示customView及其尺寸。例如,如果您具有Button btn = findViewById(R.id.yourbutton); btn.setOnClickListener(v -> { LinearLayout main_layout = findViewById(R.id.main_content); CustomViews customView = new CustomViews(this); customView.setLayoutParams(new LinearLayout.LayoutParams( LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT)); main_layout.addView(customView); }); 方向的线性布局,则可以通过以下方式添加视图:

vertical

这将在另一个下面显示多个customView(直到达到屏幕的高度)。 实际上,main_layout.addView(customView, 400, 400); 必须稍作修改(这里有两种更改方法):

CustomViews