简体   繁体   中英

Android - How do I add button from code on top of canvas. I don't have layout to grab

I've got an app that works like Paint. Everything works fine but what I want to do is adding couple buttons, that would change my "brush" color and width. But since I don't have xml file, I got no idea how to do that. I know that I can do something like this: container.addView(button) but I don't have layout to grab and add button to. At least I don't know how to do that.

Here is my code:

public class Paint extends Activity {

    Context context;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        PaintView paintView = new PaintView(this);
        setContentView(paintView);

    }
}

PaintView class:

package com.example.centrummultimedialne;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup.LayoutParams;

public class PaintView extends View {

    public LayoutParams params;
    private Path path = new Path();
    private Paint brush = new Paint();



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

        brush.setAntiAlias(true);
        brush.setColor(Color.MAGENTA);
        brush.setStyle(Paint.Style.STROKE);
        brush.setStrokeJoin(Paint.Join.ROUND);
        brush.setStrokeWidth(8f);

        params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        float pointX = event.getX();
        float pointY = event.getY();

        switch(event.getAction()){
            case MotionEvent.ACTION_DOWN:
                path.moveTo(pointX, pointY);
                return true;
            case MotionEvent.ACTION_MOVE:
                path.lineTo(pointX, pointY);
                break;
            default:
                return false;
        }
        postInvalidate();
        return false;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        canvas.drawPath(path, brush);
    }
}

I suggest you to use a xml layout. It's a lot easier.

main.xml //Put this inside src/main/res/layout/

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <com.example.centrummultimedialne.PaintView
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:orientation="vertical"
        android:layout_weight="1.0"
        android:id="@+id/paintView"/>

    <LinearLayout
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:orientation="horizontal">

        <Button
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:text="Color"
            android:layout_weight="1.0"
            android:id="@+id/btn1"/>

        <Button
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:text="Stroke"
            android:layout_weight="1.0"
            android:id="@+id/btn2"/>

        <Button
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:text="Button"
            android:layout_weight="1.0"
            android:id="@+id/btn3"/>

    </LinearLayout>

</LinearLayout>

Change setContentView(paintView); in Paint class to setContentView(R.layout.main); Modify your PaintView's constructors to accept external layout.

public PaintView(Context context)
{
        super(context, null);
    init(context);
}
public PaintView(Context context, AttributeSet attrs)
{
        super(context, attrs, 0);
    init(context);
}
public PaintView(Context context, AttributeSet attrs, int defStyleAttr)
{
        super(context, attrs, defStyleAttr, 0);
    init(context);
}
public PaintView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)
{
        super(context, attrs, defStyleAttr, defStyleRes);
    init(context);
}

private void init()
{
        brush.setAntiAlias(true);
        brush.setColor(Color.MAGENTA);
        brush.setStyle(Paint.Style.STROKE);
        brush.setStrokeJoin(Paint.Join.ROUND);
        brush.setStrokeWidth(8f);

        params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
}

Later in Paint class, set all these buttons a listener.

public class MainActivity extends Activity implements View.OnClickListener
{
    PaintView paintView;
    
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        paintView = findViewById(R.id.paintView);
        ((Button)findViewById(R.id.btn1)).setOnClickListener(this);
        ((Button)findViewById(R.id.btn2)).setOnClickListener(this);
        ((Button)findViewById(R.id.btn3)).setOnClickListener(this);
    }

    @Override
    public void onClick(View btn)
    {
        switch(btn.getId()) {
            case R.id.btn1: paintView.changeColor(Color.BLUE); break;
            case R.id.btn2: paintView.setStroke(10F); break;
        }
    }
}

Now in PaintView class, you can implement changeColor(int Color) or any other methods you like.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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