2014-01-23 42 views
2

我有一個RelativeLayoutViewImageView覆蓋它。
現在,我正在修改ImageView的位圖的一部分,使其透明&使該區域無效。
這有效,但我沒有看到底下的第一個View,只是Activity的背景。
試圖使無效,Viewandroid.R.id.content,沒有運氣。ImageView不顯示基礎視圖時,其部分透明

問題:鑑於View A覆蓋View B,我該怎麼做的View AView B可見各零件時部分變成透明的?

+0

你能後的XML? – Niko

+0

考慮到你的問題,這應該是自動的,最近做了一些項目,我只是把imageview放在其他組件上,然後用你的手指擦除圖像,沒什麼特別的。提供更多信息,如xml或您可以共享的內容可能還有其他一些問題。 –

+0

是ImageView的位圖背景或源代碼。你確定這些觀點重疊嗎? – Blackbelt

回答

0

問題出在位圖本身。

這裏是我是如何將其轉換爲可變:

Bitmap mutableBitmap = sourceBitmap.copy(bm.getConfig(), true); // getConfig() returned ARGB_8888 

,在這裏它是如何工作:

Bitmap mutableBitmap = Bitmap.createBitmap(bm.getWidth(), bm.getHeight(), 
        bm.getConfig()); 
Canvas canvas = new Canvas(mutableBitmap); 
canvas.drawBitmap(sourceBitmap, 0, 0, null); 
-1

嘗試改變相對佈局的FrameLayout

+0

這甚至沒有關閉:) –

0

這是我的條紋解決方案,爲了更好地理解

Image before deleting

Image after deleting

XML第一截圖:

<?xml version="1.0" encoding="utf-8"?> 
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/RelativeLayout1" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@drawable/bck" 
    android:orientation="vertical" > 

<View 
    android:id="@+id/view1" 
    android:layout_width="200dp" 
    android:layout_height="200dp" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentTop="true" 
    android:background="@drawable/view" /> 

<com.example.stackoverflow.CustomView 
    android:id="@+id/CustomView1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentTop="true" 
    android:src="@drawable/imageview" /> 

    </RelativeLayout> 

MainActivity:

public class MainActivity extends Activity implements OnClickListener{ 

private CustomView imageView; 

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    imageView = (CustomView)findViewById(R.id.CustomView1); 
    imageView.setOnClickListener(this); 
} 


@Override 
public void onClick(View v) { 
    imageView.drawCircle(); 
} 
} 

自定義視圖:

public class CustomView extends ImageView { 

private Canvas mCanvas; 
private Paint mBitmapPaint; 
private Paint mPaint; 
private Bitmap mBitmap; 

public CustomView(Context context) { 
    super(context); 
    initialize(); 
} 

public CustomView(Context context, AttributeSet attrs) { 
    super(context, attrs); 
    initialize(); 
} 

public CustomView(Context context, AttributeSet attrs, int defStyle) { 
    super(context, attrs, defStyle); 
    initialize(); 
} 

private void initialize() { 
    if (!isInEditMode()) {  
     mBitmapPaint = new Paint(Paint.DITHER_FLAG);    
     mBitmapPaint.setStyle(Style.STROKE); 

     mPaint = new Paint(); 
     mPaint.setStyle(Style.FILL); 
     mPaint.setStrokeWidth(50); 
     mPaint.setMaskFilter(null);    
     mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR)); 

     reset(); 
    } 
} 

public void reset(){ 
    Bitmap bmp = ((BitmapDrawable)getDrawable()).getBitmap(); 
    mBitmap = bmp.copy(Bitmap.Config.ARGB_8888, true); 

    this.setVisibility(View.VISIBLE); 

    mCanvas = new Canvas(mBitmap); 
} 

public void drawCircle(){ 
    mCanvas.drawCircle(this.getWidth()/2, this.getHeight()/2, this.getWidth()/2, mPaint); 

    //this.setImageBitmap(mBitmap); 
    this.invalidate(); 
} 

@Override 
protected void onDraw(Canvas canvas) { 
    if (isInEditMode()) { 
     super.onDraw(canvas); 
    } 
    else { 
     //super.onDraw(canvas); 
      mBitmapPaint.setColor(0xFF000000); 
      if(mBitmap != null){ 
      canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);   
       } 
     } 
    } 

    } 

通知評論部分,你可以只設置你的透明圖像繪製或重繪像我一樣,這取決於你的其他需求。另請注意,PNG圖像已經有Alpha通道。

希望這可以幫助和享受您的工作。