1package com.example.android.apis.app;
2/*
3 * Copyright (C) 2013 The Android Open Source Project
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 *      http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18import android.app.Activity;
19import android.graphics.Bitmap;
20import android.graphics.drawable.BitmapDrawable;
21import android.os.Bundle;
22import android.print.PrintManager;
23import android.support.v4.print.PrintHelper;
24import android.view.Menu;
25import android.view.MenuItem;
26import android.webkit.WebView;
27import android.widget.ImageView;
28
29import com.example.android.apis.R;
30
31/**
32 * This class demonstrates how to implement bitmap printing.
33 * <p>
34 * This activity shows an image and offers a print option in the overflow
35 * menu. When the user chooses to print a helper class from the support
36 * library is used to print the image.
37 * </p>
38 *
39 * @see PrintManager
40 * @see WebView
41 */
42public class PrintBitmap extends Activity {
43
44    private ImageView mImageView;
45
46    @Override
47    protected void onCreate(Bundle savedInstanceState) {
48        super.onCreate(savedInstanceState);
49        setContentView(R.layout.print_bitmap);
50        mImageView = (ImageView) findViewById(R.id.image);
51    }
52
53    @Override
54    public boolean onCreateOptionsMenu(Menu menu) {
55        super.onCreateOptionsMenu(menu);
56        getMenuInflater().inflate(R.menu.print_custom_content, menu);
57        return true;
58    }
59
60    @Override
61    public boolean onOptionsItemSelected(MenuItem item) {
62        if (item.getItemId() == R.id.menu_print) {
63            print();
64            return true;
65        }
66        return super.onOptionsItemSelected(item);
67    }
68
69    private void print() {
70        // Get the print manager.
71        PrintHelper printHelper = new PrintHelper(this);
72
73        // Set the desired scale mode.
74        printHelper.setScaleMode(PrintHelper.SCALE_MODE_FIT);
75
76        // Get the bitmap for the ImageView's drawable.
77        Bitmap bitmap = ((BitmapDrawable) mImageView.getDrawable()).getBitmap();
78
79        // Print the bitmap.
80        printHelper.printBitmap("Print Bitmap", bitmap);
81    }
82}
83