public
class
CreateFormatActivity
extends
ActivityHelper
implements
View.OnTouchListener {
private
final
double
A4_RATIO =
1
.4151260504f;
private
final
double
SCREEN_RATIO =
0
.65f;
private
LinearLayout preView =
null
;
private
String pdfPath =
""
;
@Override
protected
void
activityStart(Bundle savedInstanceState)
throws
Exception {
setContentView(R.layout.activity_format_preview);
findViewById(R.id.closeBtn).setOnTouchListener(
this
);
findViewById(R.id.printBtn).setOnTouchListener(
this
);
preView = (LinearLayout) findViewById(R.id.previewList);
createFormatAndPreview();
}
@Override
public
boolean
onTouch(View view, MotionEvent motionEvent) {
if
(motionEvent.getAction() == MotionEvent.ACTION_UP) {
switch
(view.getId()) {
case
R.id.closeBtn :
finish();
break
;
case
R.id.printBtn :
try
{
File pdfFile =
new
File(pdfPath);
Intent intent =
new
Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(pdfFile),
"application/pdf"
);
startActivity(intent);
}
catch
(Exception e) {
}
break
;
default
:
;
}
}
return
false
;
}
private
void
createFormatAndPreview() {
WindowManager manager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Display display = manager.getDefaultDisplay();
Point theScreenResolution =
new
Point();
display.getSize(theScreenResolution);
int
basePreviewWidth =
1280
;
int
basePreviewHeight = (
int
) (basePreviewWidth * A4_RATIO);
float
sRatio = basePreviewWidth / A4.PORTRAIT[
0
];
LogUtil.d(String.format(
"basePreviewWidth : %s - basePreviewHeight : %s"
, basePreviewWidth, basePreviewHeight));
LogUtil.d(
"sRatio : "
+ sRatio);
Bitmap draw_bitmap = Bitmap.createBitmap(basePreviewWidth, basePreviewHeight, Bitmap.Config.ARGB_8888);
Canvas canvas =
new
Canvas(draw_bitmap);
canvas.drawColor(Color.WHITE);
Paint paintFillBlack =
new
Paint();
paintFillBlack.setTextSize(20f * sRatio);
paintFillBlack.setColor(Color.BLACK);
paintFillBlack.setStyle(Paint.Style.FILL_AND_STROKE);
paintFillBlack.setAntiAlias(
true
);
Paint paintStrokeBlack =
new
Paint();
paintStrokeBlack.setTextSize(20f * sRatio);
paintStrokeBlack.setColor(Color.BLACK);
paintStrokeBlack.setStyle(Paint.Style.STROKE);
ArrayList<Map<String, String>> rows =
new
ArrayList<Map<String, String>>();
int
pageNumber =
1
;
int
rowIndex =
0
;
String filePath = globals.getLocalPdfWorkDirPath();
String fileFullPath = filePath +
"DtcPrintPreview"
;
while
(
true
) {
try
{
FileOutputStream out =
new
FileOutputStream(fileFullPath + pageNumber +
".jpg"
);
draw_bitmap.compress(Bitmap.CompressFormat.JPEG,
100
, out);
int
preViewWidth = theScreenResolution.x /
2
;
int
preViewHeight = (
int
) (preViewWidth * A4_RATIO);
ImageView imageView =
new
ImageView(context);
imageView.setImageBitmap(orgImage);
imageView.setPadding(
10
,
10
,
10
,
10
);
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
imageView.setLayoutParams(
new
LinearLayout.LayoutParams(preViewWidth, preViewHeight));
preView.addView(imageView);
}
catch
(Exception e) {
e.printStackTrace();
}
if
(rowIndex == rows.size()) {
break
;
}
else
{
++pageNumber;
draw_bitmap = Bitmap.createBitmap(basePreviewWidth, basePreviewHeight, Bitmap.Config.ARGB_8888);
canvas =
new
Canvas(draw_bitmap);
canvas.drawColor(Color.WHITE);
}
}
PdfUtil pdfUtil =
new
PdfUtil(PrintFromDiagActivity.
this
);
pdfPath = pdfUtil.createImageToPDF(filePath,
"PORTRAIT"
,
1
);
}
}