2015-05-11 27 views
0

enter image description here我試圖創建一個圓形繪製把儘可能覆蓋的資料圖片(圓形圖像) 我寫了下面的代碼爲圓形繪製覆蓋,如何用以下要求創建繪製圓?

<?xml version="1.0" encoding="utf-8"?> 

<layer-list xmlns:android="http://schemas.android.com/apk/res/android"> 

    <item> 
     <shape> 
      <solid android:color="@color/light_blue_bg" /> 
     </shape> 
    </item> 
    <item> 
     <shape android:shape="oval"> 
      <stroke 
       android:width="2dp" 
       android:color="@android:color/white" /> 
      <solid android:color="@android:color/transparent" /> 
     </shape> 
    </item> 
</layer-list> 

參考輸出下面的屏幕圖像, enter image description here

已知問題:我創建與@顏色/ light_blue_color一個矩形,它有與行程的顏色和內實心透明色內的橢圓形,但由於外矩形彩色內圓透明度不顯示圖像。 如果我刪除外部矩形顏色真正的方形圖片將出來的圓形覆蓋。

有沒有辦法給外部圓形部分單獨塗色?

任何幫助會得到高度讚賞

+0

'「...不顯示圖片。」'圖片?什麼圖片? – pskink

+0

這個覆蓋圖背後有一個圖像....我試圖顯示白色圓圈內的圖片也試圖隱藏圖像的白圈覆蓋圖中出現的區域,以默認的bg顏色隱藏。 –

+0

圖片?你的意思是ImageView? – pskink

回答

1

@Sreedhu馬杜....你有自定義您的ImageView和位圖和帆布來實現這樣的工作吧..

雖然我建議你使用發表亨寧Dodenhof在圖書館的一個github上和Android,阿森納...... CircleImageView

上,你可以看看這是怎麼done..using該類對方..

Custom Class for Circular ImageView

小鬼方法是設置()和的onDraw()

0

我不認爲這是剛剛在XML這樣做可能。但是你可以用代碼掩蓋它。有關示例,請參見this tutorial

0

對於圓角(帶圈的ImageView)將該類添加到您的源

import android.content.Context; 
import android.graphics.Bitmap; 
import android.graphics.Bitmap.Config; 
import android.graphics.Canvas; 
import android.graphics.Color; 
import android.graphics.Paint; 
import android.graphics.PorterDuff.Mode; 
import android.graphics.PorterDuffXfermode; 
import android.graphics.Rect; 
import android.graphics.drawable.BitmapDrawable; 
import android.graphics.drawable.Drawable; 
import android.util.AttributeSet; 
import android.widget.ImageView; 

public class RoundedImageView extends ImageView { 

     public RoundedImageView(Context ctx, AttributeSet attrs) { 
       super(ctx, attrs); 
     } 

     @Override 
     protected void onDraw(Canvas canvas) { 

       Drawable drawable = getDrawable(); 

       if (drawable == null) { 
        return; 
       } 

       if (getWidth() == 0 || getHeight() == 0) { 
        return; 
       } 
       Bitmap b = ((BitmapDrawable) drawable).getBitmap(); 
       Bitmap bitmap = b.copy(Bitmap.Config.ARGB_8888, true); 

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

       Bitmap roundBitmap = getRoundedCroppedBitmap(bitmap, w); 
       canvas.drawBitmap(roundBitmap, 0, 0, null); 

     } 

     public static Bitmap getRoundedCroppedBitmap(Bitmap bitmap, int radius) { 
       Bitmap finalBitmap; 
       if (bitmap.getWidth() != radius || bitmap.getHeight() != radius) 
        finalBitmap = Bitmap.createScaledBitmap(bitmap, radius, radius, 
            false); 
       else 
        finalBitmap = bitmap; 
       Bitmap output = Bitmap.createBitmap(finalBitmap.getWidth(), 
          finalBitmap.getHeight(), Config.ARGB_8888); 
       Canvas canvas = new Canvas(output); 

       final Paint paint = new Paint(); 
       final Rect rect = new Rect(0, 0, finalBitmap.getWidth(), 
          finalBitmap.getHeight()); 

       paint.setAntiAlias(true); 
       paint.setFilterBitmap(true); 
       paint.setDither(true); 
       canvas.drawARGB(0, 0, 0, 0); 
       paint.setColor(Color.parseColor("#BAB399")); 
       canvas.drawCircle(finalBitmap.getWidth()/2 + 0.7f, 
          finalBitmap.getHeight()/2 + 0.7f, 
          finalBitmap.getWidth()/2 + 0.1f, paint); 
       paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN)); 
       canvas.drawBitmap(finalBitmap, rect, rect, paint); 

       return output; 
     } 

} 

,然後改變你的ImageView到你的包名和類名(RoundedImageView)在你的佈局文件中,如

<com.mypackege.RoundedImageView 
     android:id="@+id/imageView_round" 
     android:layout_width="100dp" 
     android:layout_height="100dp" 
     android:layout_marginTop="15dp" 
     android:src="@drawable/my_image" /> 

希望這會起作用。

0

嘗試這樣的事情,它爲我工作!

在XML地方

這一習俗RoundImage:

<com.example.hkh.learningandroid.RoundedImageView 
     android:layout_width="200dp" 
     android:layout_height="200dp" 
     android:src="@drawable/person" 
     android:layout_alignParentTop="true" 
     android:layout_centerHorizontal="true" 
     android:id="@+id/MyImageView" /> 

,並創建新的類並將其命名爲RoundImageView:

public class RoundedImageView extends ImageView { 

public RoundedImageView(Context ctx, AttributeSet attrs) { 
    super(ctx, attrs); 
} 

@Override 
protected void onDraw(Canvas canvas) { 

    Drawable drawable = getDrawable(); 

    if (drawable == null) { 
     return; 
    } 

    if (getWidth() == 0 || getHeight() == 0) { 
     return; 
    } 
    Bitmap b = ((BitmapDrawable) drawable).getBitmap(); 
    Bitmap bitmap = b.copy(Bitmap.Config.ARGB_8888, true); 

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

    Bitmap roundBitmap = getRoundedCroppedBitmap(bitmap, w); 
    canvas.drawBitmap(roundBitmap, 0, 0, null); 

} 

public static Bitmap getRoundedCroppedBitmap(Bitmap bitmap, int radius) { 
    Bitmap finalBitmap; 
    if (bitmap.getWidth() != radius || bitmap.getHeight() != radius) 
     finalBitmap = Bitmap.createScaledBitmap(bitmap, radius, radius, 
       false); 
    else 
     finalBitmap = bitmap; 
    Bitmap output = Bitmap.createBitmap(finalBitmap.getWidth(), 
      finalBitmap.getHeight(), Bitmap.Config.ARGB_8888); 
    Canvas canvas = new Canvas(output); 

    final Paint paint = new Paint(); 
    final Rect rect = new Rect(0, 0, finalBitmap.getWidth(), 
      finalBitmap.getHeight()); 

    paint.setAntiAlias(true); 
    paint.setFilterBitmap(true); 
    paint.setDither(true); 
    canvas.drawARGB(0, 0, 0, 0); 
    paint.setColor(Color.parseColor("#BAB399")); 
    canvas.drawCircle(finalBitmap.getWidth()/2 + 0.7f, 
      finalBitmap.getHeight()/2 + 0.7f, 
      finalBitmap.getWidth()/2 + 0.1f, paint); 
    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN)); 
    canvas.drawBitmap(finalBitmap, rect, rect, paint); 

    return output; 

} 
} 

和運行,我希望這會有所幫助,它會得到所需要的圓你想要的圖像