android自定义进度条移动效果

本文实例为大家分享了android实现进度条移动效果的具体代码,供大家参考,具体内容如下

自定义进度条,效果如下:

CustomViewActivity.java

public class CustomViewActivity extends Activity {
    private static final String TAG = "CustomViewActivity";
    private TextView tv_schedule;
    private View view;
    private ProgressBar progressBar;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_custom_view);
        initView();
    }
 
    private void initView() {
        initProgressView();
    }
 
    private void initProgressView() {
        tv_schedule = findViewById(R.id.tv_schedule);
        view = findViewById(R.id.view);
        progressBar = findViewById(R.id.progressbar);
        int finishSpeed = 2;
        Log.d(TAG,"finishSpeed="+finishSpeed);
        float speedPer = ((float) finishSpeed)/8*100;
        Log.d(TAG,"speedPer="+speedPer);
        tv_schedule.setText(speedPer+"%");
        progressBar.setProgress((int)speedPer);
        setTextViewWidth(tv_schedule,view,speedPer);
    }
 
    private void setTextViewWidth(TextView textView,View view,float speed) {
        Paint paint = new Paint();
        float textWidth = paint.measureText(textView.getText().toString());
        Log.d(TAG,"textWidth="+textWidth);
        LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) textView.getLayoutParams();
        LinearLayout.LayoutParams params1 = (LinearLayout.LayoutParams) view.getLayoutParams();
        if (textWidth > speed) {
            params.weight = textWidth;
            params1.weight = 100 - textWidth;
        } else {
            params.weight = speed;
            params1.weight = 100 - speed;
        }
 
    }
}

activity_custom_view.xml


    
        
        
            
            
                
                
            
        
    

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

你可能感兴趣的:(android自定义进度条移动效果)