android113 自定义进度条

MainActivity:

package com.itheima.monitor;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.ProgressBar;
public class MainActivity extends Activity {
private MyProgressBar pb;
static{
System.loadLibrary("hello");
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pb = (MyProgressBar) findViewById(R.id.pb);//自定义进度条
pb.setMax(100);
}
public void start(View v){
new Thread(){
public void run() {
startMonitor();
};
}.start();
}
public void stop(View v){
stopMonitor();
} public native void startMonitor();
public native void stopMonitor(); public void show(int pressure){
pb.setPressure(pressure);
}
}

MyProgressBar。java

package com.itheima.monitor;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View; /*界面:
<com.itheima.monitor.MyProgressBar
android:id="@+id/pb"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
/>*/ public class MyProgressBar extends View { private int pressure;
private int max;
public MyProgressBar(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
} public MyProgressBar(Context context, AttributeSet attrs) {
super(context, attrs);
} public MyProgressBar(Context context) {
super(context);
} public void setPressure(int pressure) {
this.pressure = pressure;
//使当前组件MyProgressBar视图无效,如果当前视图是可见的,那么onDraw会调用,只能在主线程使用
// invalidate(); 主线程调用这个方法
postInvalidate();//子线城用这个方法,这样进度条才会一直绘制进度条。 }
public void setMax(int max) {
this.max = max;
} //在自定义组件绘制内容
@Override
protected void onDraw(Canvas canvas) {
//任何组件在显示在页面上的时候onDraw方法都会调用一次,
Paint paint = new Paint();//画笔
if(pressure < 50){
paint.setColor(Color.GREEN);
}
else if(pressure < 80){
paint.setColor(0xffff8800);
}
else {
paint.setColor(Color.RED);
}
canvas.drawRect(10, 10 + max - pressure, 30, 10 + max, paint);//画矩形,也就是画进度条。
canvas.drawText("dddd", 10, 10, paint);//写文字
}
}

hello.c

#include <jni.h>
#include <stdio.h>
#include <stdlib.h> int getPressure(){
//……监测电压……运算得到压力值
return rand() % ;
} int monitor;
JNIEXPORT void JNICALL Java_com_itheima_monitor_MainActivity_startMonitor
(JNIEnv * env, jobject obj){//obj是MainActivity,
monitor = ;
while(monitor){
//获取锅炉压力
int pressure = getPressure(); //C中通过反射调用java的方法 //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是方法的签名,返回值是void参数是int
//void (*CallVoidMethod)(JNIEnv*, jobject, jmethodID, ...);
(*env)->CallVoidMethod(env, obj, methodId, pressure);//调用java的show方法
sleep();
} } JNIEXPORT void JNICALL Java_com_itheima_monitor_MainActivity_stopMonitor
(JNIEnv * env, jobject obj){
monitor = ;
} //进入工程的bin\classes目录,执行javap -s Mainactivity的全包名类名,就可以查看MainActivity的所有方法的签名。
上一篇:Qt之模型/视图(自定义风格)


下一篇:android 自定义进度条颜色