天天看點

API Demos 2.2 研讀筆記(11)——SetWallpaper, Translucent和TranslucentBlur

SetWallpaper

WallpaperManager是管理wallpaper的主要類,通過它我們可擷取目前系統桌面、設定桌面等等。

示例中的主要代碼:

package com.example.android.apis.app;

// Need the following import to get access to the app resources, since this
// class is in a sub-package.
import com.example.android.apis.R;

import java.io.IOException;

import android.app.Activity;
import android.app.WallpaperManager;
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;

public class SetWallpaperActivity extends Activity {
    final static private int[] mColors =
            {Color.BLUE, Color.GREEN, Color.RED, Color.LTGRAY, Color.MAGENTA, Color.CYAN,
                    Color.YELLOW, Color.WHITE};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.wallpaper_2);
        // 1.擷取與給定Context關聯的WallpaperManager
        final WallpaperManager wallpaperManager = WallpaperManager.getInstance(this);
        // 2.擷取目前系統桌面
        final Drawable wallpaperDrawable = wallpaperManager.getDrawable();
        final ImageView imageView = (ImageView) findViewById(R.id.imageview);
        imageView.setDrawingCacheEnabled(true);
        imageView.setImageDrawable(wallpaperDrawable);

        Button randomize = (Button) findViewById(R.id.randomize);
        randomize.setOnClickListener(new OnClickListener() {
            public void onClick(View view) {
           
<style name="Theme.Translucent">
<item name="android:windowIsTranslucent">true</item>
        <item name="android:windowBackground">@drawable/translucent_background</item>
        <item name="android:windowNoTitle">true</item>
        <item name="android:colorForeground">#fff</item>
    </style>      

  int mColor = (int) Math.floor(Math.random() * mColors.length); // 3.對桌面進行修改 wallpaperDrawable.setColorFilter(mColors[mColor], PorterDuff.Mode.MULTIPLY); imageView.setImageDrawable(wallpaperDrawable); imageView.invalidate(); } }); Button setWallpaper = (Button) findViewById(R.id.setwallpaper); setWallpaper.setOnClickListener(new OnClickListener() { public void onClick(View view) { try { // 4.設定桌面 wallpaperManager.setBitmap(imageView.getDrawingCache()); finish(); } catch (IOException e) { e.printStackTrace(); } } }); } }

Translucent

設定window背景為半透明狀态。在AndroidManifest.xml中為window配置半透明背景的theme。theme在xml檔案中配置。

1. 通過繼承android:style/Theme.Translucent實作。

<style name="Theme.Translucent" parent="android:style/Theme.Translucent">
        <item name="android:windowBackground">@drawable/translucent_background</item>
        <item name="android:windowNoTitle">true</item>
        <item name="android:colorForeground">#fff</item>
</style>      

2. 通過設定android:windowIsTranslucent為true實作。

<style name="Theme.Translucent">
        <item name="android:windowIsTranslucent">true</item>
        <item name="android:windowBackground">@drawable/translucent_background</item>
        <item name="android:windowNoTitle">true</item>
        <item name="android:colorForeground">#fff</item>
</style>      

如果同時android:windowBackground為白色時,就是全透明的效果了。

TranslucentBlur

設定window背景為半透明狀态并且被掩蓋的Activity為模糊狀。

主要相關代碼:

先設定目前window的背景為半透明狀态。然後設定底下的window為模糊狀。在setContentView之前調用。

// Have the system blur any windows behind this one.
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND,
                WindowManager.LayoutParams.FLAG_BLUR_BEHIND);