2016-07-22 105 views
0

我實現了自定義ImageView。因爲我的圖像很大,我使用ScrollView和Horizo​​ntalScrollView來滾動圖像。我想在onTouch事件中繪製圓圈,但它不適用於所有情況。當我滾動圖像和觸發事件時,x,y座標適用於圖像的當前可見扇區(在右上角,例如0x0,但當我滾動somwhere並且在可見頂部角落時,它也將爲0x0),但圓形正在繪製根據圖像大小。 另一個問題是,應用程序可以用於各種屏幕尺寸。 我知道它xamarin c#但在原生Android應該是一樣的。任何人都知道如何正確繪製/獲取座標?獲取觸摸座標相對於自定義ImageView

public class DrawViewInside : ImageView 
{ 

    public static Bitmap bitmapInside; 
    private Paint paint = new Paint(); 
    private Point point = new Point(); 

    private Canvas mCanvas; 
    private static Bitmap mutableBitmap; 
    private Context mContext; 
    public static Bitmap b; 
    public void SetCarId(int id) 
    { 
     carId = id; 
    } 
    public DrawViewInside(Context context, IAttributeSet attrs) : base(context, attrs) 
    { 
     mContext = context; 
     setDefault(drawable); 
    } 

    public void SetBitmap(int drawableId) 
    { 
     setDefault(drawableId); 
     Invalidate(); 
    } 


    protected override void OnMeasure(int widthMeasureSpec, int heightMeasureSpec) 
    { 
     base.OnMeasure(widthMeasureSpec, heightMeasureSpec); 

     int width = MeasureSpec.GetSize(widthMeasureSpec); 
     int height = width; 

     var metrics = Resources.DisplayMetrics; 
     if (b != null) 
     { 
      SetMeasuredDimension(b.Width, b.Height); 
     } 

    } 


    public async void setDefault(int drawableId) 
    { 


      BitmapFactory.Options options = await GetBitmapOptionsOfImage(drawableId); 
      paint.Color = Color.Red; 
      paint.StrokeWidth = 15; 
      paint.SetStyle(Paint.Style.Stroke); 
      var metrics = Resources.DisplayMetrics; 
      var widthInDp = ConvertPixelsToDp(metrics.WidthPixels); 
      var heightInDp = ConvertPixelsToDp(metrics.HeightPixels); 

      b = BitmapFactory.DecodeResource(Resources, drawableId);esources, drawableId); 
      mutableBitmap = b.Copy(Bitmap.Config.Argb8888, true); 
      mCanvas = new Canvas(mutableBitmap); 
      mCanvas.Save(); 

    } 


    protected override void OnDraw(Canvas canvas) 
    { 
     DrawCircle(canvas); 


    } 
    private void DrawCircle(Canvas canvas) 
    { 
     if (mCanvas == null) 
     { 
      setDefault(drawable); 
     } 
     else 
     { 
      mCanvas.Restore(); 
      mCanvas.DrawCircle(point.x, point.y, 10, paint); 
      mCanvas.Save(); 
      canvas.DrawBitmap(mutableBitmap, 0, 0, paint); 
      bitmapInside = mutableBitmap; 
     } 


    } 

    public override bool OnTouchEvent(MotionEvent e) 
    { 
     switch (e.Action) 
     { 
      case MotionEventActions.Down: 

       break; 

      case MotionEventActions.Up: 
         Activity act = (Activity)mContext; 

         float viewX = e.RawX - this.Left; 
         float viewY = e.RawY - this.Top; 
         point.x = viewX;// e.RawX; 
         point.y = viewY;// e.RawY; 

        break; 
       } 
     Invalidate(); 
     return true; 

    } 

} 
public class Point 
{ 
    public float x, y; 
} 

回答

0

我找到了解決辦法。只需更換

    float viewX = e.RawX - this.Left; 
        float viewY = e.RawY - this.Top; 
        point.x = viewX;// e.RawX; 
        point.y = viewY;// e.RawY; 

     float viewX = e.RawX;// - this.Left; 
         float viewY = e.RawY;// - this.Top; 
         int[] viewCoords = new int[2]; 
         this.GetLocationOnScreen(viewCoords); 
         point.x = viewX - viewCoords[0];// e.RawX; 
         point.y = viewY - viewCoords[1];// e.RawY;