【问题标题】:Android getX and getY for a custom view: strange behaviorAndroid getX 和 getY 用于自定义视图:奇怪的行为
【发布时间】:2014-12-02 13:46:51
【问题描述】:

我在 Android 自定义视图和 getX()getY() 方法方面需要您的帮助:我正在尝试在 view.getX() 和 view.getY() 处绘制一个简单的圆圈,但圆圈没有正如我所料,它位于左上角的中心。见下图(自定义View为绿色矩形):

这是我的自定义视图代码:

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);

    }
}

这是整个布局:

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

有什么建议吗?先感谢您。

【问题讨论】:

  • 画圆时用0、0代替getX()、getY()
  • getX和getY不返回View的左上角吗?
  • 是的,在父级中,但是 Canvas 被翻译成 0,0 是 View 的左上角
  • 好的,完美。谢谢,我错过了画布翻译。

标签: android xml android-layout android-canvas android-custom-view


【解决方案1】:

pskink 是完全正确的,您为绘图视图的“基本”线性布局设置了一个填充,因此当您调用 getX() 时,它会返回绘图视图在其父布局(您的基本线性布局)地标中的位置(对应于 16dp) .

然后,当您绘制一个圆时,您在 plotview 地标中设置圆心,因此绿色视图的左上角为 (0,0)。现在您可以看到 baselayout-plotview 和 plotview-circle center 之间的相同转换。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-07-24
    • 1970-01-01
    • 1970-01-01
    • 2012-05-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多