2016-06-30 2 views
1

위에 캔버스를 가지고하는 방법 : 나는 FrameLayout이를 확장하는이 클래스를 수정 한내가 여기에서 가져온 라이브러리 인 내 응용 프로그램에 대한 I 돋보기보기를 사용하고 다른 뷰

https://github.com/nomanr/android-image-magnifier

은 (는 이미지 뷰이었다 이전) 내 FrameLayout에서 작업 할 수 있습니다.

페인트 된 캔버스보기가 해당 사용자 정의보기에 추가 된 모든보기를 유지한다는 점을 제외하면 잘 작동합니다.

추가 된 뷰 앞에 캔버스를 가져 오는 방법은 무엇입니까? 내가 사용하고

사용자 정의 뷰 클래스 :

public class ImageMagnifier extends FrameLayout { 

private PointF zoomPos; 
private boolean zooming = false; 
private Matrix matrix; 
private Paint paint; 
private Bitmap bitmap; 
private BitmapShader shader; 
private int sizeOfMagnifier = 300; 
int cheight, cwidth; 

Context con; 
C c = C.getInstance(); 
public ImageMagnifier(Context context) { 
    super(context); 
    init(); 
    con=context; 
} 

public ImageMagnifier(Context context, AttributeSet attrs, int defStyleAttr) { 
    super(context, attrs, defStyleAttr); 
    init(); 
    con=context; 
} 

public ImageMagnifier(Context context, AttributeSet attrs) { 
    super(context, attrs); 
    init(); 
    con=context; 
} 

private void init() { 
    zoomPos = new PointF(0, 0); 
    matrix = new Matrix(); 
    paint = new Paint(); 

    cwidth = c.Width * 109/1280; 
    cheight = cwidth * 134/109; 
} 

public void otherTouch(int x, int y) { 
    if (x > c.getwidth1(28) && x < c.getwidth1(921) && y > c.getheight1(135) && y < c.getheight1(560)) { 
     zoomPos.x = x - 10; 
     zoomPos.y = y - 75; 
     zooming = true; 
     this.invalidate(); 

    } else { 
     RemoveMagnifire(); 
    } 
} 

public void RemoveMagnifire() { 
    zooming = false; 
    this.invalidate(); 
} 

@Override 
protected void onDraw(Canvas canvas) { 
    super.onDraw(canvas); 

    if (!zooming) { 
     buildDrawingCache(); 
    } else { 

     bitmap = getDrawingCache(); 
     shader = new BitmapShader(bitmap, Shader.TileMode.CLAMP,Shader.TileMode.CLAMP);   
     paint.setShader(shader); 
     matrix.reset(); 
     matrix.postScale(2f, 2f, zoomPos.x-10, zoomPos.y+60); 

     paint.getShader().setLocalMatrix(matrix); 

     int width = c.Width; 
     int height = c.Height; 

     float leftX = zoomPos.x - ((width * 100)/1280); 
     float topY = zoomPos.y - ((height * 250)/720); 
     float rightX = zoomPos.x + ((width * 100)/1280); 
     float bottomY = zoomPos.y - ((height * 100)/720); 

     canvas.drawRect(leftX , topY, rightX, bottomY, paint); 


    } 
} 

} 
+0

수정 된 수업을 게시하여 어떤 의미인지 알 수 있도록하십시오. –

+0

여기 있습니다 : http://pastebin.com/AzcVEWG7 – Prashant

+1

확인은 매우 간단하지만 질문 자체에 클래스를 게시하십시오. 오프 사이트 코드에 링크하지 마십시오. 미래의 사용자는 그것을 볼 수 있어야하고, 그 링크가 죽은 후에는 볼 수 없게됩니다. 감사. –

답변

1

ViewGroupdispatchDraw() 방법에 자식 View들립니다. 그 후에 돋보기 그림을 이동해야합니다.

수정 사항은 간단합니다. super 전화를 onDraw() 번으로 호출 한 후 super 전화를 dispatchDraw()으로 호출 한 후 모든 항목을 이동하십시오. 더 이상 무엇을 위해 필요하지 않은

... 

@Override 
protected void onDraw(Canvas canvas) { 
    super.onDraw(canvas); 
    // Removed 
} 

@Override 
protected void dispatchDraw(Canvas canvas) { 
    super.dispatchDraw(canvas); 

    if (!zooming) { 
     buildDrawingCache(); 
    } 
    else { 
     bitmap = getDrawingCache(); 
     shader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP); 
     paint.setShader(shader); 
     matrix.reset(); 
     matrix.postScale(2f, 2f, zoomPos.x - 10, zoomPos.y + 60); 

     paint.getShader().setLocalMatrix(matrix); 

     int width = c.Width; 
     int height = c.Height; 

     float leftX = zoomPos.x - ((width * 100)/1280); 
     float topY = zoomPos.y - ((height * 250)/720); 
     float rightX = zoomPos.x + ((width * 100)/1280); 
     float bottomY = zoomPos.y - ((height * 100)/720); 

     canvas.drawRect(leftX , topY, rightX, bottomY, paint); 
    } 
} 

... 

당신은 단지의 onDraw() 재정의를 제거 할 수 있습니다.

+0

확인해 주셔서 감사합니다. 어떻게 캔버스 투명성을 제거 할 수 있습니까? drawRect()는 사각형을 완벽하게 그립니다 만 투명도가 있습니다. – Prashant

+1

그래, 할거야은 기본적으로 투명한 배경을해야하기 때문에 당신이 사용자 정의'View'의 배경을 설정하지 한 경우 그. 색은'페인트 #의 setColor를()'방법, 당신이 원하는대로 설정하면, 같은 크기와 위치, 바로 다른 하나는 전에 다른'Rect'을 그릴하지만 다른'Paint' 개체 수 있습니다. –

+0

괜찮아. 나는 투명한 이미지를 배경으로 사용했다. – Prashant

관련 문제