2016-05-04 179 views
0

我有一個透明的CircleView(它擴展了ImageView,是一個正方形)的問題。我在CircleView上翻譯了一個圓形圖像。我遇到的問題如下:Android:ViewGroup背景透明度

您看到的灰色背景是RelativeLayout,我想隱藏每個角落,這要感謝我的CircleView的onDraw,因爲感謝此功能,我將能夠在不顯示它在圈子外面。如果需要的話

@Override 
protected void onDraw(Canvas canvas) { 
    int circleCenter = getHeight()/2; 
    radius = circleCenter; 
    loadBitmap(); 

    if(hideBackground) { 
     Path circlePath = new Path(); 
     circlePath.addCircle(circleCenter, circleCenter, radius, Path.Direction.CCW); 

     circlePath.setFillType(Path.FillType.INVERSE_EVEN_ODD); 
     paint.setColor(0x00000000); 
     paint.setShader(null); 
     canvas.drawPath(circlePath, paint); 
    } else { 
     if (image != null) { 
      radius -= 5; 
      paint.setShader(computeBitmapShader(image)); 
      canvas.drawCircle(circleCenter, circleCenter, radius, paint); 
     } 
    } 
} 

而且我的佈局:

<RelativeLayout 
    android:id="@+id/wheel_layout" 
    android:layout_width="wrap_content" 
    android:layout_height="0dp" 
    android:layout_weight="0.45" 
    android:background="#4F4F4F"> 

    <CircleView 
     android:id="@+id/wheel" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_alignParentTop="true" 
     android:layout_centerHorizontal="true" /> 

    <TextView 
     android:id="@+id/counter" 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:textColor="#FFF" 
     android:text="\?" 
     android:textStyle="bold" 
     android:textSize="66dp" 
     android:gravity="center_vertical|center_horizontal" 
     android:layout_alignParentTop="true" 
     android:layout_centerHorizontal="true" /> 

</RelativeLayout> 

所以,我要的是畫一個透明的背景我的RelativeLayout的的背景下,你有什麼想法?

回答

0

你應該做這樣的事情在你的onDraw()方法:

final int color = 0xff424242; 
    final Paint paint = new Paint(); 
    final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight()); 
    final RectF rectF = new RectF(rect); 
    final float roundPx = bitmap.getHeight()/2; 

    paint.setAntiAlias(true); 
    canvas.drawARGB(0, 0, 0, 0); 
    paint.setColor(color); 
    canvas.drawRoundRect(rectF, roundPx, roundPx, paint); 

    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN)); 
    canvas.drawBitmap(bitmap, rect, rect, paint); 

閱讀更多信息:PorterDuff