<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
本文範例為大家分享了Android自定義View實現進度條動畫的具體程式碼,供大家參考,具體內容如下
控制元件效果
原理:
控制程式碼/
/更新進度值 val mHandler = object : Handler() { override fun handleMessage(msg: Message?) { progressView.setCurrentProgress(progress1.toFloat()) } } //開啟動畫,更新進度值 private fun progressAdd() { isAnimate = true progress1 = 0 Thread(Runnable { while (isAnimate) { Thread.sleep(random.nextInt(200).toLong()) progress1 += 1 mHandler.sendEmptyMessage(0) } }).start() } //停止動畫 private fun progressReduce() { isAnimate = false }
控制元件原始碼:
public class ProgressView extends View { //前景顏色 private int FIRST_COLOR = 0xffFEA711; // 0xff00574B;// //背景顏色 private int SECOND_COLOR = 0xffFBE39C; private int mSecondWidth; private int mFirstWidth; private int mHeight; private int mSecondRadius; private int mFirstRadius; private int progressPadding = 30; private float currentValue; private int maxProgress = 100; //畫筆 private Paint paint; private RectF rectF; private Bitmap leafBitmap; private int mLeafWidth; private int mLeafHeight; private List<Leaf> mLeafInfos; public ProgressView(Context context) { super(context, null); } public ProgressView(Context context, @Nullable AttributeSet attrs) { this(context, attrs, 0); } public ProgressView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); paint = new Paint(); paint.setStyle(Paint.Style.FILL); paint.setColor(FIRST_COLOR); paint.setAntiAlias(true); rectF = new RectF(); leafBitmap = BitmapFactory.decodeResource(context.getResources(), R.mipmap.icon_leaf); mLeafWidth = leafBitmap.getWidth(); mLeafHeight = leafBitmap.getHeight(); Log.e("zhen", " mLeafWidth: " + mLeafWidth + " mLeafHeight: " + mLeafHeight); } @Override protected void onSizeChanged(int w, int h, int oldw, int oldh) { super.onSizeChanged(w, h, oldw, oldh); mHeight = h; //外圍 mSecondWidth = w; mSecondRadius = (int) ((mSecondWidth * 1f / 5) / 2); //內部進度條 mFirstWidth = mSecondWidth - progressPadding - mSecondRadius; mFirstRadius = mSecondRadius - progressPadding; mLeafInfos = new LeafFactory(mFirstRadius - mLeafWidth, 6).generateLeafs(); Log.e("zhen", " mSecondWidth: " + mSecondWidth + " mHeight: " + mHeight + " mSecondRadius: " + mSecondRadius + " mFirstWidth: " + mFirstWidth + " mFirstRadius: " + mFirstRadius); } public void setCurrentProgress(float currentProgress) { Log.e("zhen", "currentProgress: " + currentProgress); if (currentProgress > 100) return; currentValue = currentProgress / maxProgress * mFirstWidth; invalidate(); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); //畫布移動 canvas.translate(mSecondRadius, mHeight / 2); //畫背景左半圓 paint.setStyle(Paint.Style.FILL); paint.setColor(SECOND_COLOR); rectF.left = -mSecondRadius; rectF.top = -mSecondRadius; rectF.right = mSecondRadius; rectF.bottom = mSecondRadius; canvas.drawArc(rectF, 90, 180, false, paint); //畫背景右半圓 rectF.left = mSecondWidth - 3 * mSecondRadius; rectF.top = -mSecondRadius; rectF.right = mSecondWidth - mSecondRadius; rectF.bottom = mSecondRadius; canvas.drawArc(rectF, -90, 180, false, paint); //畫背景中間方形 rectF.left = 0; rectF.top = -mSecondRadius; rectF.right = mSecondWidth - 2 * mSecondRadius; rectF.bottom = mSecondRadius; canvas.drawRect(rectF, paint); //繪製進度條半圓 paint.setColor(FIRST_COLOR); float leftValue = (mFirstRadius - currentValue) > 0 ? (mFirstRadius - currentValue) : 0; float angel = (float) Math.toDegrees(Math.acos(leftValue / mFirstRadius)); rectF.left = -mFirstRadius; rectF.top = -mFirstRadius; rectF.right = mFirstRadius; rectF.bottom = mFirstRadius; canvas.drawArc(rectF, 180 - angel, 2 * angel, false, paint); //繪製進度條方形 if (currentValue >= mFirstRadius) { currentValue = currentValue > mFirstWidth ? mFirstWidth : currentValue; rectF.left = 0; rectF.top = -mFirstRadius; rectF.right = currentValue - mFirstRadius; rectF.bottom = mFirstRadius; canvas.drawRect(rectF, paint); } //落葉紛飛 if (currentValue > 0 && currentValue < mFirstWidth) { long currentTime = System.currentTimeMillis(); for (int i = 0; i < mLeafInfos.size(); i++) { Leaf leaf = mLeafInfos.get(i); int delay = (int) ((currentTime - leaf.startTime) % CIRCLE_TIME); leaf.x = mFirstWidth - mFirstRadius - delay * (mFirstWidth * 1f / CIRCLE_TIME); if (leaf.x + mFirstRadius + mLeafWidth < currentValue) continue; leaf.y = (float) (leaf.amplitude * Math.sin(2 * Math.PI / CIRCLE_TIME * delay + leaf.phaseOffeset)); Log.e("zhen", "延時ms數" + delay + " 角速度: " + (2 * Math.PI / CIRCLE_TIME) + " leaf.x: " + leaf.x + " leaf.y: " + leaf.y); // 通過Matrix控制葉子旋轉 Matrix matrix = new Matrix(); matrix.postTranslate(leaf.x, leaf.y); // 根據葉子旋轉方向確定葉子旋轉角度 float rotate = leaf.rotateOrientation ? leaf.x % 360f : -leaf.x % 360; matrix.postRotate(rotate, leaf.x + mLeafWidth / 2, leaf.y + mLeafHeight / 2); Log.e("zhen", "落葉子旋轉角度: " + rotate); canvas.drawBitmap(leafBitmap, matrix, paint); } } //繪製旋轉風車 paint.setColor(Color.WHITE); paint.setStyle(Paint.Style.STROKE); paint.setStrokeWidth(10); float x = mSecondWidth - 2 * mSecondRadius; float y = 0; canvas.drawCircle(x, y, mSecondRadius, paint); paint.setStyle(Paint.Style.FILL); paint.setColor(0XFFFCD158); canvas.drawCircle(x, y, mSecondRadius - 5, paint); } @IntDef({Amplitude.SMALL, Amplitude.MIDDLE, Amplitude.LARGE}) public @interface Amplitude { int SMALL = 0; int MIDDLE = 1; int LARGE = 2; } public static class Leaf { public static long CIRCLE_TIME = 3000; //一個週期所需要的時間 //振幅大小 private int amplitude; //初始相位偏移 private int phaseOffeset; //旋轉方向 private boolean rotateOrientation; //延時時間 private long startTime; //x位置取決於初始位置偏移 private float x; //y位置取決於振幅,初始相位 private float y; @Override public String toString() { return "Leaf{" + "amplitude=" + amplitude + ", phaseOffeset=" + phaseOffeset + ", rotateOrientation=" + rotateOrientation + ", startTime=" + startTime + ", x=" + x + ", y=" + y + '}'; } } public static class LeafFactory { private Random random = new Random(); int randomValue; private int mHeight; private int mLeafSize; public LeafFactory(int mHeight, int mLeafSize) { this.mHeight = mHeight; this.mLeafSize = mLeafSize; } public List<Leaf> generateLeafs() { List<Leaf> mLeafsInfo = new ArrayList<>(); for (int i = 0; i < mLeafSize; i++) { mLeafsInfo.add(generateLeaf()); } return mLeafsInfo; } private Leaf generateLeaf() { Leaf leaf = new Leaf(); randomValue = random.nextInt(3); switch (randomValue) { case Amplitude.SMALL: leaf.amplitude = (int) (mHeight * 1f / 3); break; case Amplitude.MIDDLE: leaf.amplitude = (int) (mHeight * 2f / 3); break; case Amplitude.LARGE: leaf.amplitude = (int) (mHeight * 3f / 3); break; } leaf.startTime = random.nextInt((int) CIRCLE_TIME); leaf.phaseOffeset = random.nextInt(8); leaf.rotateOrientation = random.nextBoolean(); Log.e("zhen", "生成一個葉子:" + leaf); return leaf; } } }
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援it145.com。
相關文章
<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
综合看Anker超能充系列的性价比很高,并且与不仅和iPhone12/苹果<em>Mac</em>Book很配,而且适合多设备充电需求的日常使用或差旅场景,不管是安卓还是Switch同样也能用得上它,希望这次分享能给准备购入充电器的小伙伴们有所
2021-06-01 09:31:42
除了L4WUDU与吴亦凡已经多次共事,成为了明面上的厂牌成员,吴亦凡还曾带领20XXCLUB全队参加2020年的一场音乐节,这也是20XXCLUB首次全员合照,王嗣尧Turbo、陈彦希Regi、<em>Mac</em> Ova Seas、林渝植等人全部出场。然而让
2021-06-01 09:31:34
目前应用IPFS的机构:1 谷歌<em>浏览器</em>支持IPFS分布式协议 2 万维网 (历史档案博物馆)数据库 3 火狐<em>浏览器</em>支持 IPFS分布式协议 4 EOS 等数字货币数据存储 5 美国国会图书馆,历史资料永久保存在 IPFS 6 加
2021-06-01 09:31:24
开拓者的车机是兼容苹果和<em>安卓</em>,虽然我不怎么用,但确实兼顾了我家人的很多需求:副驾的门板还配有解锁开关,有的时候老婆开车,下车的时候偶尔会忘记解锁,我在副驾驶可以自己开门:第二排设计很好,不仅配置了一个很大的
2021-06-01 09:30:48
不仅是<em>安卓</em>手机,苹果手机的降价力度也是前所未有了,iPhone12也“跳水价”了,发布价是6799元,如今已经跌至5308元,降价幅度超过1400元,最新定价确认了。iPhone12是苹果首款5G手机,同时也是全球首款5nm芯片的智能机,它
2021-06-01 09:30:45