源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

Android编程之手机壁纸WallPaper设置方法示例

  • 时间:2020-08-08 12:09 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:Android编程之手机壁纸WallPaper设置方法示例
本文实例讲述了Android编程之手机壁纸WallPaper设置方法。分享给大家供大家参考,具体如下:
/**
 * Andorid设置手机屏幕的壁纸
 *
 * @description:
 * @author ldm
 * @date 2016-5-4 下午3:08:56
 */
public class SetWallpaperActivity extends Activity {
  // WallpaperManager类:系统壁纸管理。通过它可以获得当前壁纸以及设置指定图片作为系统壁纸。
  private WallpaperManager wallpaperManager;
  // 壁纸对应的Drawable
  private Drawable wallpaperDrawable;
  // 展示样式的ImageView
  private ImageView imageView;
  // 随机生成图片的颜色 Button
  private Button randomize;
  // 设置壁纸
  private Button setWallpaper;
  // 暂定的一些颜色值
  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.set_wallpaper);
    // 初始化WallpaperManager
    wallpaperManager = WallpaperManager.getInstance(this);
    wallpaperDrawable = wallpaperManager.getDrawable();// 获得当前系统的壁纸
    initViews();
    initListeners();
  }
  private void initListeners() {
    randomize.setOnClickListener(new OnClickListener() {
      public void onClick(View view) {
        int mColor = (int) Math.floor(Math.random() * mColors.length);
        // 给当前系统壁纸设置颜色
        wallpaperDrawable.setColorFilter(mColors[mColor],
            PorterDuff.Mode.MULTIPLY);// 取两层绘制交集
        imageView.setImageDrawable(wallpaperDrawable);
        // imageView.invalidate();
      }
    });
    setWallpaper.setOnClickListener(new OnClickListener() {
      public void onClick(View view) {
        try {
          // 设置壁纸
          wallpaperManager.setBitmap(imageView.getDrawingCache());
          finish();
        } catch (IOException e) {
          e.printStackTrace();
        }
      }
    });
  }
  private void initViews() {
    imageView = (ImageView) findViewById(R.id.imageview);
    imageView.setDrawingCacheEnabled(true);
    imageView.setImageDrawable(wallpaperDrawable);
    randomize = (Button) findViewById(R.id.randomize);
    setWallpaper = (Button) findViewById(R.id.setwallpaper);
  }
}

布局文件:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content">
  <ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/imageview" />
  <LinearLayout
    android:orientation="horizontal"
    android:layout_width="wrap_content"
    android:layout_height="match_parent">
    <Button
      android:id="@+id/randomize"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="@string/randomize"
      android:layout_gravity="bottom" />
    <Button
      android:id="@+id/setwallpaper"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="@string/set_wallpaper"
      android:layout_gravity="bottom" />
  </LinearLayout>
</FrameLayout>

更多关于Android相关内容感兴趣的读者可查看本站专题:《[url=http://www.1sucai.cn/Special/214.htm]Android图形与图像处理技巧总结[/url]》、《[url=http://www.1sucai.cn/Special/410.htm]Android开发入门与进阶教程[/url]》、《[url=http://www.1sucai.cn/Special/508.htm]Android调试技巧与常见问题解决方法汇总[/url]》、《[url=http://www.1sucai.cn/Special/381.htm]Android基本组件用法总结[/url]》、《[url=http://www.1sucai.cn/Special/375.htm]Android视图View技巧总结[/url]》、《[url=http://www.1sucai.cn/Special/371.htm]Android布局layout技巧总结[/url]》及《[url=http://www.1sucai.cn/Special/124.htm]Android控件用法总结[/url]》 希望本文所述对大家Android程序设计有所帮助。
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部