您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
Android中怎么通过自定义view实现进度条加载效果,针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。
分析图:
代码如下:
package com.example.dotloadview; import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.Paint; import android.os.Handler; import android.util.AttributeSet; import android.view.View; /** * Created by zhouguizhi on 6/18/15. */ public class DotLoadProgressView extends View { private boolean isStart = false; private int dotIndex =0;//点下面的角标 private int dotNumber = 0; private Paint mPaint; private int paddingTop = 50; private int paddingBottom = 50; private int paddingLeft = 30; private int paddingRight = 30; private Handler mHandler = new Handler(); private long DELAY_TIME = 300; private Bitmap bitmap; private int itemDotSpace = 10;//二个点之间的距离 private Bitmap moveBitmap ; private Runnable mRunnable = new Runnable() { @Override public void run() { if (dotNumber != 0){ dotIndex = (dotIndex + 1) % dotNumber; } invalidate(); mHandler.postDelayed(mRunnable, DELAY_TIME); } }; public DotLoadProgressView(Context context, AttributeSet attrs) { super(context, attrs); moveBitmap = BitmapFactory.decodeResource(getResources(),R.drawable.active_dot); init(); } private void init() { mPaint = new Paint(Paint.ANTI_ALIAS_FLAG); mPaint.setStyle(Paint.Style.FILL); mPaint.setDither(true); mPaint.setAntiAlias(true); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); for(int i=0;i<dotNumber;i++){ canvas.drawBitmap(bitmap,paddingLeft+i*itemDotSpace+bitmap.getWidth()*i,paddingTop,mPaint); } if(isStart){ canvas.drawBitmap(moveBitmap,paddingLeft+dotIndex*itemDotSpace+moveBitmap.getWidth()*dotIndex,paddingTop,mPaint); } } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int parentWidth = MeasureSpec.getSize(widthMeasureSpec); bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.inactive_dot); setMeasuredDimension(parentWidth,bitmap.getHeight()+paddingTop+paddingBottom); int widthWithoutPadding = parentWidth - paddingLeft - paddingRight; dotNumber = calculateDotsNumber(widthWithoutPadding); } private int calculateDotsNumber(int width) { int number = (width / (itemDotSpace + bitmap.getWidth())); return number; } public void startProgress() { isStart = true; dotIndex = -1; mHandler.removeCallbacks(mRunnable); mHandler.post(mRunnable); } public void stopProgress() { isStart = false; mHandler.removeCallbacks(mRunnable); invalidate(); } @Override protected void onAttachedToWindow() { super.onAttachedToWindow(); startProgress(); } @Override protected void onDetachedFromWindow() { super.onDetachedFromWindow(); stopProgress(); } }
布局代码:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content"> <com.example.dotloadview.DotLoadProgressView android:id="@+id/progress1" android:layout_width="match_parent" android:layout_height="wrap_content" /> </RelativeLayout>
关于Android中怎么通过自定义view实现进度条加载效果问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注亿速云行业资讯频道了解更多相关知识。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。