2012-10-05 63 views
0

我是Android新手。我試圖實現照片標記應用程序,我們可以標記臉部照片標記。我能夠找到臉部並在臉部上繪製矩形。如何動態更改編輯文本位置

但我試圖在用戶觸摸這些矩形時在面部識別的矩形中放置一個EditText(用於寫入名稱)。一旦完成,我需要禁用EditText並需要顯示TextView(名稱)。請在下面找到代碼並幫助我。

代碼:

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    // setContentView(R.layout.main); 

    layout = new LinearLayout(this); 
    LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, 
      LayoutParams.WRAP_CONTENT); 

    myEditTet = new EditText(this); 

    myView custom = new myView(this); 
    layout.addView(myEditTet); 
    myEditTet.setVisibility(View.GONE); 
    // layout.addView(myTextView); 
    // myTextView.setVisibility(View.GONE); 
    layout.addView(custom, params); // Of course, this too 
    setContentView(layout); 

} 

private class myView extends View { 

    private int imageWidth, imageHeight; 
    private int numberOfFace = 5; 
    private FaceDetector myFaceDetect; 
    private FaceDetector.Face[] myFace; 
    float myEyesDistance; 
    int numberOfFaceDetected; 

    Bitmap myBitmap; 

    public myView(Context context) { 
     super(context); 
     // TODO Auto-generated constructor stub 

     BitmapFactory.Options BitmapFactoryOptionsbfo = new BitmapFactory.Options(); 
     BitmapFactoryOptionsbfo.inPreferredConfig = Bitmap.Config.RGB_565; 
     myBitmap = BitmapFactory.decodeResource(getResources(), 
       R.drawable.face3, BitmapFactoryOptionsbfo); 
     imageWidth = myBitmap.getWidth(); 
     imageHeight = myBitmap.getHeight(); 
     myFace = new FaceDetector.Face[numberOfFace]; 
     myFaceDetect = new FaceDetector(imageWidth, imageHeight, 
       numberOfFace); 
     numberOfFaceDetected = myFaceDetect.findFaces(myBitmap, myFace); 

    } 

    @Override 
    protected void onDraw(Canvas canvas) { 
     // TODO Auto-generated method stub 

     canvas.drawBitmap(myBitmap, 0, 0, null); 

     Paint myPaint = new Paint(); 
     myPaint.setColor(Color.GREEN); 
     myPaint.setStyle(Paint.Style.STROKE); 
     myPaint.setStrokeWidth(3); 

     for (int i = 0; i < numberOfFaceDetected; i++) { 
      android.media.FaceDetector.Face face = myFace[i]; 
      PointF myMidPoint = new PointF(); 
      face.getMidPoint(myMidPoint); 
      myEyesDistance = face.eyesDistance(); 
      dx = (int) (myMidPoint.x - myEyesDistance); 
      dy = (int) (myMidPoint.y - myEyesDistance); 
      dz = (int) (myMidPoint.x + myEyesDistance); 
      dt = (int) (myMidPoint.y + myEyesDistance); 
      canvas.drawRect((int) (myMidPoint.x - myEyesDistance), 
        (int) (myMidPoint.y - myEyesDistance), 
        (int) (myMidPoint.x + myEyesDistance), 
        (int) (myMidPoint.y + myEyesDistance), myPaint); 
     } 
    } 

    @Override 
    public boolean onTouchEvent(MotionEvent event) { 

     myEditTet.setVisibility(View.VISIBLE); 
     if (event.getX() > dx && event.getX() < dz && event.getY() > dy 
       && event.getY() < dt) { 
      Log.i("SARDAR", "RECT TOUCH 1111"); 

     } 
     return super.onTouchEvent(event); 
    } 
} 
} 
+0

什麼確切的問題? – njzk2

回答

1

我已經這樣做了......

params.setmargin(left,top,right,bottom); 

layout.addView(myeditText,params); 

參數是int型

+0

感謝您的回覆,請您告訴我我們需要編寫這個聲明的位置,因爲我需要在用戶點擊屏幕時添加編輯文本,我認爲我們不能在oncreate()方法中編寫。 – user369932

+0

@ user369932歡迎您:)您絕對可以在onCreate()中將其寫入此文件中.. LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); –

+0

,並讓edittext隨時都可見(在你的情況下可能在onClick) –