2011-02-25 283 views
1

我对android很陌生,但基本上我想设置一个程序,以便当用户点击一个图像视图时,一个点被绘制在他们点击的位置。我已经尝试了很多次,但只是似乎没有能够得到它的工作,并帮助将非常appreciated.So到目前为止,我有安卓绘图

package com.smallbore.smallbore; 

import android.app.Activity; 
import android.graphics.drawable.ShapeDrawable; 
import android.graphics.drawable.shapes.OvalShape; 
import android.os.Bundle; 
import android.view.MotionEvent; 
import android.view.View; 
import android.widget.ImageView; 
import android.widget.TextView; 

public class targetenter extends Activity { 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.targetenter); 
     ImageView v = (ImageView) findViewById(R.id.imageView1); 
     v.setOnTouchListener(new View.OnTouchListener() { 

      @Override 
      public boolean onTouch(View arg0, MotionEvent arg1) { 
       TextView t1 = (TextView) findViewById(R.id.textView2); 
       TextView t2 = (TextView) findViewById(R.id.textView3); 
       t1.setText("X: "+arg1.getX()); 
       t2.setText("Y: "+arg1.getY()); 
       int x = (int)arg1.getX(); 
        int y = (int)arg1.getY(); 
        int width = 50; 
        int height = 50; 

        ShapeDrawable mDrawable = new ShapeDrawable(new OvalShape()); 
        mDrawable.getPaint().setColor(0xff74AC23); 
        mDrawable.setBounds(x, y, x + width, y + height); 
        ImageView v = (ImageView) findViewById(R.id.imageView1) 
        v.setImageDrawable(mDrawable); 
        return false;  
    } 
      }); 
     }; 
    } 
+0

完成,虽然我认为这是远远不符合要求 – 2011-02-25 18:40:26

回答

1

要绘制的点,你可能不得不使用自定义视图并重写onTouchEvent和onDraw。 onTouchEvent将为您提供触摸事件的x,y坐标,并且在onDraw中,您可以在此处绘制一个圆圈到框架为此方法提供的画布。如果你想清除以前的点,你只需要跟踪最后的x,y坐标。否则,你需要保留一个运行列表(ArrayList或类似的东西)。

在你的鞋子里,我可能继承ImageView的子类,这样我就可以免费得到图像绘画的东西。在重写的onDraw方法内调用super.onDraw(canvas),然后绘制你的点(canvas.drawCircle)。