前言:今天我们通过JNI来模仿一个压力表的变化!
-----------分割线--------
效果如图所示:
--------分割线------
MainActivity.java:
package com.fly.demo6;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends AppCompatActivity {
MyView pb;
private JNI jni;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pb = (MyView) findViewById(R.id.pb);
jni = new JNI(pb);
}
public void start(View v) {
new Thread(new Runnable() {
@Override
public void run() {
jni.startMoniter();
}
}).start();
}
public void stop(View v) {
jni.stopMoniter();
}
}
JNI.java:
package com.fly.demo6;
import static com.fly.demo6.R.id.pb;
/**
* Created by Fly on 2017/7/6.
*/
public class JNI {
static {
System.loadLibrary("native-lib");
}
MyView pb;
public JNI(MyView pb) {
this.pb = pb;
}
public native void startMoniter();
public native void stopMoniter();
public void setPressure(int pressure) {
//pb.setProgress(pressure);
pb.setPressure(pressure);
}
}
MyView.java:
package com.fly.demo6;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;
/**
* Created by Fly on 2017/7/16.
*/
public class MyView extends View {
Paint paint;
int pressure = 0;
public MyView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
paint = new Paint();
}
public MyView(Context context, AttributeSet attrs) {
super(context, attrs);
paint = new Paint();
}
public MyView(Context context) {
super(context);
paint = new Paint();
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if (pressure < 40) {
paint.setColor(Color.GREEN);
} else if (pressure < 80) {
paint.setColor(Color.YELLOW);
} else {
paint.setColor(Color.RED);
}
canvas.drawRect(50, 200 - pressure, 100, 200, paint);
canvas.drawText("当前压力值" + pressure, 50, 50, paint);
paint.setTextSize(25);
}
public void setPressure(int pressure) {
this.pressure = pressure;
//invalidate只能在主线程调用 子线程用postInvalidate()
//invalidate();
postInvalidate();
}
}
简单的布局:
<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"
android:orientation="vertical"
tools:context=".MainActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="start"
android:text="开始检测" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="stop"
android:text="停止检测" />
<com.fly.demo6.MyView
android:id="@+id/pb"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
C代码:
//
// Created by Fly on 2017/7/16.
//
#include <jni.h>
#include <stdlib.h>
#include <unistd.h>
int getPressure() {
return rand() % 100;
}
int flag = 0;
/*
* Class: com_fly_demo6_JNI
* Method: startMoniter
* Signature: ()V
*/
JNIEXPORT void JNICALL Java_com_fly_demo6_JNI_startMoniter
(JNIEnv *env, jobject obj) {
flag = 1;
while (flag) {
//①拿到字节码对象
sleep(1);
jclass clazz = (*env)->FindClass(env, "com/fly/demo6/JNI");
jmethodID methodID = (*env)->GetMethodID(env, clazz, "setPressure", "(I)V");
(*env)->CallVoidMethod(env, obj, methodID, getPressure());
}
}
/*
* Class: com_fly_demo6_JNI
* Method: stopMoniter
* Signature: ()V
*/
JNIEXPORT void JNICALL Java_com_fly_demo6_JNI_stopMoniter
(JNIEnv *env, jobject obj) {
flag = 0;
}
--------完-----