Android   发布时间:2022-04-28  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了android – 将掩码应用于单个叠加视图而不屏蔽整个活动大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个绿色背景的简单活动,我试图提供一个透明圆形区域的红色覆盖.这是我想要完成的效果

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://scheR_112_11845@as.android.com/apk/res/android"
 xmlns:app="http://scheR_112_11845@as.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>

有谁知道如何实现这种效果

解决方法

好的.在朋友的帮助下,我明白了.事实证明我需要创建一个空的新画布并将其从一个函数传递到另一个函数,然后在新视图上绘制画布,稍后再添加.这是适合我的代码

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,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,null);

大佬总结

以上是大佬教程为你收集整理的android – 将掩码应用于单个叠加视图而不屏蔽整个活动全部内容,希望文章能够帮你解决android – 将掩码应用于单个叠加视图而不屏蔽整个活动所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。