Android AVD获取res/drawable目录下的图片

编写环境为eclipse
获取res下的drawable文件夹下图片资源,这里的drawable包括下面这些:
Android AVD获取res/drawable目录下的图片_第1张图片


效果图:

先创建一个布局文件activity_main.xml
然后在里面添加三个控件:ImageViewEditTextButton,用于显示图片、输入图片名称以及确定按钮。

具体代码如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    
    <ImageView
        android:id="@+id/imageview"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="35dp"
        android:background="#99CC99"
        android:layout_width="150dp"
        android:layout_height="150dp"/>

    <EditText
        android:id="@+id/et_psw"
        android:layout_width="fill_parent"
        android:layout_height="38dp"
        android:layout_gravity="center_horizontal"
        android:layout_marginLeft="50dp"
        android:layout_marginRight="50dp"
        android:background="@drawable/register_psw"
        android:paddingLeft="8dp"
        android:layout_marginTop="15dp"
        android:hint="请输入图片名称,不要带后缀"
        android:singleLine="true"
        android:textColor="#000000"
        android:textColorHint="#a3a3a3"
        android:textSize="14sp" />
    
    <Button
        android:id="@+id/button"
        android:layout_width="fill_parent"
        android:layout_height="38dp"
        android:layout_marginTop="15dp"
        android:layout_marginLeft="50dp"
        android:layout_marginRight="50dp"
        android:layout_gravity="center_horizontal"
        android:background="@drawable/register_selector"
        android:text="获取res目录下的文件"
        android:textColor="@android:color/white"
        android:textSize="18sp"/>
</LinearLayout>

Button按钮添加一个按压效果:drawable文件夹下新建register_selector.xml文件

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:state_pressed="true" android:drawable="@drawable/register_inco_selected">
    </item>
    <item android:drawable="@drawable/register_icon_normal"></item>
</selector>

具体的获取代码如下:

		ApplicationInfo  appInfo = this.getApplicationInfo();
        int resID = getResources().getIdentifier(imageName, "drawable", appInfo.packageName);
        return BitmapFactory.decodeResource(getResources(), resID);

获取了,然后要让ImageView控件显示出来:
只显示其中的一张,这一张图片的名字还要自己输,获取EditText控件中输入的内容:

et_psw=(EditText) findViewById(R.id.et_psw);
psw=et_psw.getText().toString().trim();

再加一个判断:

				if (TextUtils.isEmpty(psw)){
                	Toast.makeText(MainActivity.this, "请输入图片名称", Toast.LENGTH_SHORT).show();
                	return;
                }
                else if(getRes(psw)==null){
                		Toast.makeText(MainActivity.this, "没有此图", Toast.LENGTH_SHORT).show();
                		return;
                }

具体的逻辑代码如下:

import android.app.Activity;
import android.content.pm.ApplicationInfo;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

public class MainActivity extends Activity{

	private Button button;//获取图片资源btn
	private ImageView imageview;
	private String psw;
    private EditText et_psw;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button = (Button) findViewById(R.id.button);
        imageview = (ImageView) findViewById(R.id.imageview);
        et_psw=(EditText) findViewById(R.id.et_psw);
        
        
        button.setOnClickListener(new OnClickListener(){
            @Override
            public void onClick(View arg0) {
            	psw=et_psw.getText().toString().trim();
                imageview.setImageBitmap(getRes(psw));
                if (TextUtils.isEmpty(psw)){
                	Toast.makeText(MainActivity.this, "请输入图片名称", Toast.LENGTH_SHORT).show();
                	return;
                }
                else {
                	if(getRes(psw)==null){
                		Toast.makeText(MainActivity.this, "没有此图", Toast.LENGTH_SHORT).show();
                		return;
                	}
                }
           }
       });
    }
    
    //获取Res下的drawable文件夹下图片资源
    private Bitmap getRes(String imageName) {
        ApplicationInfo  appInfo = this.getApplicationInfo();
        int resID = getResources().getIdentifier(imageName, "drawable", appInfo.packageName);
        return BitmapFactory.decodeResource(getResources(), resID);
    }
}

demo地址:
Android AVD获取res/drawable目录下的图片_第2张图片   在这里插入图片描述

你可能感兴趣的:(Android)