0

这是上一个问题: 如何在 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 类,其中包含所有正确的设置器和获取器。我错过了什么?

提前致谢。

4

1 回答 1

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"
/>

希望这可以帮助。

于 2013-08-30T23:20:23.583 回答