android圆角遮罩view,安卓圆角、背景遮罩。覆盖实现方式(适用于所有控件)

packagecom.etwod.yulin.t4.unit;importandroid.content.Context;importandroid.content.res.TypedArray;importandroid.graphics.Bitmap;importandroid.graphics.Canvas;importandroid.graphics.Paint;importandroid.graphics.PorterDuff;importandroid.graphics.PorterDuffXfermode;importandroid.graphics.RectF;importandroid.support.annotation.ColorInt;importandroid.util.AttributeSet;importandroid.view.View;importcom.etwod.yulin.android.R;/*** A circular cover view.Above other views to hide the conner.You can set radians by self.

* Created by

* 磊磊tua

* 覆盖形成圆角*/

public class CircularCoverView extendsView {private int leftTopRadians = 30; //leftTopRadians

private int leftBottomRadians = 30; //leftBottomRadians

private int rightTopRadians = 30; //rightTopRadians

private int rightBottomRadians = 30; //rightBottomRadians

private int coverColor = 0xffeaeaea; //color of cover.

publicCircularCoverView(Context context) {this(context, null, 0);

}publicCircularCoverView(Context context, AttributeSet attrs) {this(context, attrs, 0);

}public CircularCoverView(Context context, AttributeSet attrs, intdefStyleAttr) {super(context, attrs, defStyleAttr);

TypedArray typedArray=context.obtainStyledAttributes(attrs, R.styleable.CircularCoverView);

leftTopRadians=typedArray.getDimensionPixelSize(R.styleable.CircularCoverView_left_top_radius, leftTopRadians);

leftBottomRadians=typedArray.getDimensionPixelSize(R.styleable.CircularCoverView_left_bottom_radius, leftBottomRadians);

rightTopRadians=typedArray.getDimensionPixelSize(R.styleable.CircularCoverView_right_top_radius, rightTopRadians);

rightBottomRadians=typedArray.getDimensionPixelSize(R.styleable.CircularCoverView_right_bottom_radius, rightBottomRadians);

coverColor=typedArray.getColor(R.styleable.CircularCoverView_cover_color, coverColor);

}/*** set radians of cover.*/

public void setRadians(int leftTopRadians, int rightTopRadians, int leftBottomRadians, intrightBottomRadians) {this.leftTopRadians =leftTopRadians;this.rightTopRadians =rightTopRadians;this.leftBottomRadians =leftBottomRadians;this.rightBottomRadians =rightBottomRadians;

}/*** set color of cover.

*

*@paramcoverColor cover's color*/

public void setCoverColor(@ColorInt intcoverColor) {this.coverColor =coverColor;

}/*** create a sector-bitmap as the dst.

*

*@paramw width of bitmap

*@paramh height of bitmap

*@returnbitmap*/

private Bitmap drawSector(int w, inth) {

Bitmap bm=Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);

Canvas c= newCanvas(bm);

Paint p= newPaint(Paint.ANTI_ALIAS_FLAG);

p.setColor(0xFFFFCC44);//notice:cannot set transparent color here.otherwise cannot clip at final.

c.drawArc(new RectF(0, 0, leftTopRadians * 2, leftTopRadians * 2), 180, 90, true, p);

c.drawArc(new RectF(0, getHeight() - leftBottomRadians * 2, leftBottomRadians * 2, getHeight()), 90, 90, true, p);

c.drawArc(new RectF(getWidth() - rightTopRadians * 2, 0, getWidth(), rightTopRadians * 2), 270, 90, true, p);

c.drawArc(new RectF(getWidth() - rightBottomRadians * 2, getHeight() - rightBottomRadians * 2, getWidth(), getHeight()), 0, 90, true, p);returnbm;

}/*** create a rect-bitmap as the src.

*

*@paramw width of bitmap

*@paramh height of bitmap

*@returnbitmap*/

private Bitmap drawRect(int w, inth) {

Bitmap bm=Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);

Canvas c= newCanvas(bm);

Paint p= newPaint(Paint.ANTI_ALIAS_FLAG);

p.setColor(coverColor);

c.drawRect(new RectF(0, 0, leftTopRadians, leftTopRadians), p);

c.drawRect(new RectF(0, getHeight() -leftBottomRadians, leftBottomRadians, getHeight()), p);

c.drawRect(new RectF(getWidth() - rightTopRadians, 0, getWidth(), rightTopRadians), p);

c.drawRect(new RectF(getWidth() - rightBottomRadians, getHeight() -rightBottomRadians, getWidth(), getHeight()), p);returnbm;

}

@Overrideprotected voidonDraw(Canvas canvas) {super.onDraw(canvas);

Paint paint= newPaint();

paint.setFilterBitmap(false);

paint.setStyle(Paint.Style.FILL);//create a canvas layer to show the mix-result

int sc = canvas.saveLayer(0, 0, getWidth(), getHeight(), null, Canvas.MATRIX_SAVE_FLAG |Canvas.CLIP_SAVE_FLAG|Canvas.HAS_ALPHA_LAYER_SAVE_FLAG|Canvas.FULL_COLOR_LAYER_SAVE_FLAG|Canvas.CLIP_TO_LAYER_SAVE_FLAG);//draw sector-dst-bitmap at first.

canvas.drawBitmap(drawSector(getWidth(), getHeight()), 0, 0, paint);//set Xfermode of paint.

paint.setXfermode(newPorterDuffXfermode(PorterDuff.Mode.SRC_OUT));//then draw rect-src-bitmap

canvas.drawBitmap(drawRect(getWidth(), getHeight()), 0, 0, paint);

paint.setXfermode(null);//restore the canvas

canvas.restoreToCount(sc);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要给Android页面加上遮罩层,并设置某个控件不被遮罩,你可以按照以下步骤进行: 1. 创建一个遮罩层布局文件(例如`mask_layout.xml`),定义一个半透明的背景和一个覆盖整个屏幕的View: ```xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <View android:id="@+id/mask_view" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#80000000" /> <!-- 其他遮罩层内容 --> </RelativeLayout> ``` 2. 在需要添加遮罩层的Activity的布局文件中,使用`FrameLayout`作为根布局,并将遮罩层布局文件和Activity的内容布局文件添加进去: ```xml <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <!-- 遮罩层布局 --> <include layout="@layout/mask_layout" /> <!-- Activity的内容布局 --> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent"> <!-- 其他控件 --> <!-- 目标控件,不被遮罩 --> <Button android:id="@+id/target_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Target Button" /> <!-- 其他控件 --> </LinearLayout> </FrameLayout> ``` 3. 在Activity的Java代码中,设置目标控件(例如按钮)不受遮罩影响,即为其设置点击事件,并在点击事件中移除遮罩层: ```java public class MainActivity extends AppCompatActivity { private View maskView; private Button targetButton; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); maskView = findViewById(R.id.mask_view); targetButton = findViewById(R.id.target_button); targetButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // 移除遮罩层 maskView.setVisibility(View.GONE); } }); } } ``` 通过以上步骤,你可以在Android页面上添加一个遮罩层,并设置目标控件不受遮罩影响。当目标控件被点击时,移除遮罩层即可。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值