时间:2021-05-20
本文实例讲述了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程序设计有所帮助。
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
本文实例讲述了Android编程实现带渐变效果的圆角矩形。分享给大家供大家参考,具体如下:/***带渐变色效果的圆角矩形**@description:*@aut
一、前言Android实现圆角矩形,圆形或者椭圆等图形,一般主要是个自定义View加上使用Xfermode实现的。实现圆角图片的方法其实不少,常见的就是利用Xf
本文实例讲述了Android开发基于Drawable实现圆角矩形的方法。分享给大家供大家参考,具体如下:第一步:写个类继承drawable,重写里面的方法,实现
本文实例讲述了js绘制圆形和矩形的方法。分享给大家供大家参考。具体如下:这里使用js来绘制圆形和矩形,支持选择图形的背景颜色,同时可设置圆角矩形、半径、正圆、矩
画圆角矩形方法英文命令:RoundRect 操作系统支持:Windows所属对象:画板使用画笔在画板上画出一个圆角矩形,圆角矩形的内部使用刷子填充。语法:无返回