2011-02-25 148 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)。