2017-12-03 25 views
0

안드로이드 초보자는 여기 있지만 안드로이드 초보자이지만 장치 카메라의 TextureView 스트림 위에 투명 원을 오버레이하려고하는데 오버레이 뷰를 맨 위에 표시 할 수 없습니다. TextureView.원 테두리가있는 오버레이 TextureView보기

package org.tensorflow.demo; 

import android.content.Context; 
import android.util.AttributeSet; 
import android.view.TextureView; 

/** 
* A {@link TextureView} that can be adjusted to a specified aspect ratio. 
*/ 
public class AutoFitTextureView extends TextureView { 
    private int ratioWidth = 0; 
    private int ratioHeight = 0; 

    public AutoFitTextureView(final Context context) { 
    this(context, null); 
    } 

    public AutoFitTextureView(final Context context, final AttributeSet attrs) { 
     this(context, attrs, 0); 
    } 

    public AutoFitTextureView(final Context context, final AttributeSet attrs, final int defStyle) { 
     super(context, attrs, defStyle); 
    } 

    /** 
    * Sets the aspect ratio for this view. The size of the view will be measured based on the ratio 
    * calculated from the parameters. Note that the actual sizes of parameters don't matter, that 
    * is, calling setAspectRatio(2, 3) and setAspectRatio(4, 6) make the same result. 
    * 
    * @param width Relative horizontal size 
    * @param height Relative vertical size 
    */ 
    public void setAspectRatio(final int width, final int height) { 
     if (width < 0 || height < 0) { 
      throw new IllegalArgumentException("Size cannot be negative."); 
     } 
     ratioWidth = width; 
     ratioHeight = height; 
     requestLayout(); 
    } 

    @Override 
    protected void onMeasure(final int widthMeasureSpec, final int heightMeasureSpec) { 
     super.onMeasure(widthMeasureSpec, heightMeasureSpec); 
     final int width = MeasureSpec.getSize(widthMeasureSpec); 
     final int height = MeasureSpec.getSize(heightMeasureSpec); 
     if (0 == ratioWidth || 0 == ratioHeight) { 
      setMeasuredDimension(width, height); 
     } else { 
      if (width < height * ratioWidth/ratioHeight) { 
       setMeasuredDimension(width, width * ratioHeight/ratioWidth); 
      } else { 
       setMeasuredDimension(height * ratioWidth/ratioHeight, height); 
      } 
     } 
    } 
} 

그리고 내 오버레이 현재 디버깅을 위해 검은 색이 (원 컬러와 같은 현재 설정을 찾습니다 : 나는 TextureView 같은 구현

package org.tensorflow.demo; 

import android.content.Context; 
import android.graphics.Canvas; 
import android.graphics.Paint; 
import android.util.AttributeSet; 
import android.view.View; 


public class OverlayView extends View { 
    private final Paint transparentPaint; 

    public OverlayView(final Context context, final AttributeSet attrs) { 
     super(context, attrs); 
     transparentPaint = new Paint(); 
     transparentPaint.setColor(getResources().getColor(android.R.color.black)); 
    } 

    @Override 
    public void onDraw(final Canvas canvas) { 
     int w = getWidth(); 
     int h = getHeight(); 

     int pl = getPaddingLeft(); 
     int pr = getPaddingRight(); 
     int pt = getPaddingTop(); 
     int pb = getPaddingBottom(); 

     int usableWidth = w - (pl + pr); 
     int usableHeight = h - (pt + pb); 

     int radius = Math.min(usableWidth, usableHeight)/2; 
     int cx = pl + (usableWidth/2); 
     int cy = pt + (usableHeight/2); 
     canvas.drawCircle(cx, cy, radius, transparentPaint); 
    } 
} 

마지막으로 내가 같은 내 레이아웃 XML을 가지고있다 :

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <org.tensorflow.demo.AutoFitTextureView 
     android:id="@+id/texture" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" /> 

    <org.tensorflow.demo.OverlayView 
     android:id="@+id/overlay" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" /> 

    <org.tensorflow.demo.RecognitionScoreView 
     android:id="@+id/results" 
     android:layout_width="match_parent" 
     android:layout_height="112dp" /> 

</FrameLayout> 

답변

0

는 전체 화면의 사각형에서 원형 절단하는 PorterDuff.Mode.SRC_OUT를 사용하여이 문제를 해결 끝났다

protected void createWindowFrame() { 

    int w = getWidth(); 
    int h = getHeight(); 

    windowFrame = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888); 
    Canvas osCanvas = new Canvas(windowFrame); 

    RectF outerRectangle = new RectF(0, 0, getWidth(), getHeight()); 

    Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); 
    paint.setColor(Color.parseColor("#4885ed")); 
    osCanvas.drawRect(outerRectangle, paint); 

    paint.setColor(Color.TRANSPARENT); 
    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_OUT)); 

    int pl = getPaddingLeft(); 
    int pr = getPaddingRight(); 
    int pt = getPaddingTop(); 
    int pb = getPaddingBottom(); 

    int usableWidth = w - (pl + pr); 
    int usableHeight = h - (pt + pb); 

    int radius = Math.min(usableWidth, usableHeight)/2;   
    int cx = pl + (usableWidth/2); 
    int cy = pt + (usableHeight/2); 

    osCanvas.drawCircle(cx, cy, radius, paint); 
}