android 自定义圆角button效果的实例代码(自定义view Demo)

前端之家收集整理的这篇文章主要介绍了android 自定义圆角button效果的实例代码(自定义view Demo)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

概述

在平时开发过程中经常会碰到需要使用圆角button的情况,一般也会包括很多其他小功能,比如要在里面添加img,设置不同的圆角大小等。

针对这样的场景,直接使用创建多个shape,定义多个xml文件也是可以实现的。但是如果使用非常频繁,那么直接自定义一个就会来的非常方便。

甚至在一些情况下,不是可以用shape定义的规则图形,比如需要用到贝塞尔曲线等。
如果全局需要这样风格的view,那么自定义一个View是非常必要的。

本文主要是个demo记录,如有需要的读者可以借鉴学习。

Demo

主要实现功能

  1. 自定义圆角大小
  2. 支持设置leftDrawable,和自定义文字内容文字和img默认居中)
  3. 支持点击效果

android 自定义圆角button效果的实例代码(自定义view Demo)


源码

android 自定义圆角button效果的实例代码(自定义view Demo)


RoundRadiusButton.java

  1. /**
  2. * author: xujiajia
  3. * description:
  4. * 1、drawable只有在设置textString的时候才会生效(居中效果两个一起测量)
  5. */
  6. public class RoundRadiusButton extends View {
  7. //data
  8. private int width = 0;
  9. private int height = 0;
  10. private int roundRadius = 16;
  11. private int bgColor = Color.LTGRAY;
  12. private boolean isTouching = false;
  13. //img and text
  14. private Drawable leftDrawable = null;
  15. private int drawableWidth = 20;
  16. private int drawableHeight = 20;
  17. private int leftDrawablePaddingRight = 0;
  18. private String textString;
  19. private int textSize = 30;
  20. private int textColor = Color.BLACK;
  21. //onDraw
  22. Paint paint;
  23. Path path;
  24. RectF rectF;
  25. Rect rect;
  26. public RoundRadiusButton(Context context,int width,int height) {
  27. super(context);
  28. this.width = width;
  29. this.height = height;
  30. this.setLayoutParams(new ViewGroup.LayoutParams(width,height));
  31. this.setClickable(true);
  32. }
  33. public RoundRadiusButton(Context context,AttributeSet attrs) {
  34. super(context,attrs);
  35. getDataFromAttrs(context,attrs);
  36. this.setClickable(true);
  37. }
  38. public RoundRadiusButton(Context context,AttributeSet attrs,int defStyleAttr) {
  39. super(context,attrs,defStyleAttr);
  40. getDataFromAttrs(context,attrs);
  41. this.setClickable(true);
  42. }
  43. private void getDataFromAttrs(Context context,AttributeSet attrs) {
  44. if (attrs == null) {
  45. return;
  46. }
  47. TypedArray ta = context.obtainStyledAttributes(attrs,R.styleable.RoundRadiusButton);
  48. roundRadius = ta.getDimensionPixelOffset(R.styleable.RoundRadiusButton_roundRadius,16);
  49. bgColor = ta.getColor(R.styleable.RoundRadiusButton_bgColor,Color.LTGRAY);
  50. leftDrawable = ta.getDrawable(R.styleable.RoundRadiusButton_leftDrawable);
  51. drawableWidth = ta.getDimensionPixelOffset(R.styleable.RoundRadiusButton_drawableWidth,0);
  52. drawableHeight = ta.getDimensionPixelOffset(R.styleable.RoundRadiusButton_drawableHeight,0);
  53. leftDrawablePaddingRight =
  54. ta.getDimensionPixelOffset(R.styleable.RoundRadiusButton_leftDrawablePaddingRight,0);
  55. textString = ta.getString(R.styleable.RoundRadiusButton_textString);
  56. textSize = ta.getDimensionPixelOffset(R.styleable.RoundRadiusButton_textSize,0);
  57. textColor = ta.getColor(R.styleable.RoundRadiusButton_textColor,Color.BLACK);
  58. ta.recycle();
  59. }
  60. public void setRoundRadius(int roundRadius) {
  61. this.roundRadius = roundRadius;
  62. invalidate();
  63. }
  64. public void setBgColor(int bgColor) {
  65. this.bgColor = bgColor;
  66. invalidate();
  67. }
  68. public void setLeftDrawable(Drawable leftDrawable,int drawableWidth,int drawableHeight,int paddingRight) {
  69. this.leftDrawable = leftDrawable;
  70. this.drawableWidth = drawableWidth;
  71. this.drawableHeight = drawableHeight;
  72. this.leftDrawablePaddingRight = paddingRight;
  73. invalidate();
  74. }
  75. public void setTextString(String textString) {
  76. this.textString = textString;
  77. invalidate();
  78. }
  79. public void setTextColor(int textColor) {
  80. this.textColor = textColor;
  81. invalidate();
  82. }
  83. public void setTextSize(int textSize) {
  84. this.textSize = textSize;
  85. invalidate();
  86. }
  87. @Override public boolean onTouchEvent(MotionEvent event) {
  88. if (isClickable()) {
  89. switch (event.getAction()) {
  90. case MotionEvent.ACTION_DOWN:
  91. isTouching = true;
  92. invalidate();
  93. break;
  94. case MotionEvent.ACTION_UP:
  95. isTouching = false;
  96. invalidate();
  97. break;
  98. }
  99. }
  100. return super.onTouchEvent(event);
  101. }
  102. @Override protected void onDraw(Canvas canvas) {
  103. super.onDraw(canvas);
  104. if (width == 0 || height == 0) {
  105. width = getWidth();
  106. height = getHeight();
  107. }
  108. if (paint == null) {
  109. paint = new Paint();
  110. }
  111. if (path == null) {
  112. path = new Path();
  113. }
  114. if (rectF == null) {
  115. rectF = new RectF();
  116. }
  117. if (rect == null) {
  118. rect = new Rect();
  119. }
  120. paint.setColor(bgColor);
  121. paint.setAntiAlias(true);//抗锯齿
  122. paint.setStrokeWidth(0);//线的宽度设为0,避免画圆弧的时候部分圆弧与边界相切
  123. paint.setStyle(Paint.Style.FILL_AND_STROKE);
  124. path.setFillType(Path.FillType.WINDING);
  125. //左上圆角
  126. path.moveTo(0,roundRadius);
  127. rectF.set(0,2 * roundRadius,2 * roundRadius);
  128. path.addArc(rectF,180,90);
  129. //上边
  130. path.lineTo(width - roundRadius,0);
  131. //右上圆角
  132. rectF.set(width - roundRadius * 2,width,roundRadius * 2);
  133. path.addArc(rectF,-90,90);
  134. //右边
  135. path.lineTo(width,height - roundRadius);
  136. //右下圆角
  137. rectF.set(width - roundRadius * 2,height - roundRadius * 2,height);
  138. path.addArc(rectF,90);
  139. //下边
  140. path.lineTo(roundRadius,height);
  141. //左下圆角
  142. rectF.set(0,90,90);
  143. //左边
  144. path.lineTo(0,roundRadius);
  145. path.close();
  146. canvas.drawPath(path,paint);
  147. if (isTouching) {
  148. paint.setColor(getContext().getResources().getColor(R.color.black_tran_30));
  149. canvas.drawPath(path,paint);
  150. }
  151. //填充背景中间空白的部分
  152. path.moveTo(0,roundRadius);
  153. path.lineTo(width - roundRadius,0);
  154. path.lineTo(width,height - roundRadius);
  155. path.lineTo(roundRadius,height);
  156. path.close();
  157. canvas.drawPath(path,paint);
  158. }
  159. //text,drawable两个一起计算位置
  160. if (!TextUtils.isEmpty(textString)) {
  161. paint.setStrokeWidth(1.5f);
  162. paint.setColor(textColor);
  163. paint.setTextSize(textSize);
  164. rect.setEmpty();
  165. paint.getTextBounds(textString,textString.length(),rect);
  166. float leftBitmap = 0;
  167. float topBitmap = 0;
  168. if (leftDrawable != null) {
  169. if (leftDrawable != null) {
  170. leftBitmap = (1.0f * width - drawableWidth - rect.width() - leftDrawablePaddingRight) / 2;
  171. topBitmap = (1.0f * height - drawableHeight) / 2;
  172. leftDrawable.setBounds((int) leftBitmap,(int) topBitmap,(int) (leftBitmap + drawableWidth),(int) (topBitmap + drawableHeight));
  173. leftDrawable.draw(canvas);
  174. }
  175. }
  176. float textX = 0;
  177. float textY =
  178. 1.0f * height / 2 + paint.getTextSize() / 2 - paint.getFontMetrics().descent / 2;
  179. if (leftBitmap == 0 && topBitmap == 0) {
  180. textX = width / 2 - rect.width() / 2;
  181. } else {
  182. textX = leftBitmap + drawableWidth + leftDrawablePaddingRight;
  183. }
  184. canvas.drawText(textString,textX,textY,paint);
  185. }
  186. }
  187. }

MainActivity.java

  1. public class MainActivity extends AppCompatActivity {
  2. private LinearLayout llContainer;
  3. @Override
  4. protected void onCreate(Bundle savedInstanceState) {
  5. super.onCreate(savedInstanceState);
  6. setContentView(R.layout.activity_main);
  7. initView();
  8. }
  9. private void initView() {
  10. llContainer = findViewById(R.id.ll_container);
  11. RoundRadiusButton roundRadiusButton = new RoundRadiusButton(this,500,200);
  12. roundRadiusButton.setBgColor(Color.LTGRAY);
  13. roundRadiusButton.setRoundRadius(40);
  14. //text
  15. roundRadiusButton.setTextString("testtesttest");
  16. roundRadiusButton.setTextColor(Color.WHITE);
  17. roundRadiusButton.setTextSize(40);
  18. //drawable
  19. roundRadiusButton.setLeftDrawable(getResources().getDrawable(R.mipmap.ic_launcher),60,80);
  20. roundRadiusButton.setOnClickListener(new View.OnClickListener() {
  21. @Override public void onClick(View v) {
  22. Toast.makeText(MainActivity.this,"testest",Toast.LENGTH_LONG).show();
  23. }
  24. });
  25. roundRadiusButton.setClickable(false);
  26. llContainer.addView(roundRadiusButton);
  27. }
  28. }

activity_main.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:id="@+id/ll_container"
  6. android:layout_width="match_parent"
  7. android:layout_height="match_parent"
  8. android:background="#868684"
  9. android:gravity="center"
  10. android:orientation="vertical"
  11. tools:context=".MainActivity"
  12. >
  13. <com.example.newbuttiontest.RoundRadiusButton
  14. android:layout_width="300dp"
  15. android:layout_height="200dp"
  16. app:bgColor="#FFEB3B"
  17. app:drawableHeight="18dp"
  18. app:drawableWidth="18dp"
  19. app:leftDrawable="@mipmap/ic_launcher"
  20. app:leftDrawablePaddingRight="5dp"
  21. app:roundRadius="30dp"
  22. app:textColor="#FF4329"
  23. app:textSize="16dip"
  24. app:textString="testtesttest"
  25. />
  26. </LinearLayout>

attrs.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <resources>
  3. <declare-styleable name="RoundRadiusButton">
  4. <attr name="roundRadius" format="dimension" />
  5. <attr name="bgColor" format="color" />
  6. <attr name="leftDrawable" format="reference" />
  7. <attr name="leftDrawablePaddingRight" format="dimension" />
  8. <attr name="drawableWidth" format="dimension" />
  9. <attr name="drawableHeight" format="dimension" />
  10. <attr name="textString" format="string" />
  11. <attr name="textSize" format="dimension" />
  12. <attr name="textColor" format="color" />
  13. </declare-styleable>
  14. </resources>

colors.xml

  1. <resources>
  2. <color name="black_tran_30">#30000000</color>
  3. </resources>

总结

以上所述是小编给大家介绍的android 自定义圆角button效果的实例代码,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对我们网站的支持
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!

猜你在找的Android相关文章