如何在运行时更改为自定义视图中的参数?

时间:2015-01-16 09:00:51

标签: android android-view android-lifecycle android-overlay

我正在制作一个绘图板应用程序。我已经通过这样的自定义视图完成了绘图功能:

public class DrawView extends View implements OnTouchListener {

    private Paint bmPaint = new Paint();
    private Paint drawPaint = new Paint();
    private Path path = new Path();
    private Canvas cv = null;
    private Bitmap bm = null;
    private Drawable d;
    private boolean firstTimeThru = true;

    public DrawView(Context context) {
        super(context);
        init(context);
    }

    public DrawView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public void init(Context ctx) {
        setFocusable(true);
        setFocusableInTouchMode(true);
        this.setOnTouchListener(this);
    }

    @Override
    public void onDraw(Canvas canvas) {
        // Set everything up the first time anything gets drawn:
        if (firstTimeThru) {
            firstTimeThru = false;
            //d = getResources().getDrawable(R.drawable.zone0_over);
            d = new ColorDrawable(Color.TRANSPARENT);

            // Just quickly fill the view with a red mask:          
            canvas.drawColor(Color.TRANSPARENT);

            // Create a new bitmap and canvas and fill it with a red mask:
            bm = Bitmap.createBitmap(canvas.getWidth(), canvas.getHeight(),Config.ARGB_8888);           
            cv = new Canvas();
            cv.setBitmap(bm);
            d.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
            d.draw(cv);

            // Specify that painting will be with fat strokes:
            drawPaint.setStyle(Paint.Style.STROKE);
            drawPaint.setColor(Color.RED);
            drawPaint.setStrokeWidth(canvas.getWidth() / 200); // default 15

            // Specify that painting will clear the pixels instead of paining new ones:
            drawPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.ADD));
        }

        cv.drawPath(path, drawPaint);
        canvas.drawBitmap(bm, 0, 0, bmPaint);

        super.onDraw(canvas);
    }

    public boolean onTouch(View view, MotionEvent event) {
        float xPos = event.getX();
        float yPos = event.getY();

        switch (event.getAction()) {
            // Set the starting position of a new line:
            case MotionEvent.ACTION_DOWN:
                path.moveTo(xPos, yPos);
                invalidate();
            return true;

            // Draw a line to the ending position:
            case MotionEvent.ACTION_MOVE:
                path.lineTo(xPos, yPos);
                invalidate();
            break;

            case MotionEvent.ACTION_UP:
            break;

            default:
            return false;
        }

        return true;
    }   

}

像这样的xml:

<ImageView
    android:id="@+id/change_color_btn"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

<com.myapp.tool.DrawView
    android:id="@+id/draw"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

请注意,我在onDraw函数中设置了线条大小和线条颜色,它当前是红色,但是,我想按一个按钮并将颜色更改为黄色,如何在运行中设置参数时间?

由于

3 个答案:

答案 0 :(得分:2)

将一个成员变量和setter函数添加到DrawView类:

private int mLineColor = Color.RED;
public void setLineColor(int color)
{
    mLineColor = color;
    drawPaint.setColor(mLineColor);
}

更改你的onDraw以使用它:

drawPaint.setColor(mLineColor);

然后你可以在Button onClick()处理程序中使用自定义类的Activity设置它:

DrawView drawview = (DrawView)findViewById(R.id.draw);
drawView.setLineColor(Color.YELLOW);
drawView.invalidate(); // trigger a redraw

同样适用于行程宽度。

答案 1 :(得分:1)

在布局中添加一个按钮,用于切换颜色并向其添加clicklistener。然后,您可以更改路径的颜色。你想要当前路径切换颜色还是仅切换新颜色?

答案 2 :(得分:0)

field等自定义视图中获取mColor值,并为字段值创建setter and getter,然后在创建自定义视图后设置颜色,如下所示:

private int mColor;

public int getmColor() {
    return mColor;
}

public void setmColor(int mColor) {
    this.mColor = mColor;
    this.invalidate();
}