首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 其他教程 > 操作系统 >

API Demos 2.2 研习笔记(11)——SetWallpaper, Translucent和TranslucentBlur

2012-09-01 
API Demos 2.2 研读笔记(11)——SetWallpaper, Translucent和TranslucentBlurSetWallpaper?WallpaperManager

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);

热点排行