首页 > 编程知识 正文

linearlayout设置背景,自定义linearlayout

时间:2023-05-03 12:40:08 阅读:143752 作者:2701

首先,请看这个popupwindow。

其实有阴影

请注意,我的高度和宽度加上阴影的宽度240 7 3长341 7 3

安卓: layout _ width=' 250 DP '

安卓: layout _ height=' 351 DP '

Android : background=' @ color/public _ color _ transparent '

app : public _ BG _ color=' @ color/withe '

app:public_effect='7dp '

app:public_nobottom='false '

app:public_noleft='false '

app:public_noright='false '

app:public_offset_x='3dp '

app:public_offset_y='3dp '

app:public_radius='0dp '

app : public _ shaw _ color=' # 33000000 '

复制代码

下面是我的布局代码,下面如果有控件需要阴影效果的话,可以改为相应的控件后使用。

package com.yinke.demon.common RES.view;

import android.content.Context;

import Android.content.RES.typed array;

import android.graphics.Canvas;

import android.graphics.Color;

import android.graphics.Paint;

import android.graphics.RectF;

import Android.util.attributes et;

import android.util.Log;

import Android.widget.linear layout;

import com.yinke.demon.common RES.r;

//*

* @author Drchen

*

*写下阴影的ConstraintLayout

*

如果不设定backGround的话就不能进入onDraw (、

*/

publicclassshawdelinearlayoutextendslinearlayout {

//*

*画笔,在此设定阴影

*/

private Paint paint2;

//*

*背景色

*/

私有int BG _ color;

//*

*阴影颜色

*/

private int shawder_color;

//*

*圆角

*/

private int radius;

//*

*阴影倾斜x

*/

权限int offset _ x;

//*

*阴影斜率y

*/

私有int offset _ y;

//*

*阴影宽度

*/

private int effect;

//*

*布局广泛

*/

private int with;

//*

*布局高

*/

private int higth;

//*

*划出阴影后,布局实际的背景描绘区域,left

*/

私有左;

//*

*画出阴影后,布局实际的背景描绘区域,top

*/

private int top;

//*

*投射阴影后,布局实际的背景绘制区域,right

*/

私有输入权限;

//*

*画出阴影后,布局实际的背景描绘区域,bottom

*/

私有输入底部;

//*

*四边需要画吗

*/

private boolean noleft,notop,noright,nobottom;

privatestring tag=' shawderconstrai

nLayout";

public ShawdeLinearLayout(Context context) {

this(context, null);

}

public ShawdeLinearLayout(Context context, AttributeSet attrs) {

super(context, attrs);

init(attrs);

}

private void init(AttributeSet attrs) {

TypedArray typedArray = getContext().obtainStyledAttributes(attrs, R.styleable.PublicShawderLayout);

/**

* 本项目的radio 不做百分比, 都用dp

*/

radius = typedArray.getDimensionPixelSize(R.styleable.PublicShawderLayout_public_radius, 50);

bg_color = typedArray.getColor(R.styleable.PublicShawderLayout_public_bg_color, Color.BLUE);

shawder_color = typedArray.getColor(R.styleable.PublicShawderLayout_public_shaw_color, Color.BLACK);

//百分比适配

effect = typedArray.getDimensionPixelSize(R.styleable.PublicShawderLayout_public_effect, 20);

offset_X = typedArray.getDimensionPixelOffset(R.styleable.PublicShawderLayout_public_offset_x, 20);

offset_Y = typedArray.getDimensionPixelOffset(R.styleable.PublicShawderLayout_public_offset_y, 20);

notop = typedArray.getBoolean(R.styleable.PublicShawderLayout_public_notop, false);

noleft = typedArray.getBoolean(R.styleable.PublicShawderLayout_public_noleft, false);

noright = typedArray.getBoolean(R.styleable.PublicShawderLayout_public_noright, false);

nobottom = typedArray.getBoolean(R.styleable.PublicShawderLayout_public_nobottom, false);

typedArray.recycle();

paint2 = new Paint();

paint2.setAntiAlias(true);

// 设定颜色

paint2.setColor(bg_color);

// 设定阴影(柔边, X 轴位移, Y 轴位移, 阴影颜色)

// paint2.setShadowLayer(5, 3, 3, );

paint2.setShadowLayer(effect, offset_X, offset_Y, shawder_color);

//这个要开,不然没有阴影

setLayerType(LAYER_TYPE_SOFTWARE, null);

int padd_top;

int padd_left;

int padd_right;

int padd_bottom;

if (noleft) {

padd_left = 0;

} else {

padd_left = offset_X + effect ;

}

if (notop) {

padd_top = 0;

} else {

padd_top = offset_Y + effect ;

}

if (noright) {

padd_right = 0;

} else {

padd_right = offset_Y +effect ;

}

if (nobottom) {

padd_bottom = 0;

} else {

padd_bottom = offset_Y + effect ;

}

setPadding(padd_left, padd_top, padd_right, padd_bottom);

}

@Override

protected void onSizeChanged(int w, int h, int oldw, int oldh) {

super.onSizeChanged(w, h, oldw, oldh);

this.with = w;

this.higth = h;

if (noleft) {

left = 0;

} else {

left = offset_X + effect ;

}

if (notop) {

top = 0;

} else {

top = offset_Y + effect ;

}

if (noright) {

right = with;

} else {

right = with - offset_Y - effect ;

}

if (nobottom) {

bottom = higth;

} else {

bottom = higth - offset_Y - effect ;

}

}

@Override

protected void onDraw(Canvas canvas) {

// super.onDraw(canvas); //去掉减少不必要的绘制

RectF rectF = new RectF(left, top, right, bottom);

canvas.drawRoundRect(rectF, radius, radius, paint2);

}

}

复制代码

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。