2013-07-19 36 views
1

我正在嘗試在觸摸點處繪製ImageView上的圓圈,但它在左上角繪製。在觸摸點處在ImageView上繪製圓形

我的代碼是

ImageView imageView; 
    private static int RESULT_LOAD_IMAGE = 1; 
    Bitmap bmp; 
    float touchY; 
    float touchX; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     imageView = (ImageView) findViewById(R.id.imageView1); 
     imageView.setOnTouchListener(new OnTouchListener() { 

      @Override 
      public boolean onTouch(View v, MotionEvent event) { 
       switch (event.getAction()) { 
       case MotionEvent.ACTION_DOWN: 
        touchX = event.getX(); 
        touchY = event.getY(); 
        imageView.setImageBitmap(drawCircle()); 
        break; 

       default: 
        break; 
       } 


       return false; 
      } 
     }); 
    } 

    private Bitmap drawCircle() { 
     Bitmap bitmap = bmp.copy(Bitmap.Config.ARGB_4444, true); 
     bitmap.setPixel(imageView.getWidth(), imageView.getHeight(), 0); 
     Canvas canvas = new Canvas(bitmap); 
     Paint paint = new Paint(); 
     paint.setStyle(Paint.Style.STROKE); 
     paint.setColor(Color.RED); 
     canvas.drawCircle(touchX, touchY, 100, paint); 
     return bitmap; 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.main, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     switch (item.getItemId()) { 
     case R.id.getimage: 
      pickImage(); 
      break; 

     default: 
      break; 
     } 
     return super.onOptionsItemSelected(item); 
    } 

    private void pickImage() { 
     Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); 
     startActivityForResult(intent, RESULT_LOAD_IMAGE); 

    } 

    @Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     super.onActivityResult(requestCode, resultCode, data); 
     if(requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && data != null){ 
      Uri uri = data.getData(); 
      String[] filePath = {MediaStore.Images.Media.DATA}; 
      Cursor cursor = getContentResolver().query(uri, filePath, null, null, null); 
      cursor.moveToFirst(); 
      int columnIndex = cursor.getColumnIndex(filePath[0]); 
      String picturePath = cursor.getString(columnIndex); 
      cursor.close(); 
      bmp = BitmapFactory.decodeFile(picturePath); 
      imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath)); 
     } 
    } 

和XML是

<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context=".MainActivity" > 

    <ImageView 
     android:id="@+id/imageView1" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentTop="true" 
     android:src="@drawable/ic_launcher" /> 

</RelativeLayout> 

我沒有得到什麼是錯的。我想在接觸點繪製圓圈。任何幫助將不勝感激。

回答

1

如果你要創建一個新的位圖ImageView的大小這個代碼將正常工作。

但是「bmp」位圖可能比ImageView大得多,並且被縮放。這就是爲什麼你會扭曲結果。

+0

感謝您的解決方案爲我工作...... :) –