android113 Custom Progress bar

Source: Internet
Author: User

Mainactivity:

 PackageCom.itheima.monitor;ImportAndroid.os.Bundle;Importandroid.app.Activity;ImportAndroid.view.Menu;ImportAndroid.view.View;ImportAndroid.widget.ProgressBar; Public classMainactivityextendsActivity {PrivateMyProgressBar PB; Static{system.loadlibrary ("Hello"); } @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_main); PB= (MyProgressBar) Findviewbyid (R.ID.PB);//Custom progress barPb.setmax (100); }    Public voidStart (View v) {NewThread () { Public voidrun () {startmonitor ();       };   }.start (); }    Public voidStop (View v) {stopmonitor (); }        Public native voidStartMonitor ();  Public native voidStopmonitor ();  Public voidShowintpressure)   {pb.setpressure (pressure); }}

MyProgressBar. Java

 PackageCom.itheima.monitor;ImportAndroid.content.Context;ImportAndroid.graphics.Canvas;ImportAndroid.graphics.Color;ImportAndroid.graphics.Paint;ImportAndroid.util.AttributeSet;ImportAndroid.view.View;/*interface: <com.itheima.monitor.myprogressbarandroid:id= "@+id/pb" android:layout_height= "Wrap_content" Android: Layout_width= "Wrap_content"/>*/ Public classMyProgressBarextendsView {Private intpressure; Private intMax;  PublicMyProgressBar (context context, AttributeSet attrs,intDefstyle) {        Super(context, attrs, Defstyle); }     PublicMyProgressBar (Context context, AttributeSet attrs) {Super(context, attrs); }     PublicMyProgressBar (Context context) {Super(context); }     Public voidSetpressure (intpressure) {         This. Pressure =pressure; //Invalidates the current component MyProgressBar view, and if the current view is visible, then OnDraw is called and can only be used in the main thread//invalidate (); The main thread calls this methodPostinvalidate ();//The Strand city uses this method so that the progress bar will always draw the progress bar.             }     Public voidSetmax (intmax) {         This. Max =Max; }        //drawing content on custom components@Overrideprotected voidOnDraw (canvas canvas) {//any component that is displayed on the page will be called once by the OnDraw method .Paint paint =NewPaint ();//Brushes        if(Pressure < 50) {paint.setcolor (color.green); }        Else if(Pressure < 80) {Paint.setcolor (0xffff8800); }        Else{paint.setcolor (color.red); } canvas.drawrect (Ten, + max-pressure, +, + max, paint);//draw a rectangle, that is, draw a progress bar. Canvas.drawtext ("dddd", ten, ten, paint);//Write Text    }}

hello.c

#include <jni.h>#include<stdio.h>#include<stdlib.h>intgetpressure () {//... Monitoring voltage ... Operation to get pressure value    returnRAND ()%101;}intMonitor; Jniexportvoidjnicall java_com_itheima_monitor_mainactivity_startmonitor (jnienv* env, Jobject obj) {//obj is mainactivity,Monitor =1;  while(monitor) {//Get Boiler pressure        intpressure =getpressure ();//method of calling Java through reflection in C//Jclass (*findclass) (jnienv*, const char*);Jclass clazz = (*env)->findclass (env,"com/itheima/monitor/mainactivity"); //Jmethodid (*getmethodid) (jnienv*, Jclass, const char*, const char*);Jmethodid Methodid = (*env)->getmethodid (env, Clazz,"Show","(I) V");//(I) v is the signature of the method, the return value is the void parameter is int//void (*callvoidmethod) (jnienv*, Jobject, Jmethodid, ...);(*env)->callvoidmethod (env, obj, methodid, pressure);//Call the Java Show methodSleep1); }}jniexportvoidjnicall java_com_itheima_monitor_mainactivity_stopmonitor (jnienv*env, Jobject obj) {Monitor=0;}//Enter the project's bin\classes directory, execute Javap-s mainactivity's all-inclusive name Class name, and you can view the signature of all mainactivity methods. 

android113 Custom Progress bar

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.