2015-08-09 68 views
1

因此,我正在開發一個應用程序,用戶將能夠與圖像進行交互,我需要使用OnTouchListener,但我不知道如何正確執行。每當我點擊圖片 - 沒有任何反應。最後,我試圖獲得用戶觸摸圖像時會出現的圓圈,並且會隨着手指移動。這裏是我的MainActivity:在Canvas中使用OnTouchListener的Android

public class MainActivity extends Activity{ 



    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     File file = new File(Environment.getExternalStorageDirectory() + "/Pictures/boxes.jpg"); 

     String fileString = file.getPath(); 


     takenPhoto = (ImageView) findViewById(R.id.imageView1); 

     bmp = BitmapFactory.decodeFile(fileString); 
     mutableBitmap = bmp.copy(Bitmap.Config.ARGB_8888, true); 
     takenPhoto.setImageBitmap(mutableBitmap); 

    } 

    private static class DrawView extends View { 



     public DrawView(Context context) { 
      super(context); 
     } 


     public boolean onTouch(View view, MotionEvent event) { 


       int action = event.getAction(); 

       drawPos.x = event.getX(); 
       drawPos.y = event.getY(); 

       switch (action) { 
       case MotionEvent.ACTION_DOWN: 
       case MotionEvent.ACTION_MOVE: 
        drawing = true; 
        this.invalidate(); 
        break; 
       case MotionEvent.ACTION_UP: 
       case MotionEvent.ACTION_CANCEL: 
        drawing = false; 
        this.invalidate(); 
        break; 

       default: 
        break; 
       } 



      return true; 
     } 

     @Override 
     protected void onDraw(Canvas canvas) { 

      super.onDraw(canvas); 

      if (drawing) { 
       canvas.drawCircle(zoomPos.x, zoomPos.y, 100, mPaint); 
      } 
     } 





    } 


} 
+0

如果替換setOnClickListener中的setOnTouchListener,是否可以與圖像交互? –

+0

不,我不能使用OnClickListener,因爲它沒有我需要的MotionEvent –

+0

你在哪裏使用DrawView?在上面的代碼中,您爲圖像視圖設置了一個觸摸偵聽器,該圖像視圖吞下所有觸摸事件並不做任何事情。 –

回答

3

一個簡單的解決辦法是使用自定義的ImageView,將同時處理圖像和抽獎:

import android.content.Context; 
import android.graphics.Canvas; 
import android.graphics.Paint; 
import android.graphics.PointF; 
import android.support.annotation.NonNull; 
import android.util.AttributeSet; 
import android.view.MotionEvent; 
import android.widget.ImageView; 

public class DrawingImageView extends ImageView { 

    private PointF point; 
    private Paint paint = new Paint(); 

    public DrawingImageView(Context context) { 
     super(context); 
    } 

    public DrawingImageView(Context context, AttributeSet attrs) { 
     super(context, attrs); 
    } 

    public DrawingImageView(Context context, AttributeSet attrs, int defStyleAttr) { 
     super(context, attrs, defStyleAttr); 
    } 

    @Override 
    public boolean onTouchEvent(@NonNull MotionEvent event) { 
     float x = event.getX(); 
     float y = event.getY(); 

     switch (event.getAction()) { 
      case MotionEvent.ACTION_DOWN: 
       point = new PointF(x, y); 
       invalidate(); 
       break; 
      case MotionEvent.ACTION_MOVE: 
       point.set(x, y); 
       invalidate(); 
       break; 
      case MotionEvent.ACTION_UP: 
      case MotionEvent.ACTION_CANCEL: 
       point = null; 
       invalidate(); 
       break; 
     } 
     return true; 
    } 

    @Override 
    protected void onDraw(@NonNull Canvas canvas) { 
     super.onDraw(canvas); 
     if (point != null) { 
      canvas.drawCircle(point.x, point.y, 100, paint); 
     } 
    } 
} 

然後在XML文件中:

<your.package.DrawingImageView 
    android:id="@+id/image" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" /> 

而在你的活動:

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    //... 
    view = (DrawingImageView) findViewById(R.id.image); 
    //... 
    view.setImageBitmap(mutableBitmap); 
} 

這就是全部

+0

我不確定它是否更簡單。你能告訴我你的意思是「你應該在ImageView上面添加一個DrawView」嗎? –

+0

真的,這種方法更簡單。它避免了處理多個視圖。最好只有一個視圖,它處理圖像,觸摸事件和繪圖工作。嚴重的是,你只需要導入這兩個代碼片段。以前的方法會要求你在ImageView上面添加一個視圖,並確保它具有相同的大小等等。 –

+0

工程類似於charm.Thanks! –

0

您需要從活動中刪除最後一個onTouch方法和刪除setOnTouchListener。 由於該活動捕捉觸摸事件,該視圖將永遠無法訪問它。

public class MainActivity extends Activity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     File file = new File(Environment.getExternalStorageDirectory() + "/Pictures/boxes.jpg"); 
     String fileString = file.getPath(); 
     takenPhoto = (ImageView) findViewById(R.id.imageView1); 

     bmp = BitmapFactory.decodeFile(fileString); 
     mutableBitmap = bmp.copy(Bitmap.Config.ARGB_8888, true); 
     takenPhoto.setImageBitmap(mutableBitmap); 
    } 

    private static class DrawView extends View { 

     public DrawView(Context context) { 
      super(context); 
     } 

     public boolean onTouch(View view, MotionEvent event) { 
       int action = event.getAction(); 
       drawPos.x = event.getX(); 
       drawPos.y = event.getY(); 

       switch (action) { 
       case MotionEvent.ACTION_DOWN: 
       case MotionEvent.ACTION_MOVE: 
        drawing = true; 
        this.invalidate(); 
        break; 
       case MotionEvent.ACTION_UP: 
       case MotionEvent.ACTION_CANCEL: 
        drawing = false; 
        this.invalidate(); 
        break; 

       default: 
        break; 
       } 
      return true; 
     } 

     @Override 
     protected void onDraw(Canvas canvas) { 
      super.onDraw(canvas); 
      if (drawing) { 
       canvas.drawCircle(zoomPos.x, zoomPos.y, 100, mPaint); 
      } 
     } 
    } 

} 
+0

刪除OnTouchListener沒有幫助。圖像沒有任何反應 –

+0

事實上,這將發生在DrawView中。你應該創建一個,並將其添加到視圖根目錄 –

+0

我不太明白。我應該在DrawView中創建什麼。 setOnTouchListener或圖像? –