当部分透明时,ImageView不会显示基础视图

时间:2014-01-23 11:32:17

标签: android android-layout layout imageview relativelayout

我有一个RelativeLayout,其中ViewImageView覆盖了它。
现在,我正在修改ImageView的位图的一部分,使其透明&使该地区无效 这样可行,但我看不到第一个View下面,只有活动的背景 试图使Viewandroid.R.id.content失效,没有运气。

问题:鉴于View A覆盖View B,如果View B的各个部分变得透明,我如何才能看到View A的部分内容?

3 个答案:

答案 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通道。

希望这有助于并享受您的工作。

答案 1 :(得分: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);

答案 2 :(得分:-1)

尝试将相对布局更改为Framelayout