自定义进度条
来源:互联网 发布:ubuntu vmware 编辑:程序博客网 时间:2024/05/16 09:42
最近公司在开发一个新项目,里面用到一些自定义组件,在此写下本人在制作自定义进度条的一个小Mode,如果有网友遇到同类问题,可以参考一下,同时也方便我自己做一下知识整理。
先来理清一下思路
1:继承 View,
2:重写里面的 OnMeasure()方法,及Ondraw()方法。
3:拿一个笔在一个画版上面画你想要的图形,记住,Android里面的组件都是画出来的。
4:在布局文件中引用自己的组件。
5:跟所自己的需求添加相应事件。
主要代码如下,
public class MyProgress extends View{
private float currentCount; //进度条当前值
private Paint paint;
private int mWidth , mHeight; //组件的 宽度 及 高度
public MyProgress(Context context) {
super(context);
initView(context);
}
public MyProgress(Context context, AttributeSet attrs, int defStyleAttr){
super(context, attrs, defStyleAttr);
initView(context);
}
public MyProgress(Context context, AttributeSet attrs){
super(context, attrs);
initView(context);
}
private void initView(Context context){
}
protected void onDraw(Canvas canvas){
super.onDraw(canvas);
Paint paint = new Paint();
paint.setAntiAlias(true);//这置其平滑
int round = mHeight /3;//这个地方是用来画矩形的的边角的
paint.setColor(Color.rgb(71, 76, 80)); //设置 画笔的颜色
/**
* Create a new rectangle with the specified coordinates. Note: no range checking is performed,
* so the caller must ensure that left <= right and top <= bottom.
*/
Rect rect = new Rect(0, 0, mWidth, mHeight);
canvas.drawRect(rect, paint);
//下面开始画第二个矩形
Rect secondRect = new Rect(0, 2, (int)(currentCount), mHeight -2);
paint.setColor(Color.rgb(254, 254, 20));
canvas.drawRect(secondRect, paint);
}
//将 dip 转成相素
private int dipToPx(int dip){
float scale = getContext().getResources().getDisplayMetrics().density;
return (int) (dip * scale + 0.5f * (dip >= 0 ? 1 : -1));
}
//设置进度条的当前值
public void setCurrentCount(float currentCount){
this.currentCount = currentCount ;
invalidate();
}
public float getCurrentCount(){
return currentCount;
}
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec){
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec);
int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec);
if(widthSpecMode == MeasureSpec.EXACTLY || widthSpecMode == MeasureSpec.AT_MOST){
mWidth = widthSpecSize;
}else{
mWidth = 0;
}
if(heightSpecMode == MeasureSpec.AT_MOST || heightSpecMode == MeasureSpec.UNSPECIFIED){
mHeight = dipToPx(18);
}else{
mHeight = heightSpecSize;
}
setMeasuredDimension(mWidth, mHeight);
}
}
XML布局文件引用:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<ProgressBar
android:id="@+id/progressBar1"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="100"
android:minHeight="15dp" />
<com.shenzhen.leynew.customprogress.MyProgress
android:id="@+id/customProgressBar"
android:layout_width="fill_parent"
android:layout_height="20dp"
/>
</LinearLayout>
事件处理及调用:
public class MainActivity extends Activity {
private MyProgress customProgress;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
progressListener();
}
//实例化自定义进度条
public void initView(){
customProgress = (MyProgress)this.findViewById(R.id.customProgressBar);
}
//给自定义进度条添加监听器
public void progressListener(){
customProgress.setOnTouchListener(new OnTouchListener(){
public boolean onTouch(View arg0, MotionEvent event){
int action = event.getAction();
//当手碰到组件的那一瞬间
if(action == MotionEvent.ACTION_DOWN){
customProgress.setCurrentCount(event.getX());
}
//当看在组件上移用的时候所触发的事件
if(action == MotionEvent.ACTION_MOVE){
sentInfo((int)event.getX());
}
//当事件抬起的时候
if(action == MotionEvent.ACTION_UP){
sentInfo((int)event.getX());
}
return true;
}
});
//此处是指当焦点发生改变的时候,会触发相应的事件机制
customProgress.setOnFocusChangeListener(new OnFocusChangeListener(){
public void onFocusChange(View arg0, boolean arg1) {
}
});
}
public void sentInfo(int cordinateX){
customProgress.setCurrentCount(cordinateX);
}
@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;
}
}
- 自定义进度条
- 自定义进度条
- 自定义进度条
- 自定义进度条
- 自定义进度条
- 自定义进度条
- 自定义进度条
- 自定义进度条
- 自定义进度条
- 自定义进度条
- 自定义进度条
- 自定义进度条
- 自定义进度条
- 自定义进度条
- 自定义进度条
- 自定义进度条
- 自定义进度条
- 自定义进度条
- 【js学习笔记-101】------借助<script>发送HTTP请求
- 建新网站要做的四个SEO优化
- 生成排列的方法
- 浏览器缓存机制
- Ubuntu12.04升级至13.04
- 自定义进度条
- java.lang.OutOfMemoryError: Java heap space异常解决方法。
- ubuntu的截图技巧
- 关于javamelody配置后,工程出错问题
- QTP常用的快捷键
- 如何上Gmail更快
- UDID替代方案
- 设计模式C++实现(12)——备忘录模式
- automake 工具使用