Android-custom control level indicator, android-level indicator

Source: Internet
Author: User
Tags drawtext getcolor

Android-custom control level indicator, android-level indicator

Because Android is widely used, it is often used in the industry. For example, you can use android to monitor and display some data in the industry and perform some automatic control. Of course, here, I did not do these research on automatic control, but just made a control, the level indication, in fact, is inherited from progressbar, and then re-write a measurement and drawing, it is a review of the custom control.

The final result we need to do is the following:

Here, we want to set the following attributes of this indicator:

The container wall thickness, the color of the container wall, the width of the liquid in the container, the total height of the liquid, the color display of the current height of the liquid, the display of the text indicating the current height, the text indicating the text the display of the size.

Setting the above attributes will make the display more intuitive and user-friendly in the implementation of applications. Let's start making our indicator.

1 <? Xml version = "1.0" encoding = "UTF-8"?> 2 <resources> 3 4 <declare-styleable name = "JianWWIndicateProgress"> 5 <attr name = "progress_height" format = "dimension"/> 6 <attr name = "progress_width" format = "dimension"/> 7 <attr name = "progress_unreachedcolor" format = "color"/> 8 <attr name = "progress_reached_color" format = "color"/> 9 <attr name = "progress_reached_height" format = "integer"/> 10 <attr name = "progress_cheek_width" format = "dimension"/> 11 <attr name = "progress_cheek_color" format = "color"/> "/> 12 <attr name = "progress_reached_textsize" format = "dimension"/> 13 <attr name = "progress_reached_textcolor" format = "color"/> 14 </declare-styleable> 15 16 </resources>

2. inherit the progressbar. Here it inherits the progressbar mainly to get the current progress using the getProgress () method of progressbar, and some methods provided in the setss such as setProgress () method, this facilitates data processing.

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;/*** Author: jww mail: bluejww@163.com time: March 8, 2016 */public class JianWWIndicateProgress extends ProgressBar {private static final int unreached_color = 0Xaa0000ff; // unarrived color private static final int reached_color = 0Xaaff0000; // arrival color private static final int progress_height = 150; // default private static final int progress_width = 100; // private static final int reached_height = 60; // The private static final int progress_cheek_width = 2 by default; // the container's Border width is private static final int progress_cheek_color = 0x660000ff; // container border color private static final int progress_reached_textsize = 10; // indicates the font size private static final int progress_reached_textcolor = 0Xff00ff00; // indicates the font color protected int unReachedColor; // The color that has not been reached protected int reachedColor; // reaches the color protected int progressHeight; // the total height of the liquid level in the container protected int progressWidth; // The width of the liquid surface in the container protected int reachedHeight; // protected int cheekWidth when the default liquid level reaches; // the container's Border Width protected int cheekColor; // the container's border color protected int reachedTextsize; // indicator font size protected int reachedTextcolor; // indicator font color protected float widthZoom; protected float heightZoom;/*** dp 2 px ***/protected int dp2px (int dpVal) {return (int) TypedValue. applyDimension (TypedValue. COMPLEX_UNIT_DIP, dpVal, getResources (). getDisplayMetrics ();}/*** sp 2 px **/protected int sp2px (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, 0);} public JianWWIndicateProgress (Context context, AttributeSet asets, int defStyle) {super (context, asets, defStyle); obtainStyledAttributes (asets); // paint. setTextSize (reachedTextsize); // paint. setColor (reachedTextcolor);}/*** define attributes ** @ param asets attributes */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); // The arrival color progressHeight = (int) typeArray. getDimension (R. styleable. jianWWIndicateProgress_progress_height, progress_height); progressWidth = dp2px (int) typeArray. getDimension (R. styleable. jianWWIndicateProgress_progress_width, progress_width); // The total container 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); // container's Border 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); // indicates the font size of reachedTextcolor = typeArray. getColor (R. styleable. jianWWIndicateProgress_progress_reached_textcolor, progress_reached_textcolor); // specifies the font color typeArray. recycle ();}/*** onMeasure is the arrangement of the space occupied by the drawn control in the future. */@ Overrideprotected synchronized void onMeasure (int widthMeasureSpec, int heightMeasureSpec) {super. onMeasure (widthMeasureSpec, heightMeasureSpec); int totalWidth = measurdWidth (widthMeasureSpec); int totalHeight = measurdHeight (heightMeasureSpec); trim (totalWidth, totalHeight );} /***** @ param widthMeasureSpec * @ return get width size */private int measurdWidth (int widthMeasureSpec) {int result = 0; int widthSpaceSize = MeasureSpec. getSize (widthMeasureSpec); int widthSpaceMode = MeasureSpec. getMode (widthMeasureSpec); if (widthSpaceMode = MeasureSpec. EXACTLY) {result = widthSpaceSize; widthZoom = widthSpaceSize/(progressWidth + 2 * cheekWidth); progressWidth = (int) (progressWidth * widthZoom);} else if (widthSpaceMode = MeasureSpec. UNSPECIFIED) {result = Math. max (widthSpaceSize, getPaddingLeft () + getPaddingRight () + progressWidth + 2 * cheekWidth);} else if (widthSpaceMode = MeasureSpec. AT_MOST) {result = Math. min (widthSpaceSize, getPaddingLeft () + getPaddingRight () + progressWidth + 2 * cheekWidth);} return result ;} /***** @ param heightMeasureSpec * @ return get Height size */private int measurdHeight (int heightMeasureSpec) {int result = 0; int heightSpaceSize = MeasureSpec. getSize (heightMeasureSpec); int heightSpaceMode = MeasureSpec. getMode (heightMeasureSpec); if (heightSpaceMode = MeasureSpec. EXACTLY) {result = success; heightZoom = heightSpaceSize/(progressHeight + 2 * cheekWidth); progressHeight = (int) (progressHeight * heightZoom);} else if (response = MeasureSpec. UNSPECIFIED) {result = Math. max (heightSpaceSize, getPaddingTop () + getPaddingBottom () + progressHeight + 2 * cheekWidth);} else if (heightSpaceMode = MeasureSpec. AT_MOST) {result = Math. min (heightSpaceSize, getPaddingTop () + getPaddingBottom () + progressHeight + 2 * cheekWidth);} return result ;} /*** draw control */@ Overrideprotected synchronized void onDraw (Canvas canvas) {super. onDraw (canvas); radio = getProgress () * 1.0f/getMax (); progressNowHeight = (int) (progressHeight * radio); // Save the canvas status canvas before painting. save (); // move the canvas to be drawn. translate (getPaddingLeft (), getPaddingRight (); // draw the container shell paintCheek. setColor (cheekColor); // paint brush color paintCheek. setAntiAlias (true); // whether the paintCheek is excessive. setStyle (Style. STROKE); // hollow paintCheek. setStrokeWidth (cheekWidth); // Border Width canvas. drawRect (cheekWidth/2, cheekWidth/2, progressWidth + cheekWidth * 3/2, progressHeight + cheekWidth * 3/2, paintCheek); // paint the total liquid level. setColor (unReachedColor); paint. setStyle (Style. FILL); canvas. drawRect (cheekWidth, cheekWidth, progressWidth + cheekWidth, progressHeight + cheekWidth, paint); // paint the current level. setStyle (Style. FILL); paint. setColor (reachedColor); canvas. drawRect (cheekWidth, cheekWidth + progressHeight-progressNowHeight, progressWidth + cheekWidth, progressHeight + cheekWidth, paint); // paint level indicator String text = getProgress () + "%"; paint. setTextSize (reachedTextsize); paint. setColor (reachedTextcolor); float textHeight = sp2px (reachedTextsize)/2; if (progressNowHeight> = progressHeight/2) {canvas. drawText (text, cheekWidth + progressWidth/2, cheekWidth + progressHeight-progressNowHeight + textHeight, paint);} else {canvas. drawText (text, cheekWidth + progressWidth/2, cheekWidth + progressHeight-progressNowHeight, paint);} canvas. restore ();}}

3. It is referenced in the layout.

<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="100"        app:progress_cheek_color="#660000ff"        app:progress_cheek_width="4dp"        app:progress_height="160dp"        app:progress_reached_color="#ff0000"        app:progress_reached_textcolor="#000000"        app:progress_reached_textsize="12sp"        app:progress_unreachedcolor="#4400ff00"        app:progress_width="40dp" /></RelativeLayout>

4. Initialization and use in 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 >= 100) {jprogress.setProgress(0);}mHandler.sendEmptyMessageDelayed(100, 100);};};@Overrideprotected 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(100);}}

Well, the writing is rough. You can discuss it with others and implement the function. However, some details are not well handled. ^

360 cloud disk code sharing: Access Password 9294

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.