广告
返回顶部
首页 > 资讯 > 移动开发 >android自定义进度条移动效果
  • 157
分享到

android自定义进度条移动效果

2024-04-02 19:04:59 157人浏览 独家记忆
摘要

本文实例为大家分享了Android实现进度条移动效果的具体代码,供大家参考,具体内容如下 自定义进度条,效果如下: CustomViewActivity.java public

本文实例为大家分享了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


<LinearLayout xmlns:android="Http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".custom.CustomViewActivity">
    <LinearLayout
        android:layout_marginTop="20px"
        android:layout_marginHorizontal="30px"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView
            android:text="进度:"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
        <RelativeLayout
            android:layout_weight="4"
            android:layout_width="0dp"
            android:layout_height="wrap_content">
            <ProgressBar
                android:id="@+id/progressbar"
                style="@android:style/Widget.ProgressBar.Horizontal"
                android:max="100"
                android:progress="50"
                android:progressDrawable="@drawable/progress_drawable"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"/>
            <LinearLayout
                android:orientation="horizontal"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
                <TextView
                    android:id="@+id/tv_schedule"
                    android:text=""
                    android:gravity="center_horizontal"
                    android:layout_weight="1"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"/>
                <View
                    android:id="@+id/view"
                    android:layout_weight="1"
                    android:layout_width="0dp"
                    android:layout_height="20px"/>
            </LinearLayout>
        </RelativeLayout>
    </LinearLayout>
</LinearLayout>

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

--结束END--

本文标题: android自定义进度条移动效果

本文链接: https://www.lsjlt.com/news/127205.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作