【问题标题】:Applying a mask to a single overlay view without masking the entire activity将遮罩应用到单个覆盖视图而不遮罩整个活动
【发布时间】:2012-10-30 08:58:28
【问题描述】:

我有一个绿色背景的简单活动,我试图提供一个带有透明圆形区域的红色叠加层。这是我想要达到的效果:

Expected Image

根据我在互联网上找到的代码,我看到的是这样的:

Result of code

似乎正在发生的是 PorterDuff 将自身应用于活动中的所有视图,而不是我明确告诉它的视图。 Stack 上的许多帖子都是关于用另一个位图掩盖一个位图,而我正在尝试用一个以编程方式创建的圆形来掩盖视图的一部分。这是我尝试使用的代码:

public class ClippingTestActivity extends Activity {
    private Paint mPaint;
    ClippingView ddd;
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.test);
        View v = new View(this.getBaseContext());
        v.setBackgroundColor(Color.GREEN);
        this.addContentView(v, new  LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT));

         ClippingView r = new ClippingView(this.getBaseContext());
        r.setBackgroundColor(Color.RED);   
        this.addContentView(r, new   LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT));
    }

}

    public class ClippingView extends View {
    Paint paint = new Paint();
        Path path = new Path();
        public ClippingView(Context context) {
    super(context);
    }
    @Override
    public void onDraw(Canvas canvas) {
    super.onDraw( canvas );

        paint.setColor(Color.TRANSPARENT);
        paint.setStyle(Style.FILL);

        paint.setXfermode( new PorterDuffXfermode( Mode.CLEAR ) );
        int cx = 200;
        int cy = 200;
        int radius = 50;
        canvas.drawCircle( cx, cy, radius, paint );
    }
}

布局xml文件就是这样

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:app="http://schemas.android.com/apk/res/com.appspot.scruffapp" 
 android:layout_width="fill_parent" 
 android:layout_height="fill_parent"         
 android:layout_centerHorizontal="true"
 android:background="#ff0000"> 
 </RelativeLayout> 

有谁知道如何实现这个效果?

【问题讨论】:

    标签: android android-layout view layer clipping


    【解决方案1】:

    好的。在朋友的帮助下,我想通了。结果我需要创建一个空的新画布,并将其从一个函数传递到另一个函数,然后在一个稍后添加的新视图上绘制画布。这是对我有用的代码:

    Bitmap circle = DrawView.makeCircle(drawable);
    Bitmap overlayBg = DrawView.makeOverlayBg(canvas.getWidth(),canvas.getHeight());
    
    Bitmap finalImage = Bitmap.createBitmap(canvas.getWidth(),canvas.getHeight(), Bitmap.Config.ARGB_8888);
    
    final android.graphics.Canvas tmpCanvas = new android.graphics.Canvas(finalImage);
    tmpCanvas.drawBitmap(overlayBg, 0, 0, null);
    
    final android.graphics.Paint paint = new android.graphics.Paint();
    paint.setXfermode(new android.graphics.PorterDuffXfermode(Mode.DST_OUT));
    tmpCanvas.drawBitmap(circle, cx - radius, cy - radius, paint);
    
    canvas.drawBitmap(finalImage, 0, 0, null); 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-08-29
      • 1970-01-01
      • 1970-01-01
      • 2017-09-28
      • 1970-01-01
      • 2012-11-06
      • 2015-03-23
      相关资源
      最近更新 更多