本菜鸟最近在学习android jni skia画图,从网上找的例子,运行后模拟器上没有显示,不知道该怎么做。下面是我的代码:
java部分:
public class MainActivity extends Activity {
private native void code1(); //jni
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
code1();
}
}
c++部分:
JNIEXPORT void JNICALL Java_com_pic_MainActivity_code1(JNIEnv* env,jobject obj)
{
LOGI("in good");
LOGI("in callcode1");
SkBitmap bitmap;
bitmap.setConfig(SkBitmap::kARGB_8888_Config,200,200);
bitmap.allocPixels();
SkCanvas canvas(new SkDevice(bitmap));
SkPaint paint;
LOGI("in callcode2");
// draw points with red.
paint.setARGB(255, 255, 0, 0);
// paint.setStrokeWidth(4);
LOGI("in callcode3");
canvas.drawPoint(40,30, paint);
canvas.drawPoint(80,60, paint);
canvas.drawPoint(120,90, paint);
LOGI("in callcode4");
SkRect r;
paint.setARGB(255, 255, 0, 0);
r.set(25, 25, 145, 145);
canvas.drawRect(r, paint);
//draw a line with green.
paint.setARGB(255, 0, 255, 0);
paint.setStrokeWidth(4);
canvas.drawLine(160,10,320,110,paint);
//draw a circle with bule.
paint.setARGB(255, 0, 0, 255);
canvas.drawCircle(80,180,50,paint);
// canvas.setBitmapDevice(SkBitmap());
LOGI("in callcode5");
SkImageEncoder::EncodeFile("snapshot.png", bitmap,SkImageEncoder::kPNG_Type,100);
LOGI("out callcode1");
}
如果上面的代码是错误的,请给出一个完整的代码!!!
2012-11-09 14:01:39 whosyour_dady 楼主
1个回答
从java中传一个Bitmap到jni。
DisplayMetrics metrics = new DisplayMetrics(); //获取分辨率大小
this.getWindowManager().getDefaultDisplay().getMetrics(metrics);
int width = metrics.widthPixels;
int height = metrics.heightPixels;
Bitmap dst=Bitmap.createBitmap(width, height, Config.ARGB_8888);//设置Bitmap的大小和颜色格式
test(dst); //把画布传过去
ImageView iv = new ImageView(this);
iv.setImageBitmap(dst);
setContentView(iv);
jni的接口中加上 AndroidBitmapInfo info;
int* pixels;
int ret;
ret = AndroidBitmap_getInfo(env, bitmap, &info);
ret = AndroidBitmap_lockPixels(env, bitmap, (void**)&pixels);
int width=(int)info.width;
int height=(int)info.height;
SkBitmap bmp = SkBitmap();
bmp.setConfig(SkBitmap::kARGB_8888_Config,width,height,0);
bmp.setPixels((void*)pixels);
。。。。画图。。。
AndroidBitmap_unlockPixels(env, bitmap);
ok!!!
2012-11-12 11:11:36 goog_guzl 1楼