针对android游戏截图分享

阅读更多

 

不少android游戏使用摩讯截图都截出一块黑的图片。

下面解决截图黑屏的问题:

情况一:

图通过Canvas 画在Bitmap 上,再把bitmap放入ImageView中。(这种做游戏的方式很少)

代码如下:

 

 写道
package com.czq.test;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Path.Direction;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.Display;
import android.view.MotionEvent;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;

import com.mobcent.snapshot.share.android.helper.MCShareSnapshotHelper;

public class HelloGraphicsActivity extends Activity {
ImageView img;
Canvas canvas;
Bitmap bitmap;
Paint paint;

int width;
int height;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 设置全屏,也可以在xml中进行设置 。
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.graphics_activity);
img = (ImageView) findViewById(R.id.img);
// 得到屏幕的宽和高。
Display display = getWindowManager().getDefaultDisplay();
width = display.getWidth();
height = display.getHeight();
// 创建图片,用来绘制。
bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
// 创建画布,并且设置图片用来进行绘制。一个画布可以看成是整个屏幕。
canvas = new Canvas();
canvas.setBitmap(bitmap);
// 设置画笔,的各种属性。
// 可以利用typeface设置外部字体
paint = new Paint();
paint.setAntiAlias(true);
paint.setColor(Color.RED);
paint.setStrokeWidth(10);
paint.setStyle(Paint.Style.FILL);
// 利用画布直接将内容绘制到bitmap上面。
canvas.drawCircle(50, 50, 30, paint);
// 设定字体和各种格式
Typeface typeface = Typeface.create("test", Typeface.BOLD_ITALIC);
paint.setTypeface(typeface);
paint.setTextSize(25);
canvas.drawText("我是被画出来的!", 100, 50, paint);

// 画线
paint.setColor(Color.GREEN);
canvas.drawLine(0, 0, width, height / 2, paint);

// 画一条路径;Path.moveTo
Path path = new Path();
paint.setColor(Color.BLUE);
// 移动动位置
path.moveTo(150, 150);
// 画线条
path.lineTo(150, 200);
path.lineTo(200, 210);
path.lineTo(30, 30);
canvas.drawPath(path, paint);

// 从哪里开始画一个弧形
path.reset();
path.addCircle(230, 230, 10, Direction.CCW);
canvas.drawPath(path, paint);

// 将画好的图片显示到屏幕上面。
img.setImageBitmap(bitmap);
// 清空笔刷;
paint.reset();

}

@Override
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
//截图
MCShareSnapshotHelper.takeSnapshot(HelloGraphicsActivity.this, "", "");

} else if (event.getAction() == MotionEvent.ACTION_UP) {
} else if (event.getAction() == MotionEvent.ACTION_MOVE) {
}
return true;
}
}

 情况二:

 

继承View 把图画在View上(这种情况也用的不多)

 

代码如下:

 

 写道
package com.czq.test;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Path.Direction;
import android.graphics.Typeface;
import android.view.View;

public class MyView extends View {
Canvas bitcanvas;
Bitmap bitmap;
Paint paint;

int width;
int height;

public MyView(Context context) {
super(context);
}

@Override
public void draw(Canvas canvas) {
super.draw(canvas);

// 创建图片,用来绘制。
width = 320;
height = 480;
bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
// 创建画布,并且设置图片用来进行绘制。一个画布可以看成是整个屏幕。
bitcanvas = new Canvas();

// 在画布上 设置Bitmap
bitcanvas.setBitmap(bitmap);

// 设置画笔,的各种属性。
// 可以利用typeface设置外部字体
paint = new Paint();
paint.setAntiAlias(true);
paint.setColor(Color.RED);
paint.setStrokeWidth(10);
paint.setStyle(Paint.Style.FILL);
// 利用画布直接将内容绘制到bitmap上面。
bitcanvas.drawCircle(50, 50, 30, paint);
// 设定字体和各种格式
Typeface typeface = Typeface.create("test", Typeface.BOLD_ITALIC);
paint.setTypeface(typeface);
paint.setTextSize(25);
bitcanvas.drawText("我是被画出来的!", 100, 50, paint);

// 画线
paint.setColor(Color.GREEN);
bitcanvas.drawLine(0, 0, width, height / 2, paint);

// 画一条路径;Path.moveTo
Path path = new Path();
paint.setColor(Color.BLUE);
// 移动动位置
path.moveTo(150, 150);
// 画线条
path.lineTo(150, 200);
path.lineTo(200, 210);
path.lineTo(30, 30);
bitcanvas.drawPath(path, paint);

// 从哪里开始画一个弧形
path.reset();
path.addCircle(230, 230, 10, Direction.CCW);
bitcanvas.drawPath(path, paint);
// 将画好的图片显示到屏幕上面。
// 清空笔刷;

canvas.drawBitmap(bitmap, 0, 0, paint);
paint.reset();
}

@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
/*
* width = 320; height = 480; bitmap = Bitmap.createBitmap(width,
* height, Bitmap.Config.ARGB_8888); canvas.setBitmap(bitmap);
*/
// canvas.drawBitmap(bitmap, 0, 0, paint);
}

}
package com.czq.test;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.Window;
import android.view.WindowManager;

import com.mobcent.snapshot.share.android.helper.MCShareSnapshotHelper;

public class ViewActivity extends Activity{
Canvas canvas;
Bitmap bitmap;
Paint paint;

int width;
int height;


@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 设置全屏,也可以在xml中进行设置 。
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(new MyView(this));


}

@Override
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
//截图
MCShareSnapshotHelper.takeSnapshot(ViewActivity.this, "", "");

} else if (event.getAction() == MotionEvent.ACTION_UP) {
} else if (event.getAction() == MotionEvent.ACTION_MOVE) {
}
return true;
}
}
  

 

情况三: 把图画在自定义的SurfaceView 上(这种情况比较常见,目前大多数android游戏都是这样做)

代码如下:

 写道
package com.czq.test;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.view.SurfaceView;
import android.view.animation.Animation;

public class MySurfaceView extends SurfaceView implements Callback, Runnable {// 备注1
private SurfaceHolder sfh;
private Thread th;
private Canvas canvas;
private Paint paint;
private int ScreenW, ScreenH;
private Bitmap mbmpTest;

public MySurfaceView(Context context) {
super(context);
th = new Thread(this);
sfh = this.getHolder();
sfh.addCallback(this); // 备注1
paint = new Paint();
paint.setAntiAlias(true);
paint.setColor(Color.RED);
this.setKeepScreenOn(true);// 保持屏幕常亮

}

@Override
public void startAnimation(Animation animation) {
super.startAnimation(animation);
}

public void surfaceCreated(SurfaceHolder holder) {
ScreenW = this.getWidth();// 备注2
ScreenH = this.getHeight();
// 创建 画布上的bitmap
mbmpTest = Bitmap.createBitmap(ScreenW, ScreenH, Config.RGB_565);

th.start();
}

private void draw() {
try {
Canvas bitCanvas = new Canvas(mbmpTest); //先把图画在bitmap上

bitCanvas.drawColor(Color.WHITE);
bitCanvas.drawText("hello", 100, 100, paint);
bitCanvas.drawText("这就是简单的一个游戏框架", 100, 130, paint);

canvas = sfh.lockCanvas();
canvas.drawBitmap(mbmpTest, 0, 0, paint); // 再把bitmap 画在当前view的canvas上
} catch (Exception ex) {
} finally { // 备注3
if (canvas != null)
sfh.unlockCanvasAndPost(canvas); // 将画好的画布提交
}
}

public void run() {
while (true) {
draw();
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}

public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub
}

public Bitmap getMbmpTest() {
return mbmpTest;
}

public void setMbmpTest(Bitmap mbmpTest) {
this.mbmpTest = mbmpTest;
}

}

 截图方法:

 写道
import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.Window;
import android.view.WindowManager;

import com.mobcent.snapshot.share.android.helper.MCShareSnapshotHelper;

public class SurfaceActivity extends Activity {
/** Called when the activity is first created. */
private MySurfaceView view;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
view=new MySurfaceView(this);
setContentView(view);
}

@Override
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
MCShareSnapshotHelper.takeSnapshotWithBitmap(SurfaceActivity.this, view.getMbmpTest(), "", "");
} else if (event.getAction() == MotionEvent.ACTION_UP) {
} else if (event.getAction() == MotionEvent.ACTION_MOVE) {
}
return true;
}
}
 

 


  • 摩讯游戏截图工程源码.rar (1.9 MB)
  • 下载次数: 65

你可能感兴趣的:(android,游戏,摩讯,截图)