android实现圆角矩形背景的方法

本文实例讲述了android实现圆角矩形背景的方法。分享给大家供大家参考。具体如下:

1. java代码如下:

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代码如下:

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; } }

希望本文所述对大家的Android程序设计有所帮助。

时间: 2024-08-01 16:02:24

android实现圆角矩形背景的方法的相关文章

android实现圆角矩形背景的方法_Android

本文实例讲述了android实现圆角矩形背景的方法.分享给大家供大家参考.具体如下: 1. java代码如下: import android.graphics.Canvas; import android.graphics.Color; import android.graphics.ColorFilter; import android.graphics.Paint; import android.graphics.Rect; import android.graphics.drawable.

Fireworks 裁切出圆角矩形图片的方法

  相信做网站的朋友肯定知道Fireworks,它是一款图像处理软件,虽然没有Photoshop功能那么强大,但足以满足网页设计的需要.好了,进入今天的主题,在网页中经常看到圆角矩形的图片,很好看,那是怎么做出来的呢?所以今天就教大家用Fireworks 8裁切出圆角矩形图片的方法. 1.用Fireworks 8打开需要进行裁切圆角矩形的图片 2.点击左边工具栏中"矩形"工具右下角的小箭头,选择"圆角矩形",相关截图如下所示: 3.在图片中画一个圆角矩形(此时会在矩

Android实现圆角矩形和圆形ImageView的方式_Android

Android中实现圆角矩形和圆形有很多种方式,其中最常见的方法有ImageLoader设置Option和自定义View. 1.ImageLoader加载图片 public static DisplayImageOptions getRoundOptions() { DisplayImageOptions options = new DisplayImageOptions.Builder() // 是否设置为圆角,弧度为多少,当弧度为90时显示的是一个圆 .displayer(new Round

Android实现圆角边框对话框的方法

前言 最近要实现个圆角边框的对话框设计图,查了网上很多种实现,都差不多,从中得到灵感,实现了另一种方式,利用layer-list: 先来看看实现的效果如下: 首先在drawable目录下定义好圆角背景文件dialog_corner_bg.xml <?xml version="1.0" encoding="utf-8"?> <layer-list xmlns:android="http://schemas.android.com/apk/r

圆角矩形之深入探讨

圆角 首先声明,这个教程仅仅可以作为一个讨论的话题来进行,讨论话题的中心是由一个问题引起的,问题如下:问题:如何才能制作出这样一个矩形框--有两个圆角,两个直角?比如下面的效果图所示: 如果上面的效果图你感到烦乱的话,那么就来看看这个单一的效果图吧:制作的方法有很多,你可以想到有几种呢?我们的答案是:6种! [第一种方案:Mask(遮罩)的灵活运用] 如果是刚刚通过遮罩教程学习的朋友看到这个问题,想必会不经思索的会说:"Easy,使用Mask,就可以轻松实现这种效果!"果真如此吗?请看

Android开发使用自定义View将圆角矩形绘制在Canvas上的方法

本文实例讲述了Android开发使用自定义View将圆角矩形绘制在Canvas上的方法.分享给大家供大家参考,具体如下: 前几天,公司一个项目中,头像图片需要添加圆角,这样UI效果会更好看,于是写了一个小的demo进行圆角的定义,该处主要是使用BitmapShader进行了渲染(如果要将一张图片裁剪成椭圆或圆形显示在屏幕上,也可以使用BitmapShader来完成). BitmapShader类完成渲染图片的基本步骤如下: 1.创建BitmapShader类的对象 /** * Call this

Android编程实现带渐变效果的圆角矩形示例

本文实例讲述了Android编程实现带渐变效果的圆角矩形.分享给大家供大家参考,具体如下: /** * 带渐变色效果的圆角矩形 * * @description: * @author ldm * @date 2016-4-26 下午3:47:12 */ public class RoundRectsActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onC

android中圆角图像生成方法

  这篇文章主要介绍了android中圆角图像生成方法,涉及Android处理圆角图像的技巧,需要的朋友可以参考下 本文实例讲述了android中圆角图像生成方法.分享给大家供大家参考.具体分析如下: 在android开发中为了美观,常常要求ImageView中显示出圆角图像的效果,这个如何实现? 这里总结了网上的最优方法:将图像处理成圆角,然后在加载给ImageView显示,代码如下: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 public Bitmap

Android利用canvas画各种图形(点、直线、弧、圆、椭圆、文字、矩形、多边形、曲线、圆角矩形)

1.首先说一下canvas类: Class Overview The Canvas class holds the "draw" calls. To draw something, you need 4 basic components: A Bitmap to hold the pixels, a Canvas to host the draw calls (writing into the bitmap), a drawing primitive (e.g. Rect, Path,