Android中自定义控件之液位指示器_Android

由于安卓应用很广泛,在工业中也常有一些应用,比如可以用安卓来去工业中的一些数据进行实现的监测,显示,同时可以做一些自动化控制,当然在这里,我不是做这些自动化控制方面的研究,只是做一个控件,液位指示,其实就是继承自progressbar,然后重新写一测量与绘制,算是对自定义控件进行一下复习。

  我们要做的最终就是下面这个效果:

在这里,我们要做到对这个指示器的以下属性可设置:

容器壁的厚度、容器壁的颜色、容器中液体的宽度、液体总高度、液体当前高度的颜色显示、液体未达到颜色显示、当前高度的文字指示、指示文字大小的显示。

对以上属性的可以设置,会使在实现应用中让显示更加直观人性化。下面就开始我们的指示器的制作。

1.先在项目的res目录下建一个resouce文件,用来定义自定义的属性,这些都会在下面给出的源码中给出,新人可以参考下,老家伙你就绕道吧^^:

<?xml version="." encoding="utf-"?>
<resources>
<declare-styleable name="JianWWIndicateProgress">
<attr name="progress_height" format="dimension" />
<attr name="progress_width" format="dimension" />
<attr name="progress_unreachedcolor" format="color" />
<attr name="progress_reached_color" format="color" />
<attr name="progress_reached_height" format="integer" />
<attr name="progress_cheek_width" format="dimension" />
<attr name="progress_cheek_color" format="color" />
<attr name="progress_reached_textsize" format="dimension" />
<attr name="progress_reached_textcolor" format="color" />
</declare-styleable>
</resources> 

2.继承progressbar,这里继承他主要是为了能够用progressbar的getProgress()方法得到当前的progress,与setProgress()方法等progress中提供的一些方法,便于对数据的一些处理。

package com.jianww.firstview;
import com.example.jwwcallback.R;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.widget.ProgressBar;
public class JianWWIndicateProgress extends ProgressBar {
private static final int unreached_color = Xaaff;// 未到达的颜色
private static final int reached_color = Xaaff;// 到达颜色
private static final int progress_height = ;// 容器中液位的默认高度
private static final int progress_width = ;// 容器中液位的宽度
private static final int reached_height = ;// 默认到达到达的高度
private static final int progress_cheek_width = ;// 容器的边框宽度
private static final int progress_cheek_color = xff;// 容器的边框颜色
private static final int progress_reached_textsize = ;// 指示字体尺寸
private static final int progress_reached_textcolor = Xffff;// 指示字体颜色
protected int unReachedColor;// 未到达的颜色
protected int reachedColor;// 到达颜色
protected int progressHeight;// 容器中液位的总高度
protected int progressWidth;// 容器中液面的宽度
protected int reachedHeight;// 默认液位到达的到达的高度
protected int cheekWidth;// 容器的边框宽度
protected int cheekColor;// 容器的边框颜色
protected int reachedTextsize;// 指示字体尺寸
protected int reachedTextcolor;// 指示字体颜色
protected float widthZoom;
protected float heightZoom;
/**
* dp px
*
*/
protected int dppx(int dpVal) {
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dpVal, getResources().getDisplayMetrics());
}
/**
* sp px
*
*/
protected int sppx(int spVal) {
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, spVal, getResources().getDisplayMetrics());
}
private Paint paintCheek = new Paint();
private Paint paint = new Paint();
private float radio;
private float progressNowHeight;
public JianWWIndicateProgress(Context context) {
this(context, null);
}
public JianWWIndicateProgress(Context context, AttributeSet asets) {
this(context, asets, );
}
public JianWWIndicateProgress(Context context, AttributeSet asets, int defStyle) {
super(context, asets, defStyle);
obtainStyledAttributes(asets);
// paint.setTextSize(reachedTextsize);
// paint.setColor(reachedTextcolor);
}
/**
* 定义属性
*
* @param asets属性
*/
private void obtainStyledAttributes(AttributeSet asets) {
final TypedArray typeArray = getContext().obtainStyledAttributes(asets, R.styleable.JianWWIndicateProgress);
unReachedColor = typeArray.getColor(R.styleable.JianWWIndicateProgress_progress_unreachedcolor,
unreached_color);
reachedColor = typeArray.getColor(R.styleable.JianWWIndicateProgress_progress_reached_color, reached_color);// 到达颜色
progressHeight = (int) typeArray.getDimension(R.styleable.JianWWIndicateProgress_progress_height,
progress_height);
progressWidth = dppx(
(int) typeArray.getDimension(R.styleable.JianWWIndicateProgress_progress_width, progress_width));// 容器的总宽度
reachedHeight = (int) typeArray.getDimension(R.styleable.JianWWIndicateProgress_progress_reached_height,
reached_height);// 到达的高度
cheekWidth = (int) typeArray.getDimension(R.styleable.JianWWIndicateProgress_progress_cheek_width,
progress_cheek_width);// 容器的边框宽度
cheekColor = typeArray.getColor(R.styleable.JianWWIndicateProgress_progress_cheek_color, progress_cheek_color);
reachedTextsize = (int) typeArray.getDimension(R.styleable.JianWWIndicateProgress_progress_reached_textsize,
progress_reached_textsize);// 指示字体尺寸
reachedTextcolor = typeArray.getColor(R.styleable.JianWWIndicateProgress_progress_reached_textcolor,
progress_reached_textcolor);// 指示字体颜色
typeArray.recycle();
}
/**
* onMeasure是对绘出的控件将来占的空间进行的安排,
*/
@Override
protected synchronized void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int totalWidth = measurdWidth(widthMeasureSpec);
int totalHeight = measurdHeight(heightMeasureSpec);
setMeasuredDimension(totalWidth, totalHeight);
}
/**
*
* @param widthMeasureSpec
* @return 获取宽度尺寸
*/
private int measurdWidth(int widthMeasureSpec) {
int result = ;
int widthSpaceSize = MeasureSpec.getSize(widthMeasureSpec);
int widthSpaceMode = MeasureSpec.getMode(widthMeasureSpec);
if (widthSpaceMode == MeasureSpec.EXACTLY) {
result = widthSpaceSize;
widthZoom = widthSpaceSize/(progressWidth+*cheekWidth);
progressWidth = (int) (progressWidth * widthZoom);
} else if (widthSpaceMode == MeasureSpec.UNSPECIFIED) {
result = Math.max(widthSpaceSize, getPaddingLeft() + getPaddingRight() + progressWidth + * cheekWidth);
} else if (widthSpaceMode == MeasureSpec.AT_MOST) {
result = Math.min(widthSpaceSize, getPaddingLeft() + getPaddingRight() + progressWidth + * cheekWidth);
}
return result;
}
/**
*
* @param heightMeasureSpec
* @return获取高度尺寸
*/
private int measurdHeight(int heightMeasureSpec) {
int result = ;
int heightSpaceSize = MeasureSpec.getSize(heightMeasureSpec);
int heightSpaceMode = MeasureSpec.getMode(heightMeasureSpec);
if (heightSpaceMode == MeasureSpec.EXACTLY) {
result = heightSpaceSize;
heightZoom = heightSpaceSize/(progressHeight+*cheekWidth);
progressHeight = (int) (progressHeight*heightZoom);
} else if (heightSpaceMode == MeasureSpec.UNSPECIFIED) {
result = Math.max(heightSpaceSize, getPaddingTop() + getPaddingBottom() + progressHeight + * cheekWidth);
} else if (heightSpaceMode == MeasureSpec.AT_MOST) {
result = Math.min(heightSpaceSize, getPaddingTop() + getPaddingBottom() + progressHeight + * cheekWidth);
}
return result;
}
/**
* 绘制控件
*/
@Override
protected synchronized void onDraw(Canvas canvas) {
super.onDraw(canvas);
radio = getProgress() * .f / getMax();
progressNowHeight = (int) (progressHeight * radio);
// 存绘画前画布状态
canvas.save();
// 把画布移动到要绘制的点
canvas.translate(getPaddingLeft(), getPaddingRight());
// 绘制容器外壳
paintCheek.setColor(cheekColor);// 画笔颜色
paintCheek.setAntiAlias(true);// 是否过度
paintCheek.setStyle(Style.STROKE);// 空心
paintCheek.setStrokeWidth(cheekWidth);// 边框的宽度
canvas.drawRect(cheekWidth/, cheekWidth/, progressWidth + cheekWidth*/, progressHeight + cheekWidth*/,
paintCheek);
// 绘总液位
paint.setColor(unReachedColor);
paint.setStyle(Style.FILL);
canvas.drawRect(cheekWidth, cheekWidth, progressWidth+cheekWidth, progressHeight+cheekWidth,
paint);
// 绘当前液位
paint.setStyle(Style.FILL);
paint.setColor(reachedColor);
canvas.drawRect(cheekWidth, cheekWidth + progressHeight - progressNowHeight,
progressWidth + cheekWidth, progressHeight + cheekWidth, paint);
// 绘液位指示
String text = getProgress() + "%";
paint.setTextSize(reachedTextsize);
paint.setColor(reachedTextcolor);
float textHeight = sppx(reachedTextsize)/;
if(progressNowHeight >= progressHeight/){
canvas.drawText(text, cheekWidth + progressWidth / , cheekWidth + progressHeight - progressNowHeight+textHeight, paint);
}else{
canvas.drawText(text, cheekWidth + progressWidth / , cheekWidth + progressHeight - progressNowHeight, paint);
}
canvas.restore();
}
}

3.就是在布局中的引用了

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:jwwprogress="http://schemas.android.com/apk/res-auto"
xmlns:app="http://schemas.android.com/apk/res/com.example.jwwcallback"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center" >
<com.jianww.firstview.JianWWIndicateProgress
android:id="@+id/jp_progress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:max=""
app:progress_cheek_color="#ff"
app:progress_cheek_width="dp"
app:progress_height="dp"
app:progress_reached_color="#ff"
app:progress_reached_textcolor="#"
app:progress_reached_textsize="sp"
app:progress_unreachedcolor="#ff"
app:progress_width="dp" />
</RelativeLayout> 

4.就是在acitivity中的初始化与使用。

package com.example.jwwmain;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Random;
import com.example.jwwcallback.R;
import com.jianww.firstview.JianWWIndicateProgress;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
public class MainActivity extends Activity {
private JianWWIndicateProgress jprogress;
private int nowProgress;
private Handler mHandler = new Handler() {
public void handleMessage(android.os.Message msg) {
int progress = jprogress.getProgress();
jprogress.setProgress(++progress);
if (progress >= ) {
jprogress.setProgress();
}
mHandler.sendEmptyMessageDelayed(, );
};
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
jprogress = (JianWWIndicateProgress) findViewById(R.id.jp_progress);
mHandler.sendEmptyMessage();
}
}

关于Android中自定义控件之液位指示器的相关知识就给大家介绍到这里,希望对大家有所帮助!

以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索Android自定义控件
液位指示器
液位指示器、便携式液位指示器、润滑脂液位指示器、机油液位指示器管、android 自定义控件,以便于您获取更多的相关知识。

时间: 2024-08-26 03:57:22

Android中自定义控件之液位指示器_Android的相关文章

Android中自定义控件之液位指示器

由于安卓应用很广泛,在工业中也常有一些应用,比如可以用安卓来去工业中的一些数据进行实现的监测,显示,同时可以做一些自动化控制,当然在这里,我不是做这些自动化控制方面的研究,只是做一个控件,液位指示,其实就是继承自progressbar,然后重新写一测量与绘制,算是对自定义控件进行一下复习. 我们要做的最终就是下面这个效果: 在这里,我们要做到对这个指示器的以下属性可设置: 容器壁的厚度.容器壁的颜色.容器中液体的宽度.液体总高度.液体当前高度的颜色显示.液体未达到颜色显示.当前高度的文字指示.指

Android中Intent习惯用法_Android

Android中的Intent是一个非常重要的类,如果对Intent不是特别了解,可以参见<详解Android中Intent的使用方法>.如果对Intent Filter不是特别了解,可以参见<详解Android中Intent对象与Intent Filter过滤匹配过程>. 本文着重讲一下Android中一些常见的Intent的习惯用法,比如如何通过Intent发送短信.发送邮件.启动摄像机拍照录视频.设置闹铃.打开WIFI设置界面等等. 限于篇幅,本文分为上下两篇,这是上篇. 发

详解Android中处理崩溃异常_Android

大家都知道,现在安装Android系统的手机版本和设备千差万别,在模拟器上运行良好的程序安装到某款手机上说不定就出现崩溃的现象,开发者个人不可能购买所有设备逐个调试,所以在程序发布出去之后,如果出现了崩溃现象,开发者应该及时获取在该设备上导致崩溃的信息,这对于下一个版本的bug修复帮助极大,所以今天就来介绍一下如何在程序崩溃的情况下收集相关的设备参数信息和具体的异常信息,并发送这些信息到服务器供开发者分析和调试程序. 我们先建立一个crash项目,项目结构如图: 在MainActivity.ja

从源码剖析Android中的Intent组件_Android

我们知道,Intent主要用来激活安卓几大组件,那么它具体是怎样来激活的?激活时是否可以携带java对象?为何要将对象序列化后才能传递? 一.Intent官网解释Intent可以被startActivity用来加载Activity,也可以被broadcastIntent发送给指定的BroadReceiver组件, 或者被startService.bingService来与后台service通信. Intent最主要作用就是加载Activity,好比Activity之间的胶水. Intent数据结

详解Android中的Context抽象类_Android

关于Context我们首先应该知道: (1)它描述的是一个应用程序环境的信息,即上下文. (2)该类是一个抽象(abstract class)类,Android提供了该抽象类的具体实现类(后面我们会讲到是ContextIml类). (3)通过它我们可以获取应用程序的资源和类,也包括一些应用级别操作,例如:启动一个Activity,发送广播,接受Intent信息等.. 于是,我们可以利用该Context对象去构建应用级别操作(application-level operations) .一.Con

Android中的序列化浅析_Android

序列化原因 序列化的原因基本可以归纳为以下三种情况: 1.永久性保存对象,保存对象的字节序列到本地文件中: 2.对象在网络中传递: 3.对象在IPC间传递. 序列化方法 在Android系统中关于序列化的方法一般有两种,分别是实现Serializable接口和Parcelable接口,其中Serializable接口是来自Java中的序列化接口,而Parcelable是Android自带的序列化接口. 上述的两种序列化接口都有各自不同的优缺点,我们在实际使用时需根据不同情况而定. 1.Seria

Android中layout属性大全_Android

本文总结了Android中layout属性的含义与用法.分享给大家供大家参考.具体如下: 布局: AbsoluteLayout(绝对布局): xmlns:android="http://scmemas.android.com/apk/res/android" style="@..." android:clipChildren="true|false" android:clipToPadding="true|false" and

Android中TelephonyManager用法实例_Android

本文实例讲述了Android中TelephonyManager用法.分享给大家供大家参考,具体如下: 一.概述: TelephonyManager类主要提供了一系列用于访问与手机通讯相关的状态和信息的get方法.其中包括手机SIM的状态和信息.电信网络的状态及手机用户的信息.在应用程序中可以使用这些get方法获取相关数据. TelephonyManager类的对象可以通过Context.getSystemService(Context.TELEPHONY_SERVICE)方法来获得,需要注意的是

Android中Handler消息传递机制_Android

Handler 是用来干什么的? 1)执行计划任务,可以在预定的时间执行某些任务,可以模拟定时器 2)线程间通信.在Android的应用启动时,会创建一个主线程,主线程会创建一个消息队列来处理各种消息.当你创建子线程时,你可以在你的子线程中拿到父线程中创建的Handler 对象,就可以通过该对象向父线程的消息队列发送消息了.由于Android要求在UI线程中更新界面,因此,可以通过该方法在其它线程中更新界面. 出于性能优化考虑,Android的UI操作并不是线程安全的,这意味着如果有多个线程并发