2023年7月15日发(作者:)
android⾃定义圆⾓button效果的实例代码(⾃定义viewDemo)概述在平时开发过程中经常会碰到需要使⽤圆⾓button的情况,⼀般也会包括很多其他⼩功能,⽐如要在⾥⾯添加img,设置不同的圆⾓⼤⼩等。针对这样的场景,直接使⽤创建多个shape,定义多个xml⽂件也是可以实现的。但是如果使⽤⾮常频繁,那么直接⾃定义⼀个就会来的⾮常⽅便。甚⾄在⼀些情况下,不是可以⽤shape定义的规则图形,⽐如需要⽤到贝塞尔曲线等。如果全局需要这样风格的view,那么⾃定义⼀个View是⾮常必要的。本⽂主要是个demo记录,如有需要的读者可以借鉴学习。Demo主要实现功能:1. ⾃定义圆⾓⼤⼩2. ⽀持设置leftDrawable,和⾃定义⽂字内容(⽂字和img默认居中)3. ⽀持点击效果源码/** * author: xujiajia * description: * 1、drawable只有在设置textString的时候才会⽣效(居中效果两个⼀起测量) */public class RoundRadiusButton extends View { //data private int width = 0; private int height = 0; private int roundRadius = 16; private int bgColor = ; private boolean isTouching = false; //img and text private Drawable leftDrawable = null; private int drawableWidth = 20; private int drawableHeight = 20; private int leftDrawablePaddingRight = 0; private String textString; private int textSize = 30; private int textColor = ; //onDraw Paint paint; Path path; RectF rectF; Rect rect; public RoundRadiusButton(Context context, int width, int height) { super(context); = width; = height; outParams(new Params(width, height)); ckable(true); } public RoundRadiusButton(Context context, AttributeSet attrs) { super(context, attrs); getDataFromAttrs(context, attrs); ckable(true); } public RoundRadiusButton(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); getDataFromAttrs(context, attrs); ckable(true); } private void getDataFromAttrs(Context context, AttributeSet attrs) { if (attrs == null) { return; } TypedArray ta = StyledAttributes(attrs, adiusButton); roundRadius = ensionPixelOffset(adiusButton_roundRadius, 16); bgColor = or(adiusButton_bgColor, ); leftDrawable = wable(adiusButton_leftDrawable); drawableWidth = ensionPixelOffset(adiusButton_drawableWidth, 0); drawableHeight = ensionPixelOffset(adiusButton_drawableHeight, 0); leftDrawablePaddingRight = ensionPixelOffset(adiusButton_leftDrawablePaddingRight, 0); textString = ing(adiusButton_textString); textSize = ensionPixelOffset(adiusButton_textSize, 0); textColor = or(adiusButton_textColor, ); e(); } public void setRoundRadius(int roundRadius) { adius = roundRadius; invalidate(); } public void setBgColor(int bgColor) { r = bgColor; invalidate(); } public void setLeftDrawable(Drawable leftDrawable, int drawableWidth, int drawableHeight, int paddingRight) { awable = leftDrawable; leWidth = drawableWidth; leHeight = drawableHeight; awablePaddingRight = paddingRight; invalidate(); } public void setTextString(String textString) { ring = textString; invalidate(); } public void setTextColor(int textColor) { lor = textColor; invalidate(); } public void setTextSize(int textSize) { ze = textSize; invalidate(); } @Override public boolean onTouchEvent(MotionEvent event) { if (isClickable()) { switch (ion()) { case _DOWN: isTouching = true; invalidate(); break; case _UP: isTouching = false; invalidate(); break; } } return hEvent(event); } @Override protected void onDraw(Canvas canvas) { (canvas); if (width == 0 || height == 0) { width = getWidth(); height = getHeight(); } if (paint == null) { paint = new Paint(); } if (path == null) { path = new Path(); } if (rectF == null) { rectF = new RectF(); } if (rect == null) { rect = new Rect(); } or(bgColor); iAlias(true);//抗锯齿 okeWidth(0);//线的宽度设为0,避免画圆弧的时候部分圆弧与边界相切 le(_AND_STROKE); lType(G); //左上圆⾓ (0, roundRadius); (0, 0, 2 * roundRadius, 2 * roundRadius); (rectF, 180, 90); //上边 (width - roundRadius, 0); //右上圆⾓ (width - roundRadius * 2, 0, width, roundRadius * 2); (rectF, -90, 90); //右边 (width, height - roundRadius); //右下圆⾓ (width - roundRadius * 2, height - roundRadius * 2, width, height); (rectF, 0, 90); //下边 (roundRadius, height); //左下圆⾓ (0, height - roundRadius * 2, 2 * roundRadius, height); (rectF, 90, 90); //左边 (0, roundRadius); (); th(path, paint); if (isTouching) { or(getContext().getResources().getColor(_tran_30)); th(path, paint); } //填充背景中间空⽩的部分 (0, roundRadius); (width - roundRadius, 0); (width, height - roundRadius); (roundRadius, height); (); th(path, paint); if (isTouching) { or(getContext().getResources().getColor(_tran_30)); th(path, paint); } //text, drawable两个⼀起计算位置 if (!y(textString)) { okeWidth(1.5f); or(textColor); tSize(textSize); ty(); tBounds(textString, 0, (), rect); float leftBitmap = 0; float topBitmap = 0; if (leftDrawable != null) { if (leftDrawable != null) { leftBitmap = (1.0f * width - drawableWidth - () - leftDrawablePaddingRight) / 2; topBitmap = (1.0f * height - drawableHeight) / 2; nds((int) leftBitmap, (int) topBitmap, (int) (leftBitmap + drawableWidth), (int) (topBitmap + drawableHeight)); (canvas); } } float textX = 0; float textY = 1.0f * height / 2 + tSize() / 2 - tMetrics().descent / 2; if (leftBitmap == 0 && topBitmap == 0) { textX = width / 2 - () / 2; } else { textX = leftBitmap + drawableWidth + leftDrawablePaddingRight; } xt(textString, textX, textY, paint); } }}blic class MainActivity extends AppCompatActivity { private LinearLayout llContainer; @Override protected void onCreate(Bundle savedInstanceState) { te(savedInstanceState); setContentView(ty_main); initView(); } private void initView() { llContainer = findViewById(_container); RoundRadiusButton roundRadiusButton = new RoundRadiusButton(this, 500, 200); olor(); ndRadius(40); //text tString("testtesttest"); tColor(); tSize(40); //drawable tDrawable(getResources().getDrawable(_launcher), 60, 60, 80); lickListener(new kListener() { @Override public void onClick(View v) { xt(, "testest", _LONG).show(); } }); ckable(false); w(roundRadiusButton); }}activity_
发布者:admin,转转请注明出处:http://www.yc00.com/web/1689429869a246885.html
评论列表(0条)