国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > Android > 正文

Android實(shí)現(xiàn)手機(jī)壁紙改變的方法

2020-04-11 11:25:14
字體:
供稿:網(wǎng)友

本文實(shí)例講述了Android實(shí)現(xiàn)手機(jī)壁紙改變的方法。分享給大家供大家參考。具體如下:

main.xml布局文件:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  android:orientation="vertical" android:layout_width="fill_parent"  android:layout_height="fill_parent">  <Button android:id="@+id/clearWall"    android:layout_width="fill_parent"    android:layout_height="wrap_content"     android:text="恢復(fù)默認(rèn)墻紙" />  <ImageView android:id="@+id/currWall"     android:layout_width="100px"    android:layout_height="150px"    android:layout_gravity="center_horizontal" />  <Button android:id="@+id/getWall"     android:layout_width="fill_parent"    android:layout_height="wrap_content"     android:text="獲取當(dāng)前墻紙" />  <Gallery android:id="@+id/gallery"    android:layout_width="fill_parent"    android:layout_height="wrap_content" />  <Button android:id="@+id/setWall"     android:layout_width="fill_parent"    android:layout_height="wrap_content"     android:text="設(shè)置為當(dāng)前墻紙" /></LinearLayout>

清單文件:

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"   package="com.ljq.activity"   android:versionCode="1"   android:versionName="1.0">  <application android:icon="@drawable/icon" android:label="@string/app_name">    <activity android:name=".WallActivity"         android:label="@string/app_name">      <intent-filter>        <action android:name="android.intent.action.MAIN" />        <category android:name="android.intent.category.LAUNCHER" />      </intent-filter>    </activity>  </application>  <uses-sdk android:minSdkVersion="7" />  <!-- 設(shè)置手機(jī)墻紙權(quán)限 -->  <uses-permission android:name="android.permission.SET_WALLPAPER" /></manifest>

WallAdapter自定義適配器:

package com.ljq.activity;import android.content.Context;import android.view.View;import android.view.ViewGroup;import android.widget.BaseAdapter;import android.widget.Gallery;import android.widget.ImageView;public class WallAdapter extends BaseAdapter {  private int[] imgIds = null;  private Context context = null;  public WallAdapter(int[] imgIds, Context context) {    super();    this.imgIds = imgIds;    this.context = context;  }  public int getCount() {    return imgIds.length;  }  public Object getItem(int position) {    //return imgIds[position];    return imgIds[position%imgIds.length];//可循環(huán)  }  public long getItemId(int position) {    return position;  }  public View getView(int position, View convertView, ViewGroup parent) {    ImageView imageView = new ImageView(context);    imageView.setBackgroundResource(imgIds[position]);// 設(shè)置ImageView的背景圖片    imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);    imageView.setLayoutParams(new Gallery.LayoutParams(120, 120));    return imageView;  }}

WallActivity類:

package com.ljq.activity;import java.io.IOException;import java.io.InputStream;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.widget.AdapterView;import android.widget.Button;import android.widget.Gallery;import android.widget.ImageView;import android.widget.AdapterView.OnItemSelectedListener;public class WallActivity extends Activity {  private int[] imgIds={R.drawable.w1, R.drawable.w2, R.drawable.w3, R.drawable.w4};  private int selectIndex=-1;//被選中的圖片在id數(shù)組中的索引  private ImageView currWall=null;  private Gallery gallery=null;  private Button clearWall=null;  private Button getWall=null;  private Button setWall=null;  @Override  public void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.main);    gallery=(Gallery)findViewById(R.id.gallery);    gallery.setAdapter(new WallAdapter(imgIds, WallActivity.this));    gallery.setSpacing(5);    gallery.setOnItemSelectedListener(new OnItemSelectedListener(){      public void onItemSelected(AdapterView<?> parent, View view,          int position, long id) {        selectIndex = position;//記錄被選中的圖片索引      }      public void onNothingSelected(AdapterView<?> parent) {      }    });    currWall=(ImageView)findViewById(R.id.currWall);    clearWall=(Button)findViewById(R.id.clearWall);    getWall=(Button)findViewById(R.id.getWall);    setWall=(Button)findViewById(R.id.setWall);    clearWall.setOnClickListener(listener);    getWall.setOnClickListener(listener);    setWall.setOnClickListener(listener);  }  View.OnClickListener listener=new View.OnClickListener(){    public void onClick(View v) {      Button btn=(Button)v;      switch (btn.getId()) {      case R.id.clearWall://還原手機(jī)壁紙        try {          WallActivity.this.clearWallpaper();        } catch (IOException e) {          e.printStackTrace();        }        break;      case R.id.getWall://設(shè)置ImageView顯示的內(nèi)容為當(dāng)前墻紙        currWall.setBackgroundDrawable(getWallpaper());        break;      case R.id.setWall://設(shè)置墻紙        InputStream in=WallActivity.this.getResources().openRawResource(imgIds[selectIndex]);        try {          setWallpaper(in);        } catch (IOException e) {          e.printStackTrace();        }        break;      }    }  };}

運(yùn)行結(jié)果:

希望本文所述對大家的Android程序設(shè)計(jì)有所幫助。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 乌苏市| 酒泉市| 浦江县| 海淀区| 江津市| 雷州市| 建湖县| 赤城县| 安阳县| 延寿县| 色达县| 大洼县| 福州市| 上犹县| 洛隆县| 巧家县| 盐城市| 古蔺县| 定襄县| 长岭县| 奎屯市| 慈溪市| 罗源县| 安国市| 凯里市| 万宁市| 县级市| 贵州省| 淅川县| 五台县| 泸溪县| 罗平县| 改则县| 房山区| 桐柏县| 西华县| 台南市| 牡丹江市| 顺昌县| 遂溪县| 曲水县|