SetWallpaper, Translucent和TranslucentBlur

SetWallpaperWallpaperManager是管理wallpaper的主要类,通过它我们可获取当前系统壁纸、设置壁纸等等。

示例中的主要代码:
Java代码
  1. package com.example.android.apis.app;

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

  5. import java.io.IOException;

  6. import android.app.Activity;
  7. import android.app.WallpaperManager;
  8. import android.graphics.Color;
  9. import android.graphics.PorterDuff;
  10. import android.graphics.drawable.Drawable;
  11. import android.os.Bundle;
  12. import android.view.View;
  13. import android.view.WindowManager;
  14. import android.view.View.OnClickListener;
  15. import android.widget.Button;
  16. import android.widget.ImageView;

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

  21.     @Override
  22.     protected void onCreate(Bundle savedInstanceState) {
  23.         super.onCreate(savedInstanceState);
  24.         setContentView(R.layout.wallpaper_2);
  25.         // 1.获取与给定Context关联的WallpaperManager
  26.         final WallpaperManager wallpaperManager = WallpaperManager.getInstance(this);
  27.         // 2.获取当前系统壁纸
  28.         final Drawable wallpaperDrawable = wallpaperManager.getDrawable();
  29.         final ImageView imageView = (ImageView) findViewById(R.id.imageview);
  30.         imageView.setDrawingCacheEnabled(true);
  31.         imageView.setImageDrawable(wallpaperDrawable);

  32.         Button randomize = (Button) findViewById(R.id.randomize);
  33.         randomize.setOnClickListener(new OnClickListener() {
  34.             public void onClick(View view) {
  35.                 int mColor = (int) Math.floor(Math.random() * mColors.length);
  36.                 // 3.对壁纸进行修改
  37.                 wallpaperDrawable.setColorFilter(mColors[mColor], PorterDuff.Mode.MULTIPLY);
  38.                 imageView.setImageDrawable(wallpaperDrawable);
  39.                 imageView.invalidate();
  40.             }
  41.         });

  42.         Button setWallpaper = (Button) findViewById(R.id.setwallpaper);
  43.         setWallpaper.setOnClickListener(new OnClickListener() {
  44.             public void onClick(View view) {
  45.                 try {
  46.                     // 4.设置壁纸
  47.                     wallpaperManager.setBitmap(imageView.getDrawingCache());
  48.                     finish();
  49.                 } catch (IOException e) {
  50.                     e.printStackTrace();
  51.                 }
  52.             }
  53.         });
  54.     }
  55. }
复制代码
Translucent

设置window背景为半透明状态。在AndroidManifest.xml中为window配置半透明背景的theme。theme在xml文件中配置。

1. 通过继承android:style/Theme.Translucent实现。

Xml代码
  1. <style name="Theme.Translucent" parent="android:style/Theme.Translucent">
  2.         <item name="android:windowBackground">@drawable/translucent_background</item>
  3.         <item name="android:windowNoTitle">true</item>
  4.         <item name="android:colorForeground">#fff</item>
  5. </style>
复制代码
2. 通过设置android:windowIsTranslucent为true实现。

Xml代码
  1. <style name="Theme.Translucent">
  2.         <item name="android:windowIsTranslucent">true</item>
  3.         <item name="android:windowBackground">@drawable/translucent_background</item>
  4.         <item name="android:windowNoTitle">true</item>
  5.         <item name="android:colorForeground">#fff</item>
  6. </style>
复制代码
如果同时android:windowBackground为白色时,就是全透明的效果了。

TranslucentBlur

设置window背景为半透明状态并且被掩盖的Activity为模糊状。

主要相关代码:
先设置当前window的背景为半透明状态。然后设置底下的window为模糊状。在setContentView之前调用。
  1. // Have the system blur any windows behind this one.
  2.         getWindow().setFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND,
  3.                 WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
复制代码
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值