安卓中改变手机壁纸

以你之姓@ 2022-09-19 15:26 365阅读 0赞
  1. 布局文件如下:
  2. XML文件如下所示:
  3. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. android:background="#000000"
  8. android:orientation="vertical" >
  9. <Button
  10. android:id="@+id/btn_reset"
  11. android:layout_width="match_parent"
  12. android:layout_height="wrap_content"
  13. android:text="@string/returns" />
  14. <ImageView
  15. android:id="@+id/iv_current"
  16. android:layout_width="80dp"
  17. android:layout_height="130dp"
  18. android:layout_gravity="center_horizontal"
  19. android:contentDescription="@string/action_settings" />
  20. <Button
  21. android:id="@+id/btn_current"
  22. android:layout_width="match_parent"
  23. android:layout_height="wrap_content"
  24. android:text="@string/getcurrent" />
  25. <Gallery
  26. android:id="@+id/gallery"
  27. android:layout_width="match_parent"
  28. android:layout_height="150dp"
  29. android:layout_gravity="center"
  30. />
  31. <Button
  32. android:id="@+id/btn_setwall"
  33. android:layout_width="match_parent"
  34. android:layout_height="wrap_content"
  35. android:text="@string/setcurretn" />
  36. </LinearLayout>

安卓手机壁纸的改变

<1>要实现这一功能需要添加一个权限

  1. 代码实现如下所示:
  2. 一、需要创建一个适配器
  3. package com.brucecheng.waller;
  4. import android.content.Context;
  5. import android.view.View;
  6. import android.view.ViewGroup;
  7. import android.widget.BaseAdapter;
  8. import android.widget.Gallery;
  9. import android.widget.ImageView;
  10. @SuppressWarnings("deprecation")
  11. public class WallPaper extends BaseAdapter {
  12. //创建资源数组
  13. private int[] imageIds;
  14. private Context context;
  15. //重写构造方法
  16. public WallPaper(Context context,int[] imageIds){
  17. this.imageIds=imageIds;
  18. this.context=context;
  19. }
  20. @Override
  21. public int getCount() {
  22. return imageIds.length;
  23. }
  24. @Override
  25. public Object getItem(int position) {
  26. return imageIds[position];
  27. }
  28. @Override
  29. public long getItemId(int position) {
  30. return position;
  31. }
  32. @Override
  33. public View getView(int position, View convertView, ViewGroup parent) {
  34. //创建一个ImageView用于显示壁纸图片
  35. ImageView iv=new ImageView(context);
  36. //设置参数
  37. iv.setScaleType(ImageView.ScaleType.FIT_CENTER);
  38. iv.setLayoutParams(new Gallery.LayoutParams(120, 120));
  39. iv.setImageResource(imageIds[position]);
  40. return iv;
  41. }
  42. 二、Activity的代码实现如下所示:
  43. package com.brucecheng.waller;
  44. import java.io.IOException;
  45. import java.io.InputStream;
  46. import android.app.Activity;
  47. import android.os.Bundle;
  48. import android.view.View;
  49. import android.view.View.OnClickListener;
  50. import android.widget.AdapterView;
  51. import android.widget.AdapterView.OnItemSelectedListener;
  52. import android.widget.Button;
  53. import android.widget.Gallery;
  54. import android.widget.ImageView;
  55. @SuppressWarnings("deprecation")
  56. public class WallerActivity extends Activity {
  57. //创建资源数组
  58. private int[] imageIds={R.drawable.bing11,
  59. R.drawable.bing12,
  60. R.drawable.bing5,
  61. R.drawable.bing6,
  62. R.drawable.bing7,
  63. R.drawable.album,
  64. };
  65. //创建一个整型值表示当前的壁纸
  66. int currentIndex=-1;
  67. //寻找控件
  68. Gallery gallery;
  69. Button btn_reset,btn_current,btn_setwall;
  70. ImageView iv_current;
  71. @Override
  72. protected void onCreate(Bundle savedInstanceState) {
  73. super.onCreate(savedInstanceState);
  74. setContentView(R.layout.activity_main);
  75. //调用方法实例化控件
  76. findViewById();
  77. //为gallery绑定适配器
  78. gallery.setAdapter(new WallPaper(this, imageIds));
  79. //设置监听事件
  80. gallery.setOnItemSelectedListener(new OnItemSelectedListener() {
  81. @Override
  82. public void onItemSelected(AdapterView<?> parent, View view,
  83. int position, long id) {
  84. //记录被选中的图片索引
  85. currentIndex=position;
  86. }
  87. @Override
  88. public void onNothingSelected(AdapterView<?> parent) {
  89. }
  90. });
  91. //为其他的控件设置监听事件
  92. btn_current.setOnClickListener(listener);
  93. btn_setwall.setOnClickListener(listener);
  94. btn_reset.setOnClickListener(listener);
  95. }
  96. //重写监听事件
  97. OnClickListener listener=new OnClickListener() {
  98. @Override
  99. public void onClick(View v) {
  100. switch (v.getId()) {
  101. //获取当前壁纸
  102. case R.id.btn_current:
  103. iv_current.setBackgroundDrawable(getWallpaper());
  104. break;
  105. //设置壁纸
  106. case R.id.btn_setwall:
  107. InputStream in=WallerActivity.this.getResources().openRawResource(imageIds[currentIndex]);
  108. //设为当前壁纸
  109. try {
  110. setWallpaper(in);
  111. } catch (IOException e1) {
  112. e1.printStackTrace();
  113. }
  114. break;
  115. //恢复默认壁纸
  116. case R.id.btn_reset:
  117. try {
  118. WallerActivity.this.clearWallpaper();
  119. } catch (Exception e) {
  120. e.printStackTrace();
  121. }
  122. break;
  123. }
  124. }
  125. };
  126. //自定义方法实例化控件
  127. public void findViewById(){
  128. btn_current=(Button) findViewById(R.id.btn_current);
  129. btn_reset=(Button) findViewById(R.id.btn_reset);
  130. btn_setwall=(Button) findViewById(R.id.btn_setwall);
  131. iv_current=(ImageView) findViewById(R.id.iv_current);
  132. gallery=(Gallery) findViewById(R.id.gallery);
  133. }
  134. }

发表评论

表情:
评论列表 (有 0 条评论,365人围观)

还没有评论,来说两句吧...

相关阅读

    相关 手机刷机教程

    以魅蓝note为例 1:根据手机的品牌去手机官网下载新的系统包![Center][] 我这里选择的是移动版的,下下来的结果是update.zip。不用解压它。 ![Cen

    相关 手机图标尺寸

      应用程序图标 (Icon)应当是一个 Alpha 通道透明的32位 PNG 图片。由于安卓设备众多,一个应用程序图标需要设计几种不同大小,如: LDPI (Low

    相关 adb控制手机

           最近做项目发现同事做的Api能够让电脑通过USB控制手机进行操作,原来用的是谷歌开发的adb。简单说一下如何控制手机拍照吧,其他的以后接触到了再补上。