簡體   English   中英

如何在后台服務中創建視圖以將畫布另存為圖片?

[英]How to create view in background service to save the canvas as a picture?

這可能是一個奇怪的要求,但是在特定情況下很有用。 例如,當您在Android設備上運行網絡服務並為頁面提供特殊圖紙時。

所有Web服務內容將在后台服務的后台線程中運行,而沒有任何可見的活動寡婦。 如何將一些布局文件充入視圖並在后台線程中更改Java中的內容?

那可能嗎? 怎么樣?

public class MyIntentService extends IntentService {
    @Override
    protected void onHandleIntent(Intent intent) {
        LayoutInflater inflater = LayoutInflater.from(getApplicationContext());
        View view = inflater.inflate(R.layout.view, null);
        Button button = (Button) view.findViewById( R.id.button );

        String text = intent.getExtras().getString("text");
        button.setText(text);

        view.setDrawingCacheEnabled(true);
        Bitmap bitmap = view.getDrawingCache();
    }
}

由於我不確定確切的用例,因此以下代碼僅是概念證明,但應易於適應您的需求。 在IntentService中將示例布局放大,更改各種View屬性,並將位圖保存到外部存儲。

布局文件off_screen.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/linear_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical"
    android:background="#aaddff" >

    <ImageView android:id="@+id/image_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher" />

    <TextView android:id="@+id/text_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#000000"
        android:text="Hello!" />

</LinearLayout>

IntentService類ViewPictureService

public class ViewPictureService extends IntentService
{   
    public static final String EXTRA_WIDTH = "width";
    public static final String EXTRA_HEIGHT = "height";
    public static final String EXTRA_RESOURCE = "resource";
    public static final String EXTRA_FILENAME = "filename";

    public ViewPictureService()
    {
        super("ViewPictureService");
    }

    @Override
    protected void onHandleIntent(Intent intent)
    {
        int resId = intent.getIntExtra(EXTRA_RESOURCE, 0);
        int width = intent.getIntExtra(EXTRA_WIDTH, -1);
        int height = intent.getIntExtra(EXTRA_HEIGHT, -1);
        String filename = intent.getStringExtra(EXTRA_FILENAME);

        saveLayoutBitmap(resId, width, height, filename);
    }

    private void saveLayoutBitmap(int resId, int width, int height, String filename)
    {
        LayoutInflater inflater = LayoutInflater.from(this);
        View view = inflater.inflate(resId, null);

        ((LinearLayout) view).setBackgroundColor(Color.RED);
        ((TextView) view.findViewById(R.id.text_view)).setText("World!");
        ((ImageView) view.findViewById(R.id.image_view)).setImageDrawable(getResources().getDrawable(android.R.drawable.ic_dialog_alert));

        WindowManager wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
        Display display = wm.getDefaultDisplay();

        int w = width < 0 ? display.getWidth() : width;
        int h = height < 0 ? display.getHeight() : height;

        view.measure(MeasureSpec.makeMeasureSpec(w, MeasureSpec.EXACTLY), 
                     MeasureSpec.makeMeasureSpec(h, MeasureSpec.EXACTLY));

        view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
        view.setDrawingCacheEnabled(true);
        view.buildDrawingCache();
        Bitmap bmp = view.getDrawingCache();

        String target = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)
            + File.separator + filename;
        File f = new File(target);

        try
        {
            FileOutputStream fos = new FileOutputStream(f);
            bmp.compress(Bitmap.CompressFormat.PNG, 100, fos);
            fos.flush();
            fos.close();
        }
        catch (FileNotFoundException e)
        {
            e.printStackTrace();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }       
    }
}

以及該服務的示例調用:

Intent intent = new Intent(this, ViewPictureService.class);

intent.putExtra(ViewPictureService.EXTRA_RESOURCE, R.layout.off_screen);
intent.putExtra(ViewPictureService.EXTRA_WIDTH, 540);
intent.putExtra(ViewPictureService.EXTRA_HEIGHT, 960);
intent.putExtra(ViewPictureService.EXTRA_FILENAME, "view_" + System.currentTimeMillis() + ".png");

startService(intent);

當然,文件的寫入將需要清單中的權限:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

服務將在<application>部分中需要一個條目:

<service android:name=".ViewPictureService" />

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM