Android开发牛刀小试之“AA算钱软件”开发(三)

今天主要添加了一个换肤的功能,可以切换6种不同的背景,即点击“换肤”按钮可以实现切换背景图片。

这个我在网上搜了好长时间,最终最靠谱和好理解的应该是下面这个链接里的方法:点击打开链接

为了添加这个功能耗费了我好长时间,以至于我开始怀疑我这样做开发是不是有问题?因为我没有去系统学习过Android开发,很多细节的东西不清楚,这样下去会不会越来越难啊。。求哪位哥们指点下我这个新人。

好了,还是把这个功能实现的详细过程说下吧!上代码!

核心代码如下:

Resources res = getResources();
Drawable drawable = res.getDrawable(R.drawable.newImage); 
LinearLayout linearLayout = (LinearLayout)findViewById(R.id.bg);
linearLayout.setBackgroundDrawable(drawable);

对应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"
	android:background="@drawable/bg"
	android:id="@+id/bg">
	<LinearLayout
	    xmlns:android="http://schemas.android.com/apk/res/android"
		android:orientation="horizontal" 
		android:layout_width="wrap_content"
		android:layout_height="wrap_content">
	    <Button 
			android:id="@+id/but"
			android:layout_width="wrap_content"
			android:layout_height="wrap_content" 
			android:text="关于" 
			android:textSize="35px"/>
	    <Button 
	     	android:id="@+id/skinbut"
	     	android:layout_width="wrap_content"
			android:layout_height="wrap_content" 
			android:text="换肤" 
			android:textSize="35px"/>
	</LinearLayout> <span style="font-family:Arial, Helvetica, sans-serif;">//后面的比较长且不相关就不给出了</span>
我们看到原背景为bg.png图片,更改之后是newImage.png图片。之前在网上都是采用View实现切换背景的,但是总是只有一小块图片被切换了,所以我想应该是用linearLayout是最合适的吧~

之后我在此基础上实现了循环切换背景的功能,也就是每次点击“换肤”button都会切换一张新图,总共是6张图,放在drawable-hdpi文件夹下:


核心代码如下:

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		super.setContentView(R.layout.main);
		this.popbut = (Button) super.findViewById(R.id.popbut);
		this.statusinfo = (TextView) super.findViewById(R.id.statusinfo);	
		this.popbut.setOnClickListener(new OnClickListenerImpl()) ;
		Button skinButton = (Button) findViewById(R.id.skinbut);
		skinButton = (Button) super.findViewById(R.id.skinbut);
		skinButton.setOnClickListener(new SkinOnClickListenerImpl());
		
		button1=(Button)findViewById(R.id.but);  
		button1.setOnClickListener(new Button.OnClickListener(){

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				new AlertDialog.Builder(AAcharge.this)
				.setTitle("关于")
				.setMessage("This is MSJ's first Android APP, I hope you like it, have fun !")
				.setPositiveButton("确定", null)
				.show();
			}
			});
		
	}

	private class SkinOnClickListenerImpl implements OnClickListener{

		@SuppressWarnings("deprecation")
		@Override
		public void onClick(View arg0) {
			// TODO Auto-generated method stub
			Resources res = getResources();
			int skinid = 0;
			if (skinID<=6){
				skinid = getResources().getIdentifier("bg"+skinID, "drawable", getPackageName());
				Drawable drawable = res.getDrawable(skinid);
				LinearLayout linearLayout = (LinearLayout)AAcharge.this.findViewById(R.id.bg);
				linearLayout.setBackgroundDrawable(drawable);
				skinID = skinID +1;
			} else {
				skinID = 0;
				skinid = getResources().getIdentifier("bg"+skinID, "drawable", getPackageName());
				Drawable drawable = res.getDrawable(skinid);
				LinearLayout linearLayout = (LinearLayout)AAcharge.this.findViewById(R.id.bg);
				linearLayout.setBackgroundDrawable(drawable);
				skinID = skinID +1;
			}
		}
		
	}

谢谢阅读!欢迎给出宝贵意见!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值