`

有效的减少应用占用的空间——使用系统资源

阅读更多

做安卓开发的,是不是总想把自己的应用做的足够强大,但空间足够小?可以需求方又要求很炫的效果,怎么办?

其实系统自带了很多风格的边框、图标等,而且不同版本之间略有差异,但功能是一样的。我们为什么放着这些现成的资源不用呢?下面就来看看系统都有什么吧。运行下面的例子,可以看到系统中所有drawable资源。

 

MainActivity.java

package com.jmeditor.sysresbrowser;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MainActivity extends Activity {

	//浏览资源的序号
	private static int index = 0;
	//所有资源列表
	private static List<Map<String,Object>> rs;
	//显示图片的View
	private ImageView imageView;
	//显示图片的背景,因为有些只适合在黑背景下显示
	private LinearLayout imageBg;
	//显示数量
	private TextView textView;
	//显示当前图片的ID,方便引用
	private TextView tips;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		textView = (TextView)findViewById(R.id.textView);
		tips = (TextView)findViewById(R.id.tips);
		imageView = (ImageView)findViewById(R.id.imageView);
		imageBg = (LinearLayout)findViewById(R.id.imageBg);
		Button imageViewBtnPre = (Button)findViewById(R.id.imageViewBtnPre);
		Button imageViewBtnNext = (Button)findViewById(R.id.imageViewBtnNext);
		
		//读取系统所有的drawable资源
		try {
			Class c = Class.forName("android.R$drawable");
			Field[] fs = c.getDeclaredFields();
			rs = new ArrayList<Map<String,Object>>();
				
			for (Field field : fs) {
				if (field.getType().getName().equals("int") ) {
					Map<String,Object> m = new HashMap<String,Object>();
					m.put("name", field.getName());
					m.put("value", field.get(this));
					rs.add(m);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		//上一张
		imageViewBtnPre.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				if(index > 0){
					index --;
					showImg();
				}
			}

			
		});
		//下一张
		imageViewBtnNext.setOnClickListener(new OnClickListener(){
			
			@Override
			public void onClick(View v) {
				if(index < rs.size() - 1){
					index ++;
					showImg();
				}
			}
			
		});
		
		//背景色
		this.findViewById(R.id.bg_1).setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				imageBg.setBackgroundColor(getResources().getColor(R.color.white));
			}
			
		});
		this.findViewById(R.id.bg_2).setOnClickListener(new OnClickListener(){
			
			@Override
			public void onClick(View v) {
				imageBg.setBackgroundColor(getResources().getColor(R.color.black));
			}
			
		});
		this.findViewById(R.id.bg_3).setOnClickListener(new OnClickListener(){
			
			@Override
			public void onClick(View v) {
				imageBg.setBackgroundColor(getResources().getColor(R.color.blue));
			}
			
		});
	}
	
	private void showImg() {
		imageView.setBackgroundResource(Integer.valueOf(rs.get(index).get("value").toString()));
		String s = getResources().getString(R.string.total_msg);
		textView.setText(s.replaceAll("\\{1\\}", "" + rs.size()).replaceAll("\\{2\\}", "" + (index+1)));
		tips.setText(rs.get(index).get("name").toString());
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}

 

布局文件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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />
    
    <LinearLayout 
        android:id="@+id/imageBg"
        android:layout_width="fill_parent"
        android:layout_height="100dip"
        android:padding="5dip">
	    <ImageView 
	        android:id="@+id/imageView"
	        android:layout_width="wrap_content"
	        android:layout_height="wrap_content"/>
    </LinearLayout>
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dip">
		<TextView
	        android:layout_width="wrap_content"
	        android:layout_height="wrap_content"
	        android:text="@string/tips"/>
		<TextView
		    android:id="@+id/tips"
	        android:layout_width="wrap_content"
	        android:layout_height="wrap_content"/>
	</LinearLayout>
	<LinearLayout 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">
	    <Button 
	        android:id="@+id/imageViewBtnPre"
	        android:text="@string/pre_img"
	        android:layout_width="wrap_content"
	        android:layout_height="wrap_content"/>
	    
	    <Button 
	        android:id="@+id/imageViewBtnNext"
	        android:text="@string/next_img"
	        android:layout_width="wrap_content"
	        android:layout_height="wrap_content"/>
	</LinearLayout>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/background_title"/>
	    
    <LinearLayout 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
	    <View
	        android:id="@+id/bg_1"
	        android:layout_width="50dip"
	        android:layout_height="50dip"
	        android:layout_margin="5dip"
	        android:background="@color/white"/>
	    
	    <View
	        android:id="@+id/bg_2"
	        android:layout_width="50dip"
	        android:layout_height="50dip"
	        android:layout_margin="5dip"
	        android:background="@color/black"/>
	    
	    <View
	        android:id="@+id/bg_3"
	        android:layout_width="50dip"
	        android:layout_height="50dip"
	        android:layout_margin="5dip"
	        android:background="@color/blue"/>
    </LinearLayout>
</LinearLayout>

 

 

 

  • 大小: 43.7 KB
分享到:
评论

相关推荐

    asp.net知识库

    在 SQL Server 2005 中使用表值函数来实现空间数据库 SQL Server 2005的30个最重要特点 同时安装sql2000和sql2005的经验 类如何与界面绑定 在Asp.net中如何用SQLDMO来获取SQL Server中的对象信息 使用Relations建立...

    计算机应用技术(实用手册)

    这会使得内存有15 MB以上的空间无法让系统使用,这个项目请使用系统的默认值。 Delay Prior to Thermal(激活延时设置): 此项目可用来选在择温探(Thermal)装置动作之前的延迟时间。 AGP Aperture Size(AGP...

    web_asp程序设计及网页设计

    大量的图形、音频和视频信息会占用相当大的磁盘空间,我们甚至无法预知信息的多少。对于Web没有必要把所有信息都放在一起,信息可以放在不同的站点上。只需要在浏览器中指明这个站点就可以了。使在物理上并不一定在...

    服务器虚拟化方案.pptx

    减少复杂性 更好的可用性 更小的TCO成本 减少部署的时间和成本 虚拟化数据中心 传统数据中心 复杂,混乱 额外的集群软件 大量设备和空间占用 现场实施 虚拟化转变数据中心架构 服务器虚拟化方案全文共39页,当前为...

    刀片服务器详解(1).docx

    资料显示,在机柜系统配置好的前提下,将1U机架优化服务器系统移植到刀片服务器上,所占用的空间只是原来的1/3~1/2。而在一个标准的机柜式环境里,刀片服务器的处理密度要提高四到五倍。比如在处理1024节点的高密度...

    刀片服务器详解(2).docx

    资料显示,在机柜系统配置好的前提下,将1U机架优化效劳器系统移植到刀片效劳器上, 占用的空间只是原来的1/3~1/2。而在一个标准的机柜式环境里,刀片效劳器的处理密度要提高四到五倍。比方在处理1024节点的高密度...

    X3BLOG AJAX国产大型开源多用户博客系统 1.1.0.beta1编译版

    存资源的占用降至最低水平,并通过gzip压缩进一步缩减服务器的网络带宽消耗,提高响应速度 。 无Session设计杜绝了用户会话无故丢失的尴尬,客户端关联的会话加密方式带来了用户数据的高安全性,独特的 XSL结构...

    X3BLOG AJAX国产大型开源多用户博客系统 1.1.0.beta1源码版

    存资源的占用降至最低水平,并通过gzip压缩进一步缩减服务器的网络带宽消耗,提高响应速度 。 无Session设计杜绝了用户会话无故丢失的尴尬,客户端关联的会话加密方式带来了用户数据的高安全性,独特的 XSL结构...

    《计算机操作系统》期末复习指导

    1、科普的观点 操作系统是计算机系统的管理和控制中心,它依照设计者制定的各种调度策略组织和管理计算机系统资源,使之能高效地运行。 2、功能的观点 操作系统是一个计算机资源管理系统,它负责计算机系统的全部...

    X3BLOG 单用户版 1.0 build80707 (access)

    X3BLOG完美的利用了浏览器的XML解析技术,完全实现数据和界面的分离,使网络传输数据量大大减少,加载速度远远超过了市面上所有的BLOG产品,有效的减轻了服务器的带宽压力,服务器端使用四大动态网站开发语言中速度...

    新版Android开发教程.rar

    这一联盟将会支持 Google 发布的 Android 手机操作系统或者应用软件,共同开发名为 Android 的 开 放源代码的移动系统。开放手机联盟包括手机制造商、手机芯片厂商和移动运营商几类。目前,联盟成员 数 量已经达到了...

    工程硕士学位论文 基于Android+HTML5的移动Web项目高效开发探究

    其中使用Struts作为系统的整体基础架构,负责MVC的分离,在Struts框架的模型部分,控制业务跳转,利用Hibernate框架对持久层提供支持,Spring做管理,管理Struts和Hibernate。 WebStorage HTML新增的本地存储解决...

    电脑变慢de完全解决方案.

    该病毒运行后,会使消耗大量的系统资源,使系统明显变慢,并且杀掉一些正在运行的反病毒软件,建立四个线程在局域网中疯狂传播。  病毒特征  如果用户发现计算机中有这些特征,则很有可能中了此病毒:  •病毒...

    X3BLOG 单用户版 FOR ACCESS 1.0beta 源代码

    X3-BLOG完美的利用了浏览器的XML解析技术,完全实现数据和界面的分离,使网络传输数据量大大减少,加载速度远远超过了市面上所有的BLOG产品,有效的减轻了服务器的带宽压力,服务器端使用四大动态网站开发语言中速度...

    x3blog 单用户博客系统 1.0.80802 源代码

    1.X3BLOG完美的利用了浏览器的XML解析技术,完全实现数据和界面的分离,使网络传输数据量大大减少,加载速度远远超过了市面上所有的BLOG产品,有效的减轻了服务器的带宽压力,服务器端使用四大动态网站开发语言中...

    x3blog 单用户博客系统 1.0.80802 编译版

    1.X3BLOG完美的利用了浏览器的XML解析技术,完全实现数据和界面的分离,使网络传输数据量大大减少,加载速度远远超过了市面上所有的BLOG产品,有效的减轻了服务器的带宽压力,服务器端使用四大动态网站开发语言中...

    x3blog单用户博客程序源码-1.0.build80802-src

    功能与特点X3BLOG完美的利用了浏览器的XML解析技术,完全实现数据和界面的分离,使网络传输数据量大大减少,加载速度远远超过了市面上所有的BLOG产品,有效的减轻了服务器的带宽压力,服务器端使用四大动态网站开发...

    oracle学习文档 笔记 全面 深刻 详细 通俗易懂 doc word格式 清晰 连接字符串

    mssql 微软 只能能运行在windows平台,体积比较庞大,占用许多系统资源, 但使用很方便,支持命令和图形化管理,收费。 中型企业 Mysql 甲骨文 是个开源的数据库server,可运行在多种平台, 特点是响应速度特别快,...

    x3blog博客程序 0.7.1.1

    功能与特点X3-BLOG完美的利用了浏览器的XML解析技术,完全实现数据和界面的分离,使网络传输数据量大大减少,加载速度远远超过了市面上所有的BLOG产品,有效的减轻了服务器的带宽压力,服务器端使用四大动态网站开发...

Global site tag (gtag.js) - Google Analytics