progress各种各样的都有,自定义大多数也是简单的,根据业务需求来自己定义,记录一下,先上效果图
本来想找个第三方改改就上的,不过自己的业务需求有点不搭,一下子没找到合适的,也没这么多时间去找了,想想还是自己写个吧,因为也简单。
主要就是需求就是椭圆进度,百分比跟随渐变背景,这样一想其实就是一个布局,然后控制里面的进度长度,或者移动,我这是控制长度,这样毕竟简单,而且扩展好,以后进度条有什么奇葩需求也好改。
import android.content.Context; import android.content.res.TypedArray; import android.graphics.Color; import android.support.annotation.AttrRes; import android.support.annotation.NonNull; import android.support.annotation.Nullable; import android.util.AttributeSet; import android.view.Gravity; import android.view.View; import android.view.ViewGroup; import android.widget.FrameLayout; import android.widget.TextView; /** * Created by LiuZhen on 2017/7/8. */ public class UpdateProgressBar extends FrameLayout { private TextView tv_progress; private int width; private ViewGroup.LayoutParams params; /** * The progress text offset. */ private int mOffset; /** * The progress text size. */ private float mTextSize; /** * The progress text color. */ private int mTextColor; private float default_text_size; /** * The progress area bar color. */ private int mReachedBarColor; /** * The bar unreached area color. */ private int mUnreachedBarColor; private final int default_reached_color = Color.rgb(66, 145, 241); private final int default_unreached_color = Color.rgb(204, 204, 204); private final int default_text_color = Color.rgb(66, 145, 241); public UpdateProgressBar(@NonNull Context context) { this(context,null); } public UpdateProgressBar(@NonNull Context context, @Nullable AttributeSet attrs) { this(context, attrs,0); } public UpdateProgressBar(@NonNull Context context, @Nullable AttributeSet attrs, @AttrRes int defStyleAttr) { super(context, attrs, defStyleAttr); init(attrs, defStyleAttr); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int desiredWidth = 100; int desiredHeight = 100; int widthMode = MeasureSpec.getMode(widthMeasureSpec); int widthSize = MeasureSpec.getSize(widthMeasureSpec); int heightMode = MeasureSpec.getMode(heightMeasureSpec); int heightSize = MeasureSpec.getSize(heightMeasureSpec); int height; //Measure Width if (widthMode == MeasureSpec.EXACTLY) { //Must be this size width = widthSize; } else if (widthMode == MeasureSpec.AT_MOST) { //Can't be bigger than... width = Math.min(desiredWidth, widthSize); } else { //Be whatever you want width = desiredWidth; } //Measure Height if (heightMode == MeasureSpec.EXACTLY) { //Must be this size height = heightSize; } else if (heightMode == MeasureSpec.AT_MOST) { //Can't be bigger than... height = Math.min(desiredHeight, heightSize); } else { //Be whatever you want height = desiredHeight; } int childCount = getChildCount(); for (int i = 0; i
用法布局文件
MainActivity
import android.os.Bundle; import android.support.v7.app.ActionBarActivity; import android.support.v7.app.AppCompatActivity; import android.view.Menu; import android.view.MenuItem; import android.widget.Toast; import com.progressbar.NumberProgressBar; import java.util.Timer; import java.util.TimerTask; public class MainActivity extends AppCompatActivity { private Timer timer; private UpdateProgressBar progressBar; private int progress; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); progressBar = (UpdateProgressBar)findViewById(R.id.progress); timer = new Timer(); timer.schedule(new TimerTask() { @Override public void run() { runOnUiThread(new Runnable() { @Override public void run() { progress++; progressBar.setProgress(progress); if(progress == 100) { Toast.makeText(getApplicationContext(), getString(R.string.finish), Toast.LENGTH_SHORT).show(); // progress = 0; // progressBar.setProgress(0); timer.cancel(); } } }); } }, 1000, 100); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } @Override protected void onDestroy() { super.onDestroy(); timer.cancel(); } }
渐变背景
<&#63;xml version="1.0" encoding="utf-8"&#63;>
<&#63;xml version="1.0" encoding="utf-8"&#63;>
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。