本文主要是介绍简单仿写网易加载进度条,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:id="@+id/activity_main"android:layout_width="match_parent"android:layout_height="match_parent"tools:context="com.safly.myapplication.MainActivity"><com.safly.myapplication.CustomeWaitingProgressBar
android:layout_centerInParent="true"android:id="@+id/skipView2"android:layout_width="wrap_content"android:layout_height="wrap_content"></com.safly.myapplication.CustomeWaitingProgressBar></RelativeLayout>
MainActivity
package com.safly.myapplication;import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;import com.safly.myapplication.CustomeWaitingProgressBar;public class MainActivity extends AppCompatActivity {private Handler handler = new Handler() {@Overridepublic void handleMessage(Message msg) {switch (msg.what) {case 2:pbBar.setWaitingProgress();break;}}};CustomeWaitingProgressBar pbBar;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);pbBar = (CustomeWaitingProgressBar) findViewById(R.id.skipView2);new Thread(new Runnable() {@Overridepublic void run() {try {for (int i = 0; i < 100; i++) {Log.i("MainActivity","----------------");handler.sendEmptyMessage(2);Thread.sleep(100);}} catch (InterruptedException e) {e.printStackTrace();}}}).start();pbBar.setDegreeCallBack(new CustomeWaitingProgressBar.DegreeCallBack() {@Overridepublic void degreeFinish(int cur) {Toast.makeText(MainActivity.this,"cur--"+cur,Toast.LENGTH_SHORT).show();}});pbBar.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {Toast.makeText(MainActivity.this,"setOnClickListener",Toast.LENGTH_SHORT).show();}});}
}
CustomeWaitingProgressBar
package com.safly.myapplication;import android.content.Context; import android.graphics.Canvas;import android.graphics.Color;import android.graphics.Paint;import android.graphics.Rect;import android.graphics.RectF;import android.util.AttributeSet; import android.view.View; public class CustomeWaitingProgressBar extends View {private Paint mForePaint;private float mStrokeWidth;private float mRadius;private RectF mRect; private int mWidth;private int mHeight;private int currentDegree;public CustomeWaitingProgressBar(Context context) {super(context); init();} public CustomeWaitingProgressBar(Context context, AttributeSet attrs) { super(context, attrs); init();} public CustomeWaitingProgressBar(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init();} private void init(){mRadius = DisplayUtil.dip2px(getContext(),20);mStrokeWidth = DisplayUtil.dip2px(getContext(),2);if(mForePaint == null){mForePaint=new Paint();mForePaint.setColor(Color.parseColor("#FF0000"));mForePaint.setAntiAlias(true);mForePaint.setStyle(Paint.Style.STROKE);mForePaint.setStrokeWidth(mStrokeWidth);}}@Overrideprotected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); mWidth=getRealSize(widthMeasureSpec); mHeight=getRealSize(heightMeasureSpec); setMeasuredDimension(mWidth,mHeight); } public int getRealSize(int measureSpec){ int result=-1; int mode=MeasureSpec.getMode(measureSpec); int size=MeasureSpec.getSize(measureSpec); if (mode==MeasureSpec.AT_MOST||mode==MeasureSpec.UNSPECIFIED){ //(半径*2+描边*2)的长度 result=(int) (mRadius*2+2*mStrokeWidth); }else{ result=size; } return result; }/*** 初始化yuanx的外轮廓矩形*/public void initRect(){if (mRect==null){mRect=new RectF();int left=(int) mStrokeWidth/2;int top=(int) mStrokeWidth/2;int right=mWidth-left;int bottom=mHeight-top;mRect.set(left,top,right,bottom);}}private int leftLastX = -90;private int sweepGreed = 0;private int perAcceleration = 10;private int perDrawAcceleration = 5;private int drawTime;private int leftLastSweepGreed;@Overrideprotected void onDraw(Canvas canvas) {initRect();if (sweepGreed - leftLastSweepGreed >= 120){leftLastX = -90;sweepGreed = -90;drawTime = 0;}canvas.drawArc(mRect,leftLastX,sweepGreed,false,mForePaint);leftLastSweepGreed = perAcceleration*drawTime;leftLastX+=perAcceleration;sweepGreed+=perAcceleration+perDrawAcceleration*drawTime;drawTime++;if (currentDegree == 360){if (callBack!=null){callBack.degreeFinish(currentDegree);}}}DegreeCallBack callBack;public void setDegreeCallBack(DegreeCallBack callBack){this.callBack = callBack;}interface DegreeCallBack{void degreeFinish(int cur);}public void setWaitingProgress(){invalidate();}}
这篇关于简单仿写网易加载进度条的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!