当前位置: 首页 > news >正文

Android自定义Drawable---灵活多变的矩形背景

Android自定义Drawable—灵活多变的矩形背景

在安卓开发中,我们通常需要为不同的按钮设置不同的背景以实现不同的效果,有时还需要这些按钮根据实际情况进行变化。如果采用编写resource中xml文件的形式,就需要重复定义许多只有微小变动的资源文件。这使得整个工程的简洁性和可读性受到影响。本文将介绍一种基于java代码实现的矩形背景自定义工具,该工具是继承Drawable的基础上开发的,它具有如下功能:

  1. 自定义矩形的内部填充颜色和边框颜色;
  2. 自定义矩形四角的弧度,支持分别定义和整体定义;
  3. 自定义矩形的阴影(颜色、宽度及位置);
  4. 自定义矩形的触摸水波纹效果(颜色、显示速度);
  5. 自定义矩形的边框宽度(按比例/按固定宽度),并可与ObjectAnimator配合实现动画效果

综合展示如下:
综合效果展示

直接上源码:

public class FlexibleRectDrawable extends Drawable {private Paint paint_stroke;private Paint paint_fill;private Paint paint_ripple;private RectF outerRect;private RectF innerRect;private RectF zeroRect;//矩形内部颜色及边框颜色private int solidColor;private int strokeColor;private int solidColorHolder;private int strokeColorHolder;//边框设置private boolean hasStroke;private float strokeWidth;private float strokeWidthVariable;//可变动的边框宽度,用于生成后调整宽度private float strokeInPercent;//0~1 边框与整体大小的占比//圆角半径private float rectRadius;//圆角位置private int corners;//阴影设置private boolean needShadow;private float shadowRange;//阴影粗细private float shadowDx;//阴影中心x轴偏移private float shadowDy;//阴影中心y轴偏移private int shadowColor;//阴影颜色//阴影位置private float offsetLeft;private float offsetTop;private float offsetRight;private float offsetBottom;//Ripple Effectenum RippleAnimState{STATE_ENTER, STATE_EXIT, STATE_EXIT_LATER}private RippleAnimState ripple_anim_state;private ObjectAnimator ripple_alpha_animator;private ObjectAnimator ripple_radius_animator;private PointF currentPoint;private PointF pressedPoint;private Path ripple_bound_path;private boolean needRipple;private int rippleSpeed;//msprivate float maxRippleRadius;private float rippleRadius;//属性动画private int rippleColor;private int maxRippleAlpha;private int rippleAlpha;//属性动画public static final int SQUARE_CORNER = 0;public static final int CORNER_TOP_LEFT = 1;public static final int CORNER_TOP_RIGHT = 1 << 1;public static final int CORNER_BOTTOM_LEFT = 1 << 2;public static final int CORNER_BOTTOM_RIGHT = 1 << 3;public static final int CORNER_HALF_LEFT = CORNER_TOP_LEFT | CORNER_BOTTOM_LEFT;public static final int CORNER_HALF_RIGHT = CORNER_TOP_RIGHT | CORNER_BOTTOM_RIGHT;public static final int CORNER_ALL = CORNER_TOP_LEFT | CORNER_TOP_RIGHT | CORNER_BOTTOM_LEFT | CORNER_BOTTOM_RIGHT;public enum RectType{BORDER_ONLY,SOLID_BLOCK,BORDERED_BLOCK,NOT_DEFINED}private RectType type;public FlexibleRectDrawable() {//默认值this.type = RectType.NOT_DEFINED;this.solidColor = 0;//透明色this.strokeColor = 0;//透明色this.solidColorHolder = 0;this.strokeColorHolder = 0;this.hasStroke = false;this.strokeWidth = 0;this.strokeWidthVariable = 0;this.corners = SQUARE_CORNER;this.rectRadius = 0;this.needShadow = false;this.shadowRange = 0;this.shadowDx = 0;this.shadowDy = 0;this.shadowColor = Color.parseColor("#aa000000");this.offsetBottom = 0;this.offsetLeft = 0;this.offsetTop = 0;this.offsetRight = 0;this.currentPoint = new PointF();this.pressedPoint = new PointF();this.rippleColor = Color.parseColor("#21000000");this.maxRippleAlpha = this.rippleColor>>24 & 0xFF;this.ripple_bound_path = new Path();}public void setupPainters() {paint_stroke = new Paint();paint_stroke.setAntiAlias(true);paint_stroke.setFilterBitmap(true);paint_stroke.setDither(true);paint_stroke.setStyle(Paint.Style.FILL);paint_stroke.setColor(strokeColor);//设置阴影if(needShadow)paint_stroke.setShadowLayer(shadowRange, shadowDx, shadowDy, shadowColor);paint_fill = new Paint();paint_fill.setAntiAlias(true);paint_fill.setFilterBitmap(true);paint_fill.setDither(true);paint_fill.setStyle(Paint.Style.FILL);paint_fill.setColor(solidColor);//设置水波纹效果paint_ripple = new Paint();paint_ripple.setAntiAlias(true);paint_ripple.setStyle(Paint.Style.FILL);paint_ripple.setColor(rippleColor);invalidateSelf();}@Overrideprotected void onBoundsChange(Rect bounds) {super.onBoundsChange(bounds);if (bounds.right - bounds.left > 0 && bounds.bottom - bounds.top > 0) {int width = bounds.right - bounds.left;int height = bounds.bottom - bounds.top;outerRect = new RectF(offsetLeft, offsetTop, width - offsetRight, height - offsetBottom);innerRect = new RectF(offsetLeft + strokeWidth, offsetTop + strokeWidth,width - offsetRight - strokeWidth, height - offsetBottom - strokeWidth);zeroRect = new RectF(width/2.0f, height/2.0f, width/2.0f, height/2.0f);invalidateSelf();}}@RequiresApi(api = Build.VERSION_CODES.Q)@Overridepublic void draw(Canvas canvas) {float[] Radii = {0,0,0,0,0,0,0,0};if ((corners & CORNER_TOP_LEFT) != 0) {Radii[0] = rectRadius;Radii[1] = rectRadius;}if ((corners & CORNER_TOP_RIGHT) != 0) {Radii[2] = rectRadius;Radii[3] = rectRadius;}if ((corners & CORNER_BOTTOM_RIGHT) != 0) {Radii[4] = rectRadius;Radii[5] = rectRadius;}if ((corners & CORNER_BOTTOM_LEFT) != 0) {Radii[6] = rectRadius;Radii[7] = rectRadius;}switch(type){case BORDER_ONLY:canvas.drawDoubleRoundRect(outerRect, Radii, innerRect, Radii, paint_stroke);break;case SOLID_BLOCK:canvas.drawDoubleRoundRect(outerRect,Radii,zeroRect,Radii, paint_fill);break;case BORDERED_BLOCK:canvas.drawDoubleRoundRect(outerRect, Radii, innerRect, Radii, paint_stroke);canvas.drawDoubleRoundRect(innerRect,Radii,zeroRect,Radii, paint_fill);break;case NOT_DEFINED:throw new RuntimeException("RectType undefined");default:}//draw ripplecanvas.save();ripple_bound_path.addRoundRect(innerRect,Radii,Path.Direction.CW);canvas.clipPath(ripple_bound_path);if(ripple_anim_state == STATE_ENTER){paint_ripple.setAlpha(rippleAlpha);canvas.drawCircle(pressedPoint.x, pressedPoint.y, rippleRadius, paint_ripple);}else if(ripple_anim_state == STATE_EXIT){paint_ripple.setAlpha(rippleAlpha);canvas.drawDoubleRoundRect(innerRect,Radii,zeroRect,Radii, paint_ripple);}canvas.restore();}@Overrideprotected boolean onStateChange(int[] stateSet) {boolean enable = false;boolean pressed = false;for (int st : stateSet) {switch (st) {case android.R.attr.state_pressed:pressed = true;break;case android.R.attr.state_enabled:enable = true;break;}}if (!enable) return false;if (!needRipple)return false;if (pressed) {startRippleAnimation();return true;} else if (ripple_anim_state == STATE_ENTER) {exitRippleAnimation();return true;} else {return false;}}private void startRippleAnimation() {ripple_anim_state = STATE_ENTER;pressedPoint.set(currentPoint);maxRippleRadius = Math.max(innerRect.width(), innerRect.height());if(ripple_radius_animator != null && ripple_radius_animator.isRunning()){ripple_radius_animator.cancel();}ripple_radius_animator = new ObjectAnimator();ripple_radius_animator.setTarget(this);ripple_radius_animator.setPropertyName("rippleRadius");ripple_radius_animator.setInterpolator(new LinearInterpolator());ripple_radius_animator.setDuration(rippleSpeed);ripple_radius_animator.setFloatValues(0,maxRippleRadius);ripple_radius_animator.addListener(new AnimatorListenerAdapter() {@Overridepublic void onAnimationEnd(Animator animation) {if(ripple_anim_state == STATE_EXIT_LATER){ripple_anim_state = STATE_EXIT;exitRippleAnimation();}}});ripple_radius_animator.start();}private void exitRippleAnimation() {ripple_alpha_animator = new ObjectAnimator();ripple_alpha_animator.setTarget(this);ripple_alpha_animator.setPropertyName("rippleAlpha");ripple_alpha_animator.setInterpolator(new LinearInterpolator());ripple_alpha_animator.setDuration(300);ripple_alpha_animator.setIntValues(maxRippleAlpha,0);ripple_alpha_animator.start();}@Overridepublic boolean isStateful() {return true;}@Overridepublic void setHotspot(float x, float y) {currentPoint.set(x,y);}public float getOffsetLeft() {return offsetLeft;}public void setOffsetLeft(float offsetLeft) {this.offsetLeft = offsetLeft;}public float getOffsetTop() {return offsetTop;}public void setOffsetTop(float offsetTop) {this.offsetTop = offsetTop;}public float getOffsetRight() {return offsetRight;}public void setOffsetRight(float offsetRight) {this.offsetRight = offsetRight;}public float getOffsetBottom() {return offsetBottom;}public void setOffsetBottom(float offsetBottom) {this.offsetBottom = offsetBottom;}public float getRectRadius() {return rectRadius;}public void setRectRadius(float rectRadius) {this.rectRadius = rectRadius;}public void setCorners(int corners) {this.corners = corners;}public FlexibleRectDrawable setColor(int color) {paint_stroke.setColor(color);return this;}public int getSolidColor() {return solidColor;}public void setSolidColor(int solidColor) {this.solidColor = solidColor;}public int getStrokeColor() {return strokeColor;}public void setStrokeColor(int strokeColor) {this.strokeColor = strokeColor;}public boolean isHasStroke() {return hasStroke;}public void setHasStroke(boolean hasStroke) {this.hasStroke = hasStroke;}public boolean isNeedShadow() {return needShadow;}public void setNeedShadow(boolean needShadow) {this.needShadow = needShadow;}public float getShadowRange() {return shadowRange;}public void setShadowRange(float shadowRange) {this.shadowRange = shadowRange;}public float getShadowDx() {return shadowDx;}public void setShadowDx(float shadowDx) {this.shadowDx = shadowDx;}public float getShadowDy() {return shadowDy;}public void setShadowDy(float shadowDy) {this.shadowDy = shadowDy;}public int getShadowColor() {return shadowColor;}public void setShadowColor(int shadowColor) {this.shadowColor = shadowColor;}float getStrokeWidth() {return strokeWidth;}void setStrokeWidth(float strokeWidth) {this.strokeWidth = strokeWidth;this.strokeWidthVariable = strokeWidth;}public float getStrokeWidthVariable() {if(type==RectType.SOLID_BLOCK)return Math.min((getBounds().width() - offsetRight),(getBounds().height() - offsetBottom));return strokeWidthVariable;}public void setStrokeWidthVariable(float strokeWidthVariable) {this.strokeWidthVariable = strokeWidthVariable;int width = getBounds().width();int height = getBounds().height();System.out.println("type= " + type.name() + " stroke = " + strokeWidthVariable+" width = "+width+" height = "+height);if((width - offsetRight)<=strokeWidthVariable||(height - offsetBottom)<=strokeWidthVariable){//边框宽大到可以认为是纯色块if(type == RectType.BORDERED_BLOCK && this.solidColor!=0){this.solidColorHolder = this.solidColor;this.solidColor = this.strokeColor;}//若内部无色,则用边框颜色作为填充if(type == RectType.BORDER_ONLY && this.strokeColor!=0) {this.solidColor = this.strokeColor;}type = RectType.SOLID_BLOCK;}else{if(type==RectType.SOLID_BLOCK){this.strokeColor = this.solidColor;if(this.solidColorHolder == 0){type = RectType.BORDER_ONLY;//纯色块转变为仅带边框的块}else {this.solidColor = this.solidColorHolder;type = RectType.BORDERED_BLOCK;}}innerRect.set(offsetLeft + strokeWidthVariable, offsetTop + strokeWidthVariable,width - offsetRight - strokeWidthVariable,height - offsetBottom - strokeWidthVariable);}setupPainters();//重设画笔,并重绘}public void setStrokeInPercent(@FloatRange(from= 0.0f,to= 1.0f) float strokeInPercent) {if(strokeColor==0)throw new IllegalArgumentException("setStrokeInPercent函数仅适用于带边框的Drawable");this.strokeInPercent = strokeInPercent;float delta_width = outerRect.width()*strokeInPercent/2;float delta_height = outerRect.height()*strokeInPercent/2;//System.out.println("delta_height= " + delta_height + " delta_width= " + delta_width);innerRect.set(outerRect.left+delta_width, outerRect.top+delta_height,outerRect.right-delta_width,outerRect.bottom-delta_height);if(this.strokeInPercent>0 && this.strokeInPercent<1){//带边框的块if(this.solidColor==this.strokeColor && this.solidColorHolder == 0)this.type = RectType.BORDER_ONLY;//转变为仅带边框的块else{if(this.solidColorHolder!=0){this.solidColor = this.solidColorHolder;this.solidColorHolder = 0;}this.type = RectType.BORDERED_BLOCK;}}else if(this.strokeInPercent == 0){//内部填充的纯色块this.type = RectType.SOLID_BLOCK;if(solidColor == 0)Log.e("FlexibleRectDrawable","Drawable被绘制为透明色");}else if(this.strokeInPercent == 1){//边框填充的纯色块this.type = RectType.SOLID_BLOCK;if(this.solidColorHolder==0 && this.solidColor!=this.strokeColor){this.solidColorHolder = this.solidColor;this.solidColor = this.strokeColor;}}setupPainters();}public float getStrokeInPercent() {float inner_width = innerRect.width();float outer_width = outerRect.width();return (1-inner_width/outer_width);}public RectType getType() {return type;}public void setType(RectType type) {this.type = type;}public float getRippleRadius() {return rippleRadius;}public void setRippleRadius(float rippleRadius) {this.rippleRadius = rippleRadius;invalidateSelf();}public int getRippleColor() {return rippleColor;}public void setRippleColor(int rippleColor) {this.rippleColor = rippleColor;}public int getMaxRippleAlpha() {return maxRippleAlpha;}public void setMaxRippleAlpha(int maxRippleAlpha) {this.maxRippleAlpha = maxRippleAlpha;}public int getRippleAlpha() {return rippleAlpha;}public void setRippleAlpha(int rippleAlpha) {this.rippleAlpha = rippleAlpha;invalidateSelf();}public boolean isNeedRipple() {return needRipple;}public void setNeedRipple(boolean needRipple) {this.needRipple = needRipple;}public float getMaxRippleRadius() {return maxRippleRadius;}public void setMaxRippleRadius(float maxRippleRadius) {this.maxRippleRadius = maxRippleRadius;}public int getRippleSpeed() {return rippleSpeed;}public void setRippleSpeed(int rippleSpeed) {this.rippleSpeed = rippleSpeed;}@Overridepublic void setAlpha(int i) {}@Overridepublic void setColorFilter(ColorFilter colorFilter) {}@Overridepublic int getOpacity() {return PixelFormat.TRANSLUCENT;}}

此外,设置一个Builder让自定义构建变得更容易:

public static class Builder{private FlexibleRectDrawable drawable;public Builder() {this.drawable = new FlexibleRectDrawable();}public static Builder create(){return new Builder();}public Builder setSolidFill(@ColorInt int color){this.drawable.setSolidColor(color);switch(this.drawable.getType()){case BORDER_ONLY:this.drawable.setType(RectType.BORDERED_BLOCK);break;case SOLID_BLOCK:case BORDERED_BLOCK:Log.i("DrawableBuilder","cover solid color");break;case NOT_DEFINED:this.drawable.setType(RectType.SOLID_BLOCK);break;default:}return this;}public Builder setStroke(float width,@ColorInt int color){this.drawable.setHasStroke(true);this.drawable.setStrokeColor(color);this.drawable.setStrokeWidth(width);switch(this.drawable.getType()){case BORDER_ONLY:case BORDERED_BLOCK:Log.i("DrawableBuilder","cover solid color");break;case SOLID_BLOCK:this.drawable.setType(RectType.BORDERED_BLOCK);break;case NOT_DEFINED:this.drawable.setType(RectType.BORDER_ONLY);break;default:}return this;}public Builder setShadow(float shadowRange,@ColorInt int color){this.drawable.setNeedShadow(true);this.drawable.setShadowRange(shadowRange);this.drawable.setShadowColor(color);return this;}public Builder setShadowOffset(float top, float bottom, float left, float right){if(!this.drawable.isNeedShadow())throw new IllegalArgumentException("必须先调用setShadow,再设置阴影位置");this.drawable.setOffsetTop(top);this.drawable.setOffsetBottom(bottom);this.drawable.setOffsetLeft(left);this.drawable.setOffsetRight(right);return this;}public Builder setShadowOffsetCenter(float offset){if(!this.drawable.isNeedShadow())throw new IllegalArgumentException("必须先调用setShadow,再设置阴影位置");this.drawable.setOffsetTop(offset);this.drawable.setOffsetBottom(offset);this.drawable.setOffsetLeft(offset);this.drawable.setOffsetRight(offset);return this;}public Builder setCorners(int radius,int corner_type){this.drawable.setRectRadius(radius);this.drawable.setCorners(corner_type);return this;}public Builder setRipple(int color, int speed_millisecond){this.drawable.setNeedRipple(true);int check = color >>24;if (check==-1)throw new IllegalArgumentException("ripple颜色必须具有透明色");this.drawable.setRippleColor(color);this.drawable.setMaxRippleAlpha(color>>24 & 0xFF);this.drawable.setRippleSpeed(speed_millisecond);return this;}public FlexibleRectDrawable build(){this.drawable.setupPainters();return this.drawable;}}

辅助函数:dp转px

public int dp2Px(float dpValue) {final float scale = getResources().getDisplayMetrics().density;return (int) (dpValue * scale + 0.5f);}

使用方法

1. 一个普通的圆角按钮

按钮1

FlexibleRectDrawable drawable1 = FlexibleRectDrawable.Builder.create().setStroke(dp2Px(3), Color.parseColor("#4682B4")).setSolidFill(Color.parseColor("#DAA520")).setCorners(dp2Px(50), FlexibleRectDrawable.CORNER_ALL).build();
LinearLayout view1 = findViewById(R.id.frd_view1);
view1.setBackground(drawable1);

2. 一个带水波纹效果的按钮

实心/空心:
按钮2按钮3

//实心按钮
FlexibleRectDrawable drawabled1 = FlexibleRectDrawable.Builder.create().setSolidFill(Color.parseColor("#4682B4")).setCorners(dp2Px(50), FlexibleRectDrawable.CORNER_ALL).setRipple(Color.parseColor("#22FFFFFF"),300).build();
//空心按钮
FlexibleRectDrawable drawable2 = FlexibleRectDrawable.Builder.create().setStroke(dp2Px(3), Color.parseColor("#4682B4")).setCorners(dp2Px(50), FlexibleRectDrawable.CORNER_ALL).setRipple(Color.parseColor("#22000000"),300).build();

3. 一个带阴影的按钮

实心/空心:
按钮4按钮5

//空心
FlexibleRectDrawable drawabled2 = FlexibleRectDrawable.Builder.create().setStroke(dp2Px(3),Color.parseColor("#1E90FF")).setCorners(dp2Px(50), FlexibleRectDrawable.CORNER_ALL).setShadow(dp2Px(7), Color.parseColor("#fe00FFFF")).setShadowOffsetCenter(dp2Px(5)).build();
//实心
FlexibleRectDrawable drawable2 = FlexibleRectDrawable.Builder.create().setStroke(dp2Px(3), Color.parseColor("#4682B4")).setCorners(dp2Px(50), FlexibleRectDrawable.CORNER_ALL).setSolidFill(Color.parseColor("#FFFFFF")).setShadow(dp2Px(5), Color.parseColor("#FEA9A9A9")).setShadowOffsetCenter(dp2Px(5)).build();

4. 两个左右合并的按钮

按钮6

布局文件(.xml):

<LinearLayoutandroid:id="@+id/frd_view_d3"android:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"android:layout_margin="10dp"android:layout_below="@id/frd_view_d2"><Buttonandroid:id="@+id/frd_btn1"android:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="1"android:text="btn1"/><Buttonandroid:id="@+id/frd_btn2"android:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="1"android:text="btn2"android:textColor="@color/white"/></LinearLayout>

对应java代码:

FlexibleRectDrawable drawable_btn_left = FlexibleRectDrawable.Builder.create().setStroke(dp2Px(3),Color.parseColor("#1E90FF")).setSolidFill(Color.parseColor("#1E90FF")).setCorners(dp2Px(50), FlexibleRectDrawable.CORNER_HALF_LEFT).setRipple(Color.parseColor("#33000000"),300).build();
FlexibleRectDrawable drawable_btn_right = FlexibleRectDrawable.Builder.create().setStroke(dp2Px(3),Color.parseColor("#1E90FF")).setCorners(dp2Px(50), FlexibleRectDrawable.CORNER_HALF_RIGHT).setRipple(Color.parseColor("#33FFFFFF"),300).build();
Button btn_left = findViewById(R.id.frd_btn1);
btn_left.setBackground(drawable_btn_left);
Button btn_right = findViewById(R.id.frd_btn2);
btn_right.setBackground(drawable_btn_right);

5. 按固定宽度设置矩形边框

改变边框的宽度和颜色

按钮7

private boolean on_view3 = false;
...FlexibleRectDrawable drawable3 = FlexibleRectDrawable.Builder.create().setStroke(dp2Px(3), Color.parseColor("#4682B4")).setSolidFill(Color.parseColor("#131313")).setCorners(dp2Px(50), FlexibleRectDrawable.CORNER_ALL).build();LinearLayout view3 = findViewById(R.id.frd_view3);
view3.setBackground(drawable3);view3.setOnClickListener(v -> {on_view3 = !on_view3;if(on_view3){drawable3.setStrokeColor(Color.RED);drawable3.setStrokeWidthVariable(dp2Px(5));}else{drawable3.setStrokeColor(Color.parseColor("#4682B4"));drawable3.setStrokeWidthVariable(dp2Px(3));}});

通过属性动画实现按钮状态切换

按钮8

private boolean on_view5 = false;
...FlexibleRectDrawable drawabled5 = FlexibleRectDrawable.Builder.create().setStroke(dp2Px(3),Color.parseColor("#4682B4")).setSolidFill(Color.parseColor("#3CB371")).setCorners(dp2Px(5), FlexibleRectDrawable.CORNER_ALL).build();
LinearLayout viewd5 = findViewById(R.id.frd_view_d5);
viewd5.setBackground(drawabled5);
viewd5.setOnClickListener(v -> {on_view5 = !on_view5;if(on_view5){ObjectAnimator animator = new ObjectAnimator();animator.setTarget(drawabled5);animator.setPropertyName("strokeWidthVariable");animator.setDuration(1000);animator.setFloatValues(drawabled5.getStrokeWidthVariable(),dp2Px(300));animator.start();}else {ObjectAnimator animator = new ObjectAnimator();animator.setTarget(drawabled5);animator.setPropertyName("strokeWidthVariable");animator.setDuration(1000);animator.setFloatValues(drawabled5.getStrokeWidthVariable(),dp2Px(3));animator.start();}});

6. 按所占百分比设置矩形边框

按钮9

private boolean on_view6 = false;
private float origin_percent = 0.0f;
...FlexibleRectDrawable drawabled6 = FlexibleRectDrawable.Builder.create().setStroke(dp2Px(3),Color.parseColor("#4682B4")).setCorners(dp2Px(5), FlexibleRectDrawable.CORNER_ALL).build();
LinearLayout viewd6 = findViewById(R.id.frd_view_d6);
viewd6.setBackground(drawabled6);
viewd6.setOnClickListener(v -> {on_view6 = !on_view6;if (on_view6) {origin_percent = drawabled6.getStrokeInPercent();ObjectAnimator animator = new ObjectAnimator();animator.setTarget(drawabled6);animator.setPropertyName("strokeInPercent");animator.setDuration(500);animator.setFloatValues(origin_percent, 1f);animator.start();}else {ObjectAnimator animator = new ObjectAnimator();animator.setTarget(drawabled6);animator.setPropertyName("strokeInPercent");animator.setDuration(500);animator.setFloatValues(drawabled6.getStrokeInPercent(), origin_percent);animator.start();}});

相关文章:

Android自定义Drawable---灵活多变的矩形背景

Android自定义Drawable—灵活多变的矩形背景 在安卓开发中&#xff0c;我们通常需要为不同的按钮设置不同的背景以实现不同的效果&#xff0c;有时还需要这些按钮根据实际情况进行变化。如果采用编写resource中xml文件的形式&#xff0c;就需要重复定义许多只有微小变动的资源…...

ParagonNTFSforMac_15.5.102中文版最受欢迎的NTFS硬盘格式读取工具

Paragon NTFS for Mac是一款可以为您轻松解决Mac平台上不能识别Windows通用的NTFS文件难题&#xff0c;这是一款强大的Mac读写工具&#xff0c;相信在很多时候&#xff0c;Mac用户需要对NTFS文件的移动硬盘进行写入&#xff0c;但是macOS系统默认是不让写入的&#xff0c;使用小…...

Kafka 搭建过程

目录 1.关于Kafka2.Kafka 搭建过程3.参考 本文主要介绍Kafka基本原理&#xff0c;以及搭建过程。 1.关于Kafka Apache Kafka是一个开源的分布式事件流平台&#xff0c;被设计用来实现实时数据流的发布、订阅、存储和处理。 Kafka的主要特性包括&#xff1a; 高吞吐量&#x…...

七、2023.10.1.Linux(一).7

文章目录 1、 Linux中查看进程运行状态的指令、查看内存使用情况的指令、tar解压文件的参数。2、文件权限怎么修改&#xff1f;3、说说常用的Linux命令&#xff1f;4、说说如何以root权限运行某个程序&#xff1f;5、 说说软链接和硬链接的区别&#xff1f;6、说说静态库和动态…...

一文教你搞懂Redis集群

一、Redis主从 1.1、搭建主从架构 单节点的Redis的并发能力是有上限的&#xff0c;要进一步的提高Redis的并发能力&#xff0c;据需要大家主从集群&#xff0c;实现读写分离。 共包含三个实例&#xff0c;由于资源有限&#xff0c;所以在一台虚拟机上&#xff0c;开启多个red…...

树上启发式合并 待补

对于每个子树&#xff0c;直接遍历所有轻儿子&#xff0c;继承重儿子 会了板子后&#xff0c;修改维护的东西和莫队是一样的 洛谷 U41492 #include <bits/stdc.h> #define ll long long #define ull unsigned long long constexpr int N1e55; std::vector<int> e…...

minio分布式文件存储

基本介绍 什么是 MinIO MinIO 是一款基于 Go 语言的高性能、可扩展、云原生支持、操作简单、开源的分布式对象存储产品。基于 Apache License v2.0 开源协议&#xff0c;虽然轻量&#xff0c;却拥有着不错的性能。它兼容亚马逊S3云存储服务接口。可以很简单的和其他应…...

Linux新的IO模型io_uring

一、Linux下的网络通信模型 在网络开发的过程中&#xff0c;需要处理好几个问题。首先是通信的内核支持问题&#xff1b;其次是通信的模型问题&#xff1b;最后是框架问题。这些问题在闭源的OS如Windows上&#xff0c;基本上不算什么大问题&#xff08;因为只能用人家的API&am…...

FFmpeg 命令:从入门到精通 | FFmpeg 基本介绍

FFmpeg 命令&#xff1a;从入门到精通 | FFmpeg 基本介绍 FFmpeg 命令&#xff1a;从入门到精通 | FFmpeg 基本介绍FFmpeg 简介FFmpeg 基础知识复用与解复用编解码器码率和帧率 资料 FFmpeg 命令&#xff1a;从入门到精通 | FFmpeg 基本介绍 本系列文章要解决的问题&#xff1…...

数组篇 第一题:删除排序数组中的重复项

更多精彩内容请关注微信公众号&#xff1a;听潮庭。 第一题&#xff1a;删除排序数组中的重复项 给你一个 非严格递增排列 的数组 nums &#xff0c;请你 原地 删除重复出现的元素&#xff0c;使每个元素 只出现一次 &#xff0c;返回删除后数组的新长度。元素的 相对顺序 应…...

堆的初步认识

在学习本节文章前要先了解&#xff1a;大顶堆与小顶堆&#xff1a; &#xff08;优先级队列_加瓦不加班的博客-CSDN博客&#xff09; 堆实现 计算机科学中&#xff0c;堆是一种基于树的数据结构&#xff0c;通常用完全二叉树实现。 什么叫完全二叉树&#xff1f; 答&#x…...

CycleGAN模型之Pytorch实战

一、CycleGAN基本介绍 1. CycleGAN论文:《Unpaired Image-to-Image Translation using Cycle-Consistent Adversarial Networks》 2. 原文代码:https://github.com/junyanz/pytorch-CycleGAN-and-pix2pix 3. 网传精简代码:https://github.com/aitorzip/PyTorch-CycleGAN …...

C++(STL容器适配器)

前言&#xff1a; 适配器也称配接器&#xff08;adapters&#xff09;在STL组件的灵活组合运用功能上&#xff0c;扮演着轴承、转换器的角色。 《Design Patterns》对adapter的定义如下&#xff1a;将一个class的接口转换为另一个class的接口&#xff0c;使原本因接口不兼容而…...

软考 系统架构设计师系列知识点之软件架构风格(7)

接前一篇文章&#xff1a;软考 系统架构设计师系列知识点之软件架构风格&#xff08;6&#xff09; 这个十一注定是一个不能放松、保持“紧”的十一。由于报名了全国计算机技术与软件专业技术资格&#xff08;水平&#xff09;考试&#xff0c;11月4号就要考试&#xff0c;因此…...

【Vue3】自定义指令

除了 Vue 内置的一系列指令 (比如 v-model 或 v-show) 之外&#xff0c;Vue 还允许你注册自定义的指令 (Custom Directives)。 1. 生命周期钩子函数 一个自定义指令由一个包含类似组件生命周期钩子的对象来定义。钩子函数会接收到指令所绑定元素作为其参数。 在 <script …...

UG\NX CAM二次开发 加工模块获取 UF _ask_application_module

文章作者:代工 来源网站:NX CAM二次开发专栏 简介: UG\NX CAM二次开发 加工模块获取 UF _ask_application_module 代码: void MyClass::do_it() { // TODO: add your code here // 获取NX当前所在的模块 int module_id = 0; // UF_ask_application_module(&…...

借助GPU算力编译Android

借助GPU算力编译Android 借助GPU编译Android代码的意义在于提高编译的效率和速度。传统的CPU编译方式在处理大量代码时可能会遇到性能瓶颈,而GPU编译利用了显卡的并行计算能力,可以同时处理多个任务,加快编译过程。通过利用GPU的并行计算能力,可以将编译过程中的多个任务分…...

docker-compose一键部署mysql

1.创建安装目录 mnt为硬盘挂载目录&#xff0c;根据实际情况修改 mkdir -p /mnt/mysql cd /mnt/mysql vim docker-compose.yml2.编写docker-compose.yml version: 3.1 services:db:image: mysql:5.7 #mysql版本volumes:- ./data/db:/var/lib/mysql #数据文件- ./etc/my.cnf:/…...

MATLAB 函数签名器

文章目录 MATLAB 函数签名器注释规范模板参数类型 kind数据格式 type选项的支持 使用可执行程序封装为m函数程序输出 编译待办事项推荐阅读附录 MATLAB 函数签名器 MATLAB 函数签名器 (FUNCSIGN) &#xff0c;在规范注释格式的基础上为函数文件或类文件自动生成函数签名&#…...

2019强网杯随便注bugktu sql注入

一.2019强网杯随便注入 过滤了一些函数&#xff0c;联合查询&#xff0c;报错&#xff0c;布尔&#xff0c;时间等都不能用了&#xff0c;尝试堆叠注入 1.通过判断是单引号闭合 ?inject1-- 2.尝试堆叠查询数据库 ?inject1;show databases;-- 3.查询数据表 ?inject1;show …...

Debian系统简介

目录 Debian系统介绍 Debian版本介绍 Debian软件源介绍 软件包管理工具dpkg dpkg核心指令详解 安装软件包 卸载软件包 查询软件包状态 验证软件包完整性 手动处理依赖关系 dpkg vs apt Debian系统介绍 Debian 和 Ubuntu 都是基于 Debian内核 的 Linux 发行版&#xff…...

Python爬虫实战:研究feedparser库相关技术

1. 引言 1.1 研究背景与意义 在当今信息爆炸的时代,互联网上存在着海量的信息资源。RSS(Really Simple Syndication)作为一种标准化的信息聚合技术,被广泛用于网站内容的发布和订阅。通过 RSS,用户可以方便地获取网站更新的内容,而无需频繁访问各个网站。 然而,互联网…...

连锁超市冷库节能解决方案:如何实现超市降本增效

在连锁超市冷库运营中&#xff0c;高能耗、设备损耗快、人工管理低效等问题长期困扰企业。御控冷库节能解决方案通过智能控制化霜、按需化霜、实时监控、故障诊断、自动预警、远程控制开关六大核心技术&#xff0c;实现年省电费15%-60%&#xff0c;且不改动原有装备、安装快捷、…...

生成 Git SSH 证书

&#x1f511; 1. ​​生成 SSH 密钥对​​ 在终端&#xff08;Windows 使用 Git Bash&#xff0c;Mac/Linux 使用 Terminal&#xff09;执行命令&#xff1a; ssh-keygen -t rsa -b 4096 -C "your_emailexample.com" ​​参数说明​​&#xff1a; -t rsa&#x…...

高危文件识别的常用算法:原理、应用与企业场景

高危文件识别的常用算法&#xff1a;原理、应用与企业场景 高危文件识别旨在检测可能导致安全威胁的文件&#xff0c;如包含恶意代码、敏感数据或欺诈内容的文档&#xff0c;在企业协同办公环境中&#xff08;如Teams、Google Workspace&#xff09;尤为重要。结合大模型技术&…...

ServerTrust 并非唯一

NSURLAuthenticationMethodServerTrust 只是 authenticationMethod 的冰山一角 要理解 NSURLAuthenticationMethodServerTrust, 首先要明白它只是 authenticationMethod 的选项之一, 并非唯一 1 先厘清概念 点说明authenticationMethodURLAuthenticationChallenge.protectionS…...

【C语言练习】080. 使用C语言实现简单的数据库操作

080. 使用C语言实现简单的数据库操作 080. 使用C语言实现简单的数据库操作使用原生APIODBC接口第三方库ORM框架文件模拟1. 安装SQLite2. 示例代码:使用SQLite创建数据库、表和插入数据3. 编译和运行4. 示例运行输出:5. 注意事项6. 总结080. 使用C语言实现简单的数据库操作 在…...

JVM暂停(Stop-The-World,STW)的原因分类及对应排查方案

JVM暂停(Stop-The-World,STW)的完整原因分类及对应排查方案,结合JVM运行机制和常见故障场景整理而成: 一、GC相关暂停​​ 1. ​​安全点(Safepoint)阻塞​​ ​​现象​​:JVM暂停但无GC日志,日志显示No GCs detected。​​原因​​:JVM等待所有线程进入安全点(如…...

技术栈RabbitMq的介绍和使用

目录 1. 什么是消息队列&#xff1f;2. 消息队列的优点3. RabbitMQ 消息队列概述4. RabbitMQ 安装5. Exchange 四种类型5.1 direct 精准匹配5.2 fanout 广播5.3 topic 正则匹配 6. RabbitMQ 队列模式6.1 简单队列模式6.2 工作队列模式6.3 发布/订阅模式6.4 路由模式6.5 主题模式…...

Java + Spring Boot + Mybatis 实现批量插入

在 Java 中使用 Spring Boot 和 MyBatis 实现批量插入可以通过以下步骤完成。这里提供两种常用方法&#xff1a;使用 MyBatis 的 <foreach> 标签和批处理模式&#xff08;ExecutorType.BATCH&#xff09;。 方法一&#xff1a;使用 XML 的 <foreach> 标签&#xff…...