android圆角矩形背景

Android平台

使用方便

StateRoundRectDrawable mRoundRectDradable = new StateRoundRectDrawable(int normalCorlor, int pressColor);

mRoundRectDradable.setBottomLeftRadius(0);

mRoundRectDradable.setBottomRightRadius(0); .............

xxxxx.setBackgroundDrawable(mRoundRectDradable);

[1].[代码] [Java]代码 跳至 [1] [2]

?


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

import

android.graphics.Canvas;

import

android.graphics.Color;

import

android.graphics.ColorFilter;

import

android.graphics.Paint;

import

android.graphics.Rect;

import

android.graphics.drawable.Drawable;

import

android.graphics.drawable.shapes.RoundRectShape;

import

android.view.MotionEvent;

public

class

RoundRectDradable
extends

Drawable{

    private

static

final

float

DEFAULT_RADIUS =
6.f;

    private

Paint mPaint =
new

Paint();

    private

RoundRectShape mShape;

    private

float
[]
mOuter;

    private

int

mColor;

    private

int

mPressColor;

    private

float

mTopLeftRadius = DEFAULT_RADIUS;

    private

float

mTopRightRadius = DEFAULT_RADIUS;

    private

float

mBottomLeftRadius = DEFAULT_RADIUS;

    private

float

mBottomRightRadius = DEFAULT_RADIUS;

    public

RoundRectDradable() {

        mColor
= Color.WHITE;

        mPressColor
= Color.WHITE;

        mPaint.setColor(mColor);

        mPaint.setAntiAlias(true);

    }

    

    public

float

getTopLeftRadius() {

        return

mTopLeftRadius;

    }

    public

void

setTopLeftRadius(
float

topLeftRadius) {

        this.mTopLeftRadius
= topLeftRadius;

    }

    public

float

getTopRightRadius() {

        return

mTopRightRadius;

    }

    public

void

setTopRightRadius(
float

topRightRadius) {

        this.mTopRightRadius
= topRightRadius;

    }

    public

float

getBottomLeftRadius() {

        return

mBottomLeftRadius;

    }

    public

void

setBottomLeftRadius(
float

bottomLeftRadius) {

        this.mBottomLeftRadius
= bottomLeftRadius;

    }

    public

float

getBottomRightRadius() {

        return

mBottomRightRadius;

    }

    public

void

setBottomRightRadius(
float

bottomRightRadius) {

        this.mBottomRightRadius
= bottomRightRadius;

    }

    

    public

int

getPressColor() {

        return

mPressColor;

    }

    public

void

setPressColor(
int

pressColor) {

        this.mPressColor
= pressColor;

    }

    @Override

    protected

void

onBoundsChange(Rect bounds) {

        super.onBoundsChange(bounds);

        refreshShape();

        mShape.resize(bounds.right
- bounds.left, bounds.bottom - bounds.top);

    }

    

    private

void

refreshShape(){

        mOuter
=
new

float
[]{mTopLeftRadius,
mTopLeftRadius

                ,
mTopRightRadius, mTopRightRadius

                ,
mBottomLeftRadius, mBottomLeftRadius

                ,
mBottomRightRadius, mBottomLeftRadius};

        mShape
=
new

RoundRectShape(mOuter,
null,
null);

    }

    

    public

void

setColor(
int

color){

        mColor
= color;

        mPaint.setColor(color);

    }

    

    @Override

    public

void

draw(Canvas canvas) {

        mShape.draw(canvas,
mPaint);

    }

    @Override

    public

void

setAlpha(
int

alpha) {

        mPaint.setAlpha(alpha);

    }

    

    @Override

    public

void

setColorFilter(ColorFilter cf) {

        mPaint.setColorFilter(cf);

    }

    @Override

    public

int

getOpacity() {

        return

mPaint.getAlpha();

    }

}

[2].[代码] [Java]代码 跳至 [1] [2]

?


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

import

android.graphics.Rect;

import

android.graphics.drawable.StateListDrawable;

public

class

StateRoundRectDrawable
extends

StateListDrawable{

    private

static

final

float

DEFAULT_RADIUS =
6.f;

    private

float

mTopLeftRadius = DEFAULT_RADIUS;

    private

float

mTopRightRadius = DEFAULT_RADIUS;

    private

float

mBottomLeftRadius = DEFAULT_RADIUS;

    private

float

mBottomRightRadius = DEFAULT_RADIUS;

    private

int

mNormalColor;

    private

int

mPressedColor;

    private

RoundRectDradable mNormalDradable;

    private

RoundRectDradable mPressedDradable;

    public

StateRoundRectDrawable(
int

normalCorlor,
int

pressColor) {

        this.mNormalColor
= normalCorlor;

        this.mPressedColor
= pressColor;

    }

    

    @Override

    protected

void

onBoundsChange(Rect bounds) {

        if(mNormalDradable
==
null){

            mNormalDradable
=
new

RoundRectDradable();

            mNormalDradable.setTopLeftRadius(mTopLeftRadius);

            mNormalDradable.setTopRightRadius(mTopRightRadius);

            mNormalDradable.setBottomLeftRadius(mBottomLeftRadius);

            mNormalDradable.setBottomRightRadius(mBottomRightRadius);

            mNormalDradable.setColor(mNormalColor);

            mNormalDradable.onBoundsChange(bounds);

        }

        if(mPressedDradable
==
null){

            mPressedDradable
=
new

RoundRectDradable();

            mPressedDradable.setTopLeftRadius(mTopLeftRadius);

            mPressedDradable.setTopRightRadius(mTopRightRadius);

            mPressedDradable.setBottomLeftRadius(mBottomLeftRadius);

            mPressedDradable.setBottomRightRadius(mBottomRightRadius);

            mPressedDradable.setColor(mPressedColor);

            mPressedDradable.onBoundsChange(bounds);

        }

        this.addState(new

int
[]{-android.R.attr.state_pressed},
mNormalDradable);

        this.addState(new

int
[]{android.R.attr.state_pressed},
mPressedDradable);

    }

    

    public

float

getTopLeftRadius() {

        return

mTopLeftRadius;

    }

    public

void

setTopLeftRadius(
float

topLeftRadius) {

        this.mTopLeftRadius
= topLeftRadius;

    }

    public

float

getTopRightRadius() {

        return

mTopRightRadius;

    }

    public

void

setTopRightRadius(
float

topRightRadius) {

        this.mTopRightRadius
= topRightRadius;

    }

    public

float

getBottomLeftRadius() {

        return

mBottomLeftRadius;

    }

    public

void

setBottomLeftRadius(
float

bottomLeftRadius) {

        this.mBottomLeftRadius
= bottomLeftRadius;

    }

    public

float

getBottomRightRadius() {

        return

mBottomRightRadius;

    }

    public

void

setBottomRightRadius(
float

bottomRightRadius) {

        this.mBottomRightRadius
= bottomRightRadius;

    }

    public

int

getNormalColor() {

        return

mNormalColor;

    }

    public

void

setNormalColor(
int

normalColor) {

        this.mNormalColor
= normalColor;

    }

    public

int

getPressedColor() {

        return

mPressedColor;

    }

    public

void

setPressedColor(
int

pressedColor) {

        this.mPressedColor
= pressedColor;

    }

    

}

时间: 2024-08-05 23:46:57

android圆角矩形背景的相关文章

android圆角矩形有背景颜色

android 圆角矩形,渐变颜色,自定义 首先,在drawable目录下写一个xml,名字随便起(只要符合规范),代码如下: <span style="color:#009900;"><span style="background-color: rgb(255, 255, 255);"><?xml version="1.0" encoding="utf-8"?> <shape xml

android圆角矩形进度条

最近做项目,有个一个需求,就是圆角进度条.效果图如下. 当时项目时间很紧,没多去想怎么实现最佳,就直接把美工给的圆角进度条裁剪成了四份.来做 Canvas 剪切绘制.这样虽然也能达到效果,但是服用性很差.最近网上搜索了很长时间,发现Paint画笔,有遮挡层的功能.android.graphics.Paint.setXfermode(Xfermode xfermode) .其中一个参数就是 Mode.DST_OUT 显示底图与上层图非交集的底图图像.于是就有个思路,先绘制圆角矩形进度条,然后设置画

安卓如何设置控件的圆角矩形背景

当我们开发过程中需要实现控件的圆角矩形或者给背景添加边框时,最佳的办法不是生成背景图片或者添加ImageView当做边界,而是利用Shape来做. 思路就是在drawable文件夹中自定义一个美化布局,代码如下 <?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"> &

Android圆角矩形创建工具RoundRect类

用于把普通图片转换为圆角图像的工具类RoundRect类(复制即可使用): import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.

android圆角矩形有边框无背景色

我们经常要用到圆角矩形,这也是现在的流行的样式..但是今天在工作中,遇到要写圆角矩形有边框但无背景色的button,本来想让妹子帮忙切图的,但是不想麻烦妹子,自己撑着,说可以做,结果弄了老半天菜搞定的,现在给大家看看,以后避免这麻烦...() <?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/and

android圆角矩形的实现

在res/drawable目录下穿件shape的xml来实现圆角矩形效果.代码如下: <?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" > <corners android:bottomL

android圆角矩形出现四个角黑点的解决方法

只要在布局对应的activity的oncreate方法的第一行加上 getWindow().setBackgroundDrawableResource(android.R.color.transparent); 即可解决该问题

Android中实现圆角矩形及半透明效果。

注:本文由Colin撰写,版权所有!转载请注明原文地址,谢谢合作! 在做Android开发时,我们为了美观,有时候需要使用圆角矩形,或半透明之类的效果,在网页设计中很容易实现.但在Android开发中,要稍微麻烦一点,但实现起来也不算很难. 关于设定背景图片平铺的方法请参考上一篇文章:http://itcolin.com/archives/1153.html 一.首先,需要在drawable-mdpi目录里定义一个xml文件,我命名为frame 编写如下代码,其中corners 中定义每边的圆角

Android中设定背景图片平铺。

注:本文由Colin撰写,版权所有!转载请注明原文地址,谢谢合作! 在做Android开发时,我们常常需要为程序设定一个背景,但由于现在的Android设备尺寸不一,如果随便设置一个图片为背景,那么很可能在不同的设备上会被拉伸,给用户的视觉体验会很差,那么如何解决这个问题呢,这里本菜鸟在此分享一下心得. 一.首先,需要在drawable-mdpi目录里定义一个xml文件,在此我命名为bitmap 编写如下代码,src中指定为你要平铺图片的名称: <?xml version="1.0&quo