有时候我们看可以看到有些App可以动态改变背景样式,这时候我们可以利用广播的方式进行实现。
1.自定义广播类(图片存在于assets文件夹下)
class Receiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
String path=intent.getStringExtra("path");
setBitmap(path);
}
}
@SuppressLint("NewApi")
public void setBitmap(String path){
AssetManager am=getAssets();
InputStream in=null;
try {
in=am.open("bkgs/"+path);
Bitmap bitmap=BitmapFactory.decodeStream(in);
layoutParent.setBackground(new BitmapDrawable(getResources(), bitmap));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
if(null!=in){
in.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
2.注册广播
在onCreat()方法中注册广播
private Receiver mReceiver;
private void regiesterBgReceiver() {
mReceiver=new Receiver();
IntentFilter intent=new IntentFilter("CHANGE_BG");
registerReceiver(mReceiver, intent);
}
3.点击按钮 发送广播 改变样式
private void initEvent() {
btnChange.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
sendBroadCast("00"+(++i%10)+".jpg");
}
});
}
详细代码如下:
package com.example.test_bg2;
import java.io.IOException;
import java.io.InputStream;
import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.ColorFilter;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
public class MainActivity extends Activity {
private Button btnChange;
private RelativeLayout layoutParent;
int i=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnChange=(Button) findViewById(R.id.btn_changeBg);
layoutParent=(RelativeLayout) findViewById(R.id.layout_parent);
regiesterBgReceiver();
initEvent();
}
private void initEvent() {
btnChange.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
sendBroadCast("00"+(++i%10)+".jpg");
}
});
}
private void sendBroadCast(String path){
Intent intent=new Intent("CHANGE_BG");
intent.putExtra("path", path);
sendBroadcast(intent);
}
private Receiver mReceiver;
private void regiesterBgReceiver() {
mReceiver=new Receiver();
IntentFilter intent=new IntentFilter("CHANGE_BG");
registerReceiver(mReceiver, intent);
}
class Receiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
String path=intent.getStringExtra("path");
setBitmap(path);
}
}
@SuppressLint("NewApi")
public void setBitmap(String path){
AssetManager am=getAssets();
InputStream in=null;
try {
in=am.open("bkgs/"+path);
Bitmap bitmap=BitmapFactory.decodeStream(in);
layoutParent.setBackground(new BitmapDrawable(getResources(), bitmap));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
if(null!=in){
in.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
@Override
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(mReceiver);
}
}
为了简单 这只是在单个Activity中进行实现背景样式的改变,如果改变所有的Activity样式,我们可以
自己封装BaseActivity 抽取出抽象方法供子类实现,从而改变整个应用的背景样式。