android自定义虚线,如何在android中自定义虚线进度条?

本文介绍了一个自定义视图`HorizontalDottedProgress`的实现,它创建了一个带有动画效果的点状进度条。在`onDraw`方法中绘制了多个圆点,并通过`onAttachedToWindow`启动动画。动画使用`BounceAnimation`类实现,使指定位置的点产生弹跳效果,无限重复。这个组件可以用于Android应用中展示加载或进度状态。
摘要由CSDN通过智能技术生成

MainActivity.java:

public class MainActivity extends AppCompatActivity {

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

}

}

activity_main.xml:

android:orientation="vertical"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:id="@+id/rect"

android:gravity="center"

>

android:layout_width="wrap_content"

android:layout_height="wrap_content"

>

Horizo​​ntalDottedProgress.java:

这是一个自定义类,用于创建应用了动画的点.

public class HorizontalDottedProgress extends View{

//actual dot radius

private int mDotRadius = 5;

//Bounced Dot Radius

private int mBounceDotRadius = 8;

//to get identified in which position dot has to bounce

private int mDotPosition;

//specify how many dots you need in a progressbar

private int mDotAmount = 10;

public HorizontalDottedProgress(Context context) {

super(context);

}

public HorizontalDottedProgress(Context context, AttributeSet attrs) {

super(context, attrs);

}

public HorizontalDottedProgress(Context context, AttributeSet attrs, int defStyleAttr) {

super(context, attrs, defStyleAttr);

}

//Method to draw your customized dot on the canvas

@Override

protected void onDraw(Canvas canvas) {

super.onDraw(canvas);

Paint paint = new Paint();

//set the color for the dot that you want to draw

paint.setColor(Color.parseColor("#fd583f"));

//function to create dot

createDot(canvas,paint);

}

@Override

protected void onAttachedToWindow() {

super.onAttachedToWindow();

//Animation called when attaching to the window, i.e to your screen

startAnimation();

}

private void createDot(Canvas canvas, Paint paint) {

//here i have setted progress bar with 10 dots , so repeat and wnen i = mDotPosition then increase the radius of dot i.e mBounceDotRadius

for(int i = 0; i < mDotAmount; i++ ){

if(i == mDotPosition){

canvas.drawCircle(10+(i*20), mBounceDotRadius, mBounceDotRadius, paint);

}else {

canvas.drawCircle(10+(i*20), mBounceDotRadius, mDotRadius, paint);

}

}

}

@Override

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

super.onMeasure(widthMeasureSpec, heightMeasureSpec);

int width;

int height;

//calculate the view width

int calculatedWidth = (20*9);

width = calculatedWidth;

height = (mBounceDotRadius*2);

//MUST CALL THIS

setMeasuredDimension(width, height);

}

private void startAnimation() {

BounceAnimation bounceAnimation = new BounceAnimation();

bounceAnimation.setDuration(100);

bounceAnimation.setRepeatCount(Animation.INFINITE);

bounceAnimation.setInterpolator(new LinearInterpolator());

bounceAnimation.setAnimationListener(new Animation.AnimationListener() {

@Override

public void onAnimationStart(Animation animation) {

}

@Override

public void onAnimationEnd(Animation animation) {

}

@Override

public void onAnimationRepeat(Animation animation) {

mDotPosition++;

//when mDotPosition == mDotAmount , then start again applying animation from 0th positon , i.e mDotPosition = 0;

if (mDotPosition == mDotAmount) {

mDotPosition = 0;

}

Log.d("INFOMETHOD","----On Animation Repeat----");

}

});

startAnimation(bounceAnimation);

}

private class BounceAnimation extends Animation {

@Override

protected void applyTransformation(float interpolatedTime, Transformation t) {

super.applyTransformation(interpolatedTime, t);

//call invalidate to redraw your view againg.

invalidate();

}

}

}

快照:

7b0b5f369f99530318f2fcabeee8fab7.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值