安卓NDK开发案列二:模仿压力表

前言:今天我们通过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;
}
--------完-----

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

等待着冬天的风

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值