1.定义布局文件activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<ImageSwitcher
android:id="@+id/image_show"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="8"
android:onClick="clickImage"
>
</ImageSwitcher>
<TextSwitcher
android:id="@+id/txt_show"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="@android:color/black"
/>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="center"
>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="上一幅"
android:onClick="clickPrev"
tools:ignore="ButtonStyle,HardcodedText" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="下一幅"
android:onClick="clickNext"
tools:ignore="ButtonStyle,HardcodedText" />
</LinearLayout>
</LinearLayout>
2.定义Activity
package com.imageswitcher;
import android.app.WallpaperManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AnimationUtils;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.TextSwitcher;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ViewSwitcher;
import java.io.IOException;
public class MainActivity extends AppCompatActivity {
//定义图片切换器对象
ImageSwitcher imgShow;
//定义文本切换器对象txtShow
TextSwitcher txtShow;
//用于修改桌面背景的图片
Bitmap bitmap=null;
//定义图片数组
int[]imgRes=new int[]{
R.drawable.kebo,R.drawable.kobe1,
R.drawable.kobe2,R.drawable.kebo
};
//定义文本数组
String[]txtRes=new String[]{
"第1幅","第2幅","第3幅","第4幅"
};
//定义数组下标
int index=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//调用xml布局文件
imgShow=(ImageSwitcher)this.findViewById(R.id.image_show);
txtShow=(TextSwitcher)this.findViewById(R.id.txt_show);
//定义视图操作工厂对象imgFactory
ImageViewFactory imgFactory = new ImageViewFactory();
imgShow.setFactory(imgFactory);
//定义视图操作工厂对象txtFactory
TextViewFactory txtFactory=new TextViewFactory();
txtShow.setFactory(txtFactory);
//为图片切换器设置切换的动画效果
imgShow.setInAnimation(AnimationUtils.loadAnimation(this,android.R.anim.fade_in));
txtShow.setOutAnimation(AnimationUtils.loadAnimation(this,android.R.anim.fade_out));
//为文本切换器设置文本
txtShow.setText("当前图片位置:"+txtRes[index]);
//将当前显示图片解码为bitmap对象
bitmap= BitmapFactory.decodeResource(getResources(),imgRes[index]);
}
//图片切换器的单击事件
public void clickImage(View v)
{
//设置手机背景,需先添加权限
try{
// MainActivity.this.setWallpaper(bitmap);
//替代方法
WallpaperManager wallpaperManager=WallpaperManager.getInstance(MainActivity.this);
wallpaperManager.setBitmap(bitmap);
}catch (IOException e)
{
e.printStackTrace();
}
Toast.makeText(MainActivity.this,"桌面设置成功!",Toast.LENGTH_LONG).show();
}
//上一幅的单击事件
public void clickPrev(View v)
{
//浏览到了第一幅,则将数组下标定位到最后一幅,以便循环显示
if (index<=0)
{
index=imgRes.length;
}
imgShow.setImageResource(imgRes[--index]);
txtShow.setText("当前图片位置:"+txtRes[index]);
bitmap=BitmapFactory.decodeResource(getResources(),imgRes[index]);
}
//下一幅的单击事件
public void clickNext(View v)
{
//浏览到了最后一幅,则将数组下标定位到第一幅,以便循环显示
if (index>=imgRes.length-1)
{
index=-1;
}
imgShow.setImageResource(imgRes[++index]);
txtShow.setText("当前图片位置:"+txtRes[index]);
bitmap=BitmapFactory.decodeResource(getResources(),imgRes[index]);
}
//定义视图操作工厂类
public class ImageViewFactory implements ViewSwitcher.ViewFactory {
@Override
public View makeView()
{
ImageView img=new ImageView(MainActivity.this);
img.setScaleType(ImageView.ScaleType.FIT_XY);
img.setLayoutParams(new ImageSwitcher.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT
));
return img;
}
}
public class TextViewFactory implements ViewSwitcher.ViewFactory{
@Override
public View makeView()
{
TextView text =new TextView(MainActivity.this);
//设置文本颜色
text.setTextColor(Color.WHITE);
text.setLayoutParams(new TextSwitcher.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT
));
//设置文本大小
text.setTextSize(30);
//设置文本对齐方式
text.setGravity(Gravity.CENTER);
return text;
}
}
}
import android.app.WallpaperManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AnimationUtils;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.TextSwitcher;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ViewSwitcher;
import java.io.IOException;
public class MainActivity extends AppCompatActivity {
//定义图片切换器对象
ImageSwitcher imgShow;
//定义文本切换器对象txtShow
TextSwitcher txtShow;
//用于修改桌面背景的图片
Bitmap bitmap=null;
//定义图片数组
int[]imgRes=new int[]{
R.drawable.kebo,R.drawable.kobe1,
R.drawable.kobe2,R.drawable.kebo
};
//定义文本数组
String[]txtRes=new String[]{
"第1幅","第2幅","第3幅","第4幅"
};
//定义数组下标
int index=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//调用xml布局文件
imgShow=(ImageSwitcher)this.findViewById(R.id.image_show);
txtShow=(TextSwitcher)this.findViewById(R.id.txt_show);
//定义视图操作工厂对象imgFactory
ImageViewFactory imgFactory = new ImageViewFactory();
imgShow.setFactory(imgFactory);
//定义视图操作工厂对象txtFactory
TextViewFactory txtFactory=new TextViewFactory();
txtShow.setFactory(txtFactory);
//为图片切换器设置切换的动画效果
imgShow.setInAnimation(AnimationUtils.loadAnimation(this,android.R.anim.fade_in));
txtShow.setOutAnimation(AnimationUtils.loadAnimation(this,android.R.anim.fade_out));
//为文本切换器设置文本
txtShow.setText("当前图片位置:"+txtRes[index]);
//将当前显示图片解码为bitmap对象
bitmap= BitmapFactory.decodeResource(getResources(),imgRes[index]);
}
//图片切换器的单击事件
public void clickImage(View v)
{
//设置手机背景,需先添加权限
try{
// MainActivity.this.setWallpaper(bitmap);
//替代方法
WallpaperManager wallpaperManager=WallpaperManager.getInstance(MainActivity.this);
wallpaperManager.setBitmap(bitmap);
}catch (IOException e)
{
e.printStackTrace();
}
Toast.makeText(MainActivity.this,"桌面设置成功!",Toast.LENGTH_LONG).show();
}
//上一幅的单击事件
public void clickPrev(View v)
{
//浏览到了第一幅,则将数组下标定位到最后一幅,以便循环显示
if (index<=0)
{
index=imgRes.length;
}
imgShow.setImageResource(imgRes[--index]);
txtShow.setText("当前图片位置:"+txtRes[index]);
bitmap=BitmapFactory.decodeResource(getResources(),imgRes[index]);
}
//下一幅的单击事件
public void clickNext(View v)
{
//浏览到了最后一幅,则将数组下标定位到第一幅,以便循环显示
if (index>=imgRes.length-1)
{
index=-1;
}
imgShow.setImageResource(imgRes[++index]);
txtShow.setText("当前图片位置:"+txtRes[index]);
bitmap=BitmapFactory.decodeResource(getResources(),imgRes[index]);
}
//定义视图操作工厂类
public class ImageViewFactory implements ViewSwitcher.ViewFactory {
@Override
public View makeView()
{
ImageView img=new ImageView(MainActivity.this);
img.setScaleType(ImageView.ScaleType.FIT_XY);
img.setLayoutParams(new ImageSwitcher.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT
));
return img;
}
}
public class TextViewFactory implements ViewSwitcher.ViewFactory{
@Override
public View makeView()
{
TextView text =new TextView(MainActivity.this);
//设置文本颜色
text.setTextColor(Color.WHITE);
text.setLayoutParams(new TextSwitcher.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT
));
//设置文本大小
text.setTextSize(30);
//设置文本对齐方式
text.setGravity(Gravity.CENTER);
return text;
}
}
}
3.添加权限
<!-- 设置手机桌面背景权限-->
<uses-permission android:name="android.permission.SET_WALLPAPER"/>
4.效果图: