绘制可调整大小的矩形的后续操作

时间:2013-08-30 22:03:23

标签: java android

以下是上一个问题: How to create a resizable rectangle with user touch events on Android?

忘记移动矩形,矩形本身不会被绘制。以下是我的代码。我没有任何错误,但我根本看不到矩形或角落。

public class DrawView extends View {
    private Paint paint;
    private ArrayList<ColorBall> colorballs = new ArrayList<ColorBall>();
    private int groupID = -1;
    private int ballID = 0;
    private Canvas canvas;
    Point[] pts = new Point[4];
    int left, top, right, bottom;
    private Bitmap bm;
    private BitmapDrawable bmDrawable;

    public DrawView(Context context) {
        super(context);
        paint = new Paint();
        setFocusable(true);
        canvas = new Canvas();
    }

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

    public DrawView(Context context, AttributeSet attrs)
    {
        super(context, attrs);
        paint = new Paint();
        setFocusable(true);
        canvas = new Canvas();
    }

    // -------------------------------------------------------------------//

    @Override
    protected void onDraw(Canvas canvas) {
        // TODO Auto-generated method stub
        //super.onDraw(canvas);
        this.setBackgroundColor(Color.LTGRAY);
        paint.setStrokeWidth(5);
        paint.setColor(Color.GREEN);

        if (pts[3] == null) {
            return;
        }
        int left, top, right, bottom;
        left = pts[0].x;
        top = pts[0].y;
        right = pts[0].x;
        bottom = pts[0].y;

        for (int i = 1; i < pts.length; i++) {
            left = left > pts[i].x ? pts[i].x : left;
            top = top > pts[i].y ? pts[i].y : top;
            right = right < pts[i].x ? pts[i].x : right;
            bottom = bottom < pts[i].y ? pts[i].y : bottom;
        }

        paint.setAntiAlias(true);
        paint.setDither(true);
        paint.setStrokeJoin(Paint.Join.ROUND);
        paint.setStrokeWidth(5);

        // draw stroke
        paint.setStyle(Paint.Style.STROKE);
        paint.setColor(Color.parseColor("#AADB1255"));
        paint.setStrokeWidth(2);

        canvas.drawRect(left + colorballs.get(0).getWidthOfBall() / 2, 
                top + colorballs.get(0).getWidthOfBall() / 2, 
                right + colorballs.get(2).getWidthOfBall() / 2, 
                bottom + colorballs.get(2).getWidthOfBall() / 2, paint);

        //fill the rectangle
        paint.setStyle(Paint.Style.FILL);
        paint.setColor(Color.parseColor("#55DB1255"));
        paint.setStrokeWidth(0);

        canvas.drawRect(left + colorballs.get(0).getWidthOfBall() / 2, 
                top + colorballs.get(0).getWidthOfBall() / 2, 
                right + colorballs.get(2).getWidthOfBall() / 2, 
                bottom + colorballs.get(2).getWidthOfBall() / 2, paint);



        // draw corners
        bm = BitmapFactory.decodeResource(getResources(), R.drawable.circle);
        bmDrawable = new BitmapDrawable(getResources(), bm);

        // draw balls on canvas
        paint.setColor(Color.BLUE);
        paint.setTextSize(15);
        paint.setStrokeWidth(0);

        for (int i = 0; i < colorballs.size(); i++) {
            ColorBall ball = colorballs.get(i);
            canvas.drawBitmap(bmDrawable.getBitmap(), ball.getX(), ball.getY(), paint);
            canvas.drawText("" + (i + 1), ball.getX(), ball.getY(), paint);
        }
    }



}

这是xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <View
        android:id="@+id/drawview"
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        class="com.example.androiddraw.DrawView" />

</RelativeLayout>

我有ColorBall类,包含所有适当的setter和getter。我错过了什么?

提前致谢。

1 个答案:

答案 0 :(得分:0)

查看你的代码,我看到你调用“this.setBackgroundColor(Color.LTGRAY);”在你的onDraw方法中。在你的构造函数中设置它,因为你拥有它的方式,对setBackgroundColor的调用将使你的布局失效并导致重绘。您可以进入递归无效,绘制,无效,绘制情况。看看这是否有帮助。

如果这不能解决您的问题,那么您可能需要深入了解onDraw方法。尝试从小开始看看代码的某些部分是否在布局预览中正确呈现。

要检查的另一件事是确保在Activity.onCreate()中设置setContentView(R.layout ..)。有时我们会忘记并想知道为什么屏幕上没有任何内容。

最后,我看到了更多自定义布局,如下所示。也许你可以尝试这种语法。

<com.example.androiddraw.DrawView
    android:id="@+id/drawview"
    android:layout_width="fill_parent"
    android:layout_height="match_parent"
/>

希望这有帮助。