<?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"> <textview android:layout_width="fill_parent" android:layout_height="wrap_content" android:textsize="20sp" android:text="Drawable的使用-设置壁纸"> <button android:layout_width="wrap_content" android:layout_height="wrap_content" android:textsize="20sp" android:text="查看图片A" android:id="@+id/Button01"> </button> <button android:layout_width="wrap_content" android:layout_height="wrap_content" android:textsize="20sp" android:text="查看图片B" android:id="@+id/Button02"> </button> <button android:layout_width="wrap_content" android:layout_height="wrap_content" android:textsize="20sp" android:text="设置图片A为壁纸" android:id="@+id/Button03"> </button> <button android:layout_width="wrap_content" android:layout_height="wrap_content" android:textsize="20sp" android:text="设置图片B为壁纸" android:id="@+id/Button04"> </button> <button android:layout_width="wrap_content" android:layout_height="wrap_content" android:textsize="20sp" android:text="恢复默认壁纸" android:id="@+id/Button05"> </button> <imageview android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/ImageView01"> </imageview> </textview></linearlayout>
package android.basic.lesson23;
import java.io.IOException;
import android.app.Activity;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
public class MainDrawable extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//定义UI组件
Button b1 = (Button) findViewById(R.id.Button01);
Button b2 = (Button) findViewById(R.id.Button02);
Button b3 = (Button) findViewById(R.id.Button03);
Button b4 = (Button) findViewById(R.id.Button04);
Button b5 = (Button) findViewById(R.id.Button05);
final ImageView iv= (ImageView)findViewById(R.id.ImageView01);
//定义按钮点击监听器
OnClickListener ocl = new OnClickListener() {
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.Button01:
//给ImageView设置图片,从存储卡中获取图片为Drawable,然后把Drawable设置为ImageView的背景
iv.setBackgroundDrawable(Drawable.createFromPath("/sdcard/a.jpg"));
break;
case R.id.Button02:
iv.setBackgroundDrawable(Drawable.createFromPath("/sdcard/b.jpg"));
break;
case R.id.Button03:
try {
//Activity的父类ContextWrapper有这个setWallpaper方法,当然使用此方法需要有android.permission.SET_WALLPAPER权限
setWallpaper(BitmapFactory.decodeFile("/sdcard/a.jpg"));
} catch (IOException e1) {
e1.printStackTrace();
}
break;
case R.id.Button04:
try {
setWallpaper(BitmapFactory.decodeFile("/sdcard/b.jpg"));
} catch (IOException e1) {
e1.printStackTrace();
}
break;
case R.id.Button05:
try {
//Activity的父类ContextWrapper有这个clearWallpaper方法,作用是恢复默认壁纸,当然使用此方法需要有android.permission.SET_WALLPAPER权限
clearWallpaper();
} catch (IOException e) {
e.printStackTrace();
}
break;
}
}
};
//给按钮们绑定点击监听器
b1.setOnClickListener(ocl);
b2.setOnClickListener(ocl);
b3.setOnClickListener(ocl);
b4.setOnClickListener(ocl);
b5.setOnClickListener(ocl);
}
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="android.basic.lesson23" android:versioncode="1" android:versionname="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:label="@string/app_name" android:name=".MainDrawable">
<intent -filter="">
<action android:name="android.intent.action.MAIN">
<category android:name="android.intent.category.LAUNCHER">
</category></action></intent>
</activity>
</application>
<uses -sdk="" android:minsdkversion="8">
<uses -permission="" android:name="android.permission.SET_WALLPAPER"></uses>
</uses></manifest>
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有