自定义progressbar控件
来源:互联网 发布:谷歌数据分析师 编辑:程序博客网 时间:2024/06/13 23:05
先来看一下图片效果:
github地址:https://github.com/1181631922/OrliteDemo/tree/master
里面还有一些其他的demo
花了半天时间吧,大体实现了一下,发现自己对自定义view还是不熟,有待提高,下面来上一下实现代码,总体实现还是比较简单的:
先看自定义view类:
package com.fanyafeng.orlitedemo.myview;import android.content.Context;import android.graphics.Canvas;import android.graphics.Color;import android.graphics.Paint;import android.graphics.Rect;import android.util.AttributeSet;import android.util.Log;import android.view.View;/** * Created by 365rili on 16/4/20. */public class PercentProgressBarView extends View { private Paint reachPaint; private Paint unReachPaint; private Paint textPaint; private int reachPaintColor; private int unReachPaintColor; private int textPaintColor; private int percent; private String progressText; private String initText; private String finalText; public int getReachPaintColor() { return reachPaintColor; } public void setReachPaintColor(int reachPaintColor) { this.reachPaintColor = reachPaintColor; } public int getUnReachPaintColor() { return unReachPaintColor; } public void setUnReachPaintColor(int unReachPaintColor) { this.unReachPaintColor = unReachPaintColor; } public int getTextPaintColor() { return textPaintColor; } public void setTextPaintColor(int textPaintColor) { this.textPaintColor = textPaintColor; } public String getInitText() { return initText; } public void setInitText(String initText) { this.initText = initText; setPercent(0, initText); } public String getFinalText() { return finalText; } public void setFinalText(String finalText) { this.finalText = finalText; setPercent(100, finalText); } public int getPercent() { return percent; } public void setPercent(int percent, String progressText) { this.percent = percent; this.progressText = progressText; invalidate(); } public String getProgressText() { return progressText; } public PercentProgressBarView(Context context) { super(context); } public PercentProgressBarView(Context context, AttributeSet attrs) { super(context, attrs); reachPaint = new Paint(Paint.ANTI_ALIAS_FLAG); unReachPaint = new Paint(Paint.ANTI_ALIAS_FLAG); textPaint = new Paint(); } public PercentProgressBarView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); reachPaint.setAntiAlias(true); if (reachPaintColor != 0) { reachPaint.setColor(reachPaintColor); } else { reachPaint.setColor(Color.parseColor("#3498DB")); } reachPaint.setStyle(Paint.Style.STROKE); unReachPaint.setAntiAlias(true); if (unReachPaintColor != 0) { unReachPaint.setColor(unReachPaintColor); } else { unReachPaint.setColor(Color.parseColor("#CCCCCC")); } unReachPaint.setStyle(Paint.Style.STROKE); textPaint.setAntiAlias(true); if (textPaintColor != 0) { textPaint.setColor(textPaintColor); } else { textPaint.setColor(Color.BLUE); } float endX = getRight(); float startY = getTop(); float endY = getBottom(); float height = getHeight();// 画笔所画的高度 reachPaint.setStrokeWidth(height); unReachPaint.setStrokeWidth(height);// Log.d("TAG", "startx:" + startX + "endx:" + endX + "starty:" + startY + "endy:" + endY + "height:" + height + "width:" + width);// 画完成进度 textPaint.setTextSize(40); textPaint.setTextAlign(Paint.Align.LEFT); textPaint.setStrokeWidth(10); if (progressText != null) { if (percent >= 0 && percent <= 100) { Rect textBounds = new Rect(); textPaint.getTextBounds(progressText, 0, progressText.length(), textBounds); Paint.FontMetricsInt fontMetricsInt = textPaint.getFontMetricsInt(); float baseLine = (getMeasuredHeight() - fontMetricsInt.bottom + fontMetricsInt.top) / 2 - fontMetricsInt.top; float tempEndX = percent * (endX - textBounds.width()) / 100; Log.d("TAG", "percent数值:" + percent); Log.d("TAG", "进度条整体长度:" + endX); Log.d("TAG", "进度条显示:" + tempEndX);// 进度 canvas.drawText(progressText, tempEndX - textBounds.width(), baseLine, textPaint);// 画左侧已完成 canvas.drawLine(0, (endY - startY) / 2, tempEndX - (textBounds.width() + 30), (endY - startY) / 2, reachPaint);// 画右侧未完成 canvas.drawLine(tempEndX+textBounds.width()/2, (endY - startY) / 2, endX, (endY - startY) / 2, unReachPaint); /*// 进度 canvas.drawText(progressText, endX / 2 - textBounds.width() / 2, baseLine, textPaint);// 画左侧已完成 canvas.drawLine(0, (endY - startY) / 2, endX / 2 - textBounds.width() / 2 - 10, (endY - startY) / 2, reachPaint);// 画右侧未完成 canvas.drawLine(endX / 2 + textBounds.width() / 2 + 10, (endY - startY) / 2, endX, (endY - startY) / 2, unReachPaint);**/ } } }}再看一下xml:
<?xml version="1.0" encoding="utf-8"?><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:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" app:layout_behavior="@string/appbar_scrolling_view_behavior" tools:context="com.fanyafeng.orlitedemo.activity.ProgressBarActivity" tools:showIn="@layout/activity_progress_bar"> <com.fanyafeng.orlitedemo.myview.PercentProgressBarView android:id="@+id/my_progress" android:layout_marginTop="20dp" android:layout_marginLeft="20dp" android:layout_marginRight="20dp" android:layout_width="match_parent" android:layout_height="18dp" /></LinearLayout>
ok,最后看一下activity:
package com.fanyafeng.orlitedemo.activity;import android.graphics.Color;import android.os.Bundle;import android.os.Handler;import android.os.Looper;import android.support.design.widget.FloatingActionButton;import android.support.design.widget.Snackbar;import android.support.v7.app.AppCompatActivity;import android.support.v7.widget.Toolbar;import android.util.Log;import android.view.View;import android.widget.TextView;import com.fanyafeng.orlitedemo.R;import com.fanyafeng.orlitedemo.myview.PercentProgressBarView;import org.w3c.dom.Text;public class ProgressBarActivity extends AppCompatActivity { private PercentProgressBarView my_progress; private Handler handler; private int count = 0; private Runnable runnable; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_progress_bar); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); fab.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) .setAction("Action", null).show(); } }); initView(); initData(); } private void initView() { my_progress = (PercentProgressBarView) findViewById(R.id.my_progress); my_progress.setInitText("开始下载"); my_progress.setTextPaintColor(Color.BLACK); } private void initData() { handler = new Handler(Looper.getMainLooper()); runnable = new Runnable() { @Override public void run() { if (count < 100) { count++; my_progress.setPercent(count,count+"%"); handler.postDelayed(this,100); Log.d("TAG","百分比:"+count);// if (count==100){// my_progress.setFinalText("下载完毕");// } } } }; handler.postDelayed(runnable, 1000); } @Override protected void onStop() { super.onStop(); handler.removeCallbacks(runnable); }}后期还会慢慢完善,自己想加入控制类的方法自己可以定义get和set方法,第一次写自定义view还有好多问题吧,只是简陋的实现了功能,还有很多需要优化,细化
写给自己:贵在自知
0 0
- 自定义控件之-----progressBar
- 自定义控件之-----progressBar
- 自定义控件之-----progressBar
- 自定义控件之ProgressBar
- 自定义progressbar控件
- Andorid 自定义ProgressBar控件
- 【android自定义控件】ProgressBar自定义
- Android自定义view之下载控件,ProgressBar
- Android自定义控件---继承ProgressBar功能扩展
- ProgressBar控件
- ProgressBar控件
- 自定义progressbar
- 自定义ProgressBar
- 自定义Progressbar
- 自定义ProgressBar
- 自定义ProgressBar
- 自定义progressbar
- ProgressBar 自定义
- 记录加入CSDN的第一天
- angular的$q服务以及js的promise编程
- label型向下选择类似选择器效果 demo
- memcached在centos minimal安装环境下编译安装
- Eclipse 4.5中安装SVN插件
- 自定义progressbar控件
- 廖雪峰的官方网站
- 使用Tinyfox部署Web API 读取配置文件问题
- 链表
- 大数问题之26进制转化
- Git教程
- java.lang.Math下的方法
- iOS开发- UICollectionView详解+实例
- Xcode使用技巧