Android canvas drawCircle坐标原点

时间:2014-12-15 16:54:43

标签: android android-canvas graphics2d

我想在drawable上动态绘制一个圆圈。所以我创建了这个类:

public class CustomTextViewDrawable extends LayerDrawable {

    private Paint mPaint;
    private View mParent;

    public CustomTextViewDrawable(View parent, Drawable[] layers) {
        super(layers);
        mParent = parent;

        mPaint = new Paint();
        mPaint.setAntiAlias(true);
    }

    @Override
    public void draw(Canvas canvas) {
        super.draw(canvas);
        float radius = 5;

        // Top-left corner
        float centerX = 0;
        float centerY = 0;

        // Draw circle
        mPaint.setColor(Color.RED);
        mPaint.setStyle(Style.FILL_AND_STROKE);

        canvas.drawCircle(centerX, centerY, radius, mPaint);
    }

    @Override
    public boolean isStateful() {
        return false;
    }

}

我的用法如下:

// Get the drawable set in XML file ...
Drawable[] layers = new Drawable[] { imageView.getDrawable() };
Drawable d = new CustomTextViewDrawable(imageView, layers);
// ... and replace it
imageView.setImageDrawable(d);

我想得到的是1/4的圆心,左上角有一个中心,但我得到的是以下内容(在我的设备中启用了“show layout bounds”选项):

screenshot

有人能告诉我为什么要点(0,0)吗?它不应该放在左上角吗?

修改

这是XML布局文件:

<?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="wrap_content"
    android:orientation="vertical" >

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingLeft="10dp"
        android:paddingRight="10dp"
        android:paddingTop="5dp"
        android:paddingBottom="5dp" >


        <!-- Some views here ... -->

        <ImageButton
            android:id="@+id/questions_answers"
            android:layout_width="wrap_content"
            android:src="@drawable/ic_action_help"
            android:layout_height="wrap_content"   
            android:layout_centerVertical="true"
            android:layout_toLeftOf="@id/..."
            android:contentDescription="@string/..." />
    </RelativeLayout>

    <!-- Some other views here ... -->

</LinearLayout>

1 个答案:

答案 0 :(得分:0)

尝试在ImageView中绘制该点或设置为imageView android:scaleType =“fitStart”。也许你的imageView默认创建一些填充,这取决于scaleType。