代码如下,先获取一个打印管理器,再取得打印队列第一项,将内容输出,为什么运行不起来
或者有编写插件提取安卓打印文件的方法也可以
代码如下,先获取一个打印管理器,再取得打印队列第一项,将内容输出,为什么运行不起来
或者有编写插件提取安卓打印文件的方法也可以
public class PrintUtil {
private static final String TAG = "PrintUtil";
public String SCREEN_SHOTS_LOCATION = Environment
.getExternalStorageDirectory().getPath();
/**
@throws Exception
*/
public void takeScreenShot(View view, String name,TakeScreenShotCallBack callback) throws Exception{
takeScreenShot(view,name,SCREEN_SHOTS_LOCATION,callback);
}
TakeScreenShotCallBack mCallback = new TakeScreenShotCallBack(){
@Override
public void excute(File file) {
// TODO Auto-generated method stub
}
};
/**
把传入的view保存为图片
@param parent 事件触发视图,可以是button等
@param view 保存为图片的视图
@param isShow 是否显示展示视图
@return
*/
public void shotViewAsImage(View parent,View view, boolean isShow){
PopupWindow popupWindow = getPopuptWindow(view);
if(isShow)
popupWindow.showAtLocation(parent, Gravity.CENTER, 0, 0);
File newFile = null;
try {
takeScreenShot(popupWindow.getContentView(),null,mCallback);
} catch (Exception e) {
Log.e(TAG, e.getMessage());
}
}
/**
创建popupWindow
@param popupWindow_view
@return
*/
protected PopupWindow getPopuptWindow(View popupWindow_view) {
PopupWindow popupWindow = null;
popupWindow = new PopupWindow(popupWindow_view, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, true);// 创建PopupWindow实例
popupWindow.setBackgroundDrawable(new BitmapDrawable());
popupWindow.setFocusable(true);
popupWindow.setOutsideTouchable(false);
return popupWindow;
}
public interface TakeScreenShotCallBack{
public void excute(File file);
}
/**
对当前view进行截屏,并保存到path指定的目录下
@param view
@param name
@param path
@return
@throws Exception
*/
public void takeScreenShot(final View view, final String name,final String path,final TakeScreenShotCallBack callback) throws Exception {
final Bitmap bitmap = convertViewToBitmap(view);
Runnable runnable = new Runnable() {
@Override
public void run() {
Canvas canvas = new Canvas(bitmap);
int w = bitmap.getWidth();
int h = bitmap.getHeight();
Paint paint = new Paint();
paint.setColor(Color.YELLOW);
String n = null;
if(null == name || "".equals(name)){
SimpleDateFormat simple = new SimpleDateFormat("yyyyMMddhhmmss");
n = simple.format(new Date());
}else{
n = name;
}
canvas.drawText("", w - w / 2, h - h / 10, paint);
canvas.save();
canvas.restore();
FileOutputStream fos = null;
File file = null;
try {
File sddir = new File(path);
if (!sddir.exists()) {
sddir.mkdirs();
}
//生成以时间为名称的文件
file = new File(path + File.separator
}
/**
转换view为bitmap
@param view
@return
*/
public Bitmap convertViewToBitmap(View view){
view.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
view.buildDrawingCache();
Bitmap bitmap = view.getDrawingCache();
return bitmap;
}
private BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
}
调用函数
public void print(String data) {
View view = LayoutInflater.from(this).inflate(R.layout.print_test,
null, false);
//这几句可以注释掉,是另一个条形码生成的方法
/* ImageView bar_img = (ImageView) view
.findViewById(R.id.print_img_barcode);
try {
bar_img.setImageBitmap(testCODE39(data));
} catch (Exception e) {
} */
PrintUtil printer = new PrintUtil();
printer.shotViewAsImage(btn_print, view, true);
}