安卓获取res下的资源文件:string字符串、color颜色、dimen尺寸、array数组、drawable图片和xml、anim/animator动画、raw媒体、assets资源

全栈工程师开发手册 (作者:栾鹏)
安卓教程全解

安卓获取内部资源并应用。

1、获取res/values文件夹下的string.xml的字符串、color.xml的颜色、dimen.xml的尺寸、array.xml中的字符串数组,array.xml中的整型数组。

public void getresource() {
	Resources myResources = getResources();   //获取资源表实例
	
	CharSequence mytext = myResources.getText(R.string.str1);   //获取字符串,string.xml
	textView1.setText(mytext);  
	
	int mycolor= myResources.getColor(R.color.red);  //获取颜色,color.xml
	textView1.setTextColor(mycolor);   	
	
	float mydimen=myResources.getDimension(R.dimen.dimen2);  //获取尺寸,dimen.xml
	textView1.setTextSize(mydimen);  	
	
	String[] mystrarray=myResources.getStringArray(R.array.str_array);  //获取字符串数组,array.xml
	Log.v("资源字符串数组", mystrarray.toString());
	
	int[] myintarray=myResources.getIntArray(R.array.int_array);  //获取整型数组,array.xml
	Log.v("资源整型数组", myintarray.toString());

}

2、获取系统字符串、系统动画函数

//系统资源的使用
public void get_system_resource() {
	CharSequence mytext = getString(android.R.string.httpErrorBadUrl);   //获取字符串
	Log.v("系统字符串", mytext.toString());    	
}

3、获取drawable文件夹下的图片和xml

 public void getpic() 
 {
   	Resources myResources = getResources();   		
   	Drawable myDrawable = myResources.getDrawable(R.drawable.img1);   //获取drawable文件夹下的图像,
   	imageView2.setBackgroundDrawable(myDrawable);   //设置为背景
   	
   	imageView2.setBackgroundResource(R.drawable.img1);   //将上面两步合成一步
   	
   	
   	Drawable myDrawable1 = myResources.getDrawable(R.drawable.img0);   //获取drawable文件夹下图像或xml
   	imageView2.setImageDrawable(myDrawable1);  	//设置为显示图片,在背景层之上
   	
   	imageView2.setImageResource(R.drawable.img0); //将上面两步合成一步
}

4、获取anim文件夹下的视图动画、获取animator文件夹下的属性动画、drawable文件夹下的逐帧动画。

public void getanim() 
{
   	Animation myAnimation2=AnimationUtils.loadAnimation(this, R.anim.anim2);  //获取视图动画,anim文件夹下
   	textView1.setAnimation(myAnimation2);   //应用视图动画,自动启动
   	
   	ObjectAnimator myAnimator1 = (ObjectAnimator)AnimatorInflater.loadAnimator(this, R.animator.anim1);  //获取属性动画,animator文件夹下
   	myAnimator1.setTarget(button1);  //动画绑定控件
   	myAnimator1.start();   //启动属性动画
   	
   	imageView1.setBackgroundResource(R.drawable.anim3);   //获取逐帧动画,drawable文件夹下,动画绑定控件
   	AnimationDrawable myanimation3 = (AnimationDrawable) imageView1.getBackground();    //获取对动画的引用
   	myanimation3.start();  //启动逐帧动画
}

5、获取raw文件夹下的静态媒体文件

public void getraw() 
{
    	Resources myResources = getResources();   
    	InputStream myfile=myResources.openRawResource(R.raw.music1);   //文件形式读取
    	
    	//音乐文件
    	MediaPlayer mediaPlayer = MediaPlayer.create(this, R.raw.music1);   //创建音乐媒体
		mediaPlayer.start();  //启动音乐播放
		
		//视频文件(3gp,wmv,mp4),通过uri
		String uri = "android.resource://" + getPackageName() + "/" + R.raw.test;
		videoView1.setVideoURI(Uri.parse(uri));
		videoView1.start();
}

6、获取assets文件夹下的文件

public void getassets() {
    	try {
    		AssetManager assetManager = this.getAssets();
        	String [] files = assetManager.list(""); //遍历assets根目录

        	//图片文件
    		InputStream is = assetManager.open("img1.jpg");   //获取文件流
    		Bitmap image = BitmapFactory.decodeStream(is);   //将图片文件转化为图片
    		is.close();
    		//文本文件
    		is = assetManager.open("test.txt");   //获取文件流
    		int size = is.available();
            byte[] buffer = new byte[size];
            is.read(buffer);
            is.close();
            String text = new String(buffer, "utf-8");
            Log.v("assets资源", text); 
    		//音乐文件
    		MediaPlayer player = new MediaPlayer();
            AssetFileDescriptor fileDescriptor = assetManager.openFd("music1.mp3");
            player.setAudioStreamType(AudioManager.STREAM_MUSIC);  
		                                        player.setDataSource(fileDescriptor.getFileDescriptor(),fileDescriptor.getStartOffset(),fileDescriptor.getLength());
            player.prepare();
            player.start();

            
		} catch (Exception e) {
			e.printStackTrace();
		}
    	

	}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

腾讯AI架构师

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值