Android 如何在ImageView上绘制

Android 如何在ImageView上绘制,android,Android,为什么这段代码不在用户在imageview中触摸的地方画一个圆,并将坐标输出到textview package com.smallbore.smallbore; import android.app.Activity; import android.content.Context; import android.graphics.Canvas; import android.os.Bundle; import android.view.MotionEvent; import android.w

为什么这段代码不在用户在imageview中触摸的地方画一个圆,并将坐标输出到textview

package com.smallbore.smallbore;

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.os.Bundle;
import android.view.MotionEvent;
import android.widget.ImageView;
import android.widget.TextView;

public class targetenter extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.targetenter);
    }

   public class ImageView1 extends ImageView {
        public int x;
        public int y;
        public ImageView1(Context context) {
            super(context);
            // TODO Auto-generated constructor stub
        }
        public boolean dispatchtouchevent(MotionEvent event){
            x = (int) event.getX();
            y = (int) event.getY();
            TextView t1 = (TextView)findViewById(R.id.textView2);
            t1.setText("x="+x);
            TextView t2 = (TextView)findViewById(R.id.textView3);
            t2.setText("y="+y);
            invalidate();
            return true;
        }
        @Override
        public void onDraw(Canvas canvas){
            super.onDraw(canvas);
            canvas.drawCircle(x,y,10, null );
    }
}
}
以及xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:id="@+id/linearLayout1" android:layout_height="fill_parent" android:layout_width="fill_parent">
    <TextView android:layout_height="wrap_content" android:text="@string/cposition" android:id="@+id/textView1" android:layout_width="wrap_content"></TextView>
    <com.smallbore.smallbore.targetenter.Imageview1 android:id="@+id/imageView1" android:layout_width="300dip" android:layout_height="300dip" android:background="@drawable/target" android:layout_gravity="center_horizontal"></com.smallbore.smallbore.targetenter.Imageview1>
    <TextView android:text="TextView" android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
    <TextView android:text="TextView" android:id="@+id/textView3" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
</LinearLayout>


ontochevent
更改为。另外,确保您在XML中使用的是
imageView1
,而不仅仅是
ImageView

您没有在onTouchEvent中调用invalidate()。当你改变画布上的东西时,它必须被重新绘制。仅供参考,如果您不在UI线程上,则必须调用postinvalidate()。请参阅此api演示以了解有关在画布上绘制的更多信息

我不确定克里斯蒂安关于使用
dispatchTouchEvent()
的建议是否正确,这种方法在
ViewGroup
子代中将触摸事件分派给其子代是有意义的。然而,他关于在xml布局声明中使用自定义视图的建议很重要。你可以这样做:

<package.that.contains.targetenter.imageView1 android:id ....

如果您仍然有问题,那么查看
targetCenter.xml
布局可能会很有用。

因此我需要:?没关系,现在对该部分进行了排序,我想我该如何公开该类/在另一个文件中声明它?很抱歉,我对这个很陌生。@Leo:现在你在活动中将它声明为嵌套类,并且它没有声明为公共类,所以我不确定Android在加载布局时是否会找到它。我认为只要加上“public”就行了。如果不起作用,您可以将类ImageView1剪切并粘贴到另一个名为ImageView1.java的文件中,并用xml更新包路径。我得到了java.lang.ClassNotFoundException我假设这意味着xml没有以正确的方式指向该类,但我已经尝试了上面的方法,我把它放在com.smallhole.smallhole ImageView1.java中,其中包含类ImageView1,然后像上面那样将xml指向com.smallhole.smallhole.ImageView1.ImageView1,但这也不起作用。@Leo:你做得很好,但是
com.smallhole.smallhole.ImageView1.ImageView1
应该是
com.smallbore.smallbore.ImageView1
@Override
public void onDraw(Canvas canvas){
    super.onDraw(canvas);
    canvas.drawCircle(x,y,10, null );
}