简体   繁体   中英

Android getX and getY for a custom view: strange behavior

I need your help with an Android custom view and the methods getX() and getY() : I'm trying to paint a simple circle at view.getX() and view.getY(), but the circle hasn't its center in the top-left corner as I expect. See the image below (the custom View is the green rectangle):

我的自定义视图

This is my custom view code:

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;


public class PlotterView extends View {

    private Paint paint;
    public PlotterView(Context context, AttributeSet attrs) {
        super(context, attrs);
        paint = new Paint();
        paint.setColor(Color.BLUE);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        canvas.drawCircle(this.getX(), this.getY(),5, paint);

    }
}

And this is the entire layout:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    tools:context="calc.PlotFunctionActivity">

    <calc.PlotterView
        android:id="@+id/plotter"
        android:padding="0dp"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:background="#00FF00"
        android:layout_weight="8"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:orientation="horizontal"
        android:layout_weight="2">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:textSize="@dimen/plot_text_size"
            android:text="f(x)="
            android:gravity="center_vertical|end"/>
        <EditText
            android:id="@+id/function"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:textSize="@dimen/plot_text_size"
            android:layout_weight="4"
            android:gravity="center_vertical|start"/>
        <Button
            android:id="@+id/plot"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:textSize="@dimen/plot_text_size"
            android:layout_weight="2"
            android:text="@string/plot_button_label"/>
        </LinearLayout>

</LinearLayout>

Any suggestion? Thank you in advance.

pskink is totally right, you set a padding to your "base" linearlayout your plot view so when you call getX() it returns you position of plotview in its parent layout (your base linearlayout) landmark (something corresponding to 16dp).

Then when you draw a circle you set circle center in plotview landmark so top-left corner of green view is (0,0). For now you can see the same translation between baselayout-plotview and plotview-circle center.

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