android显示图片的两种方法

第一种使用xml文件

1.新建android application

2.将要显示的图片img.png放到res/drawable-mdpi目录下

3.打开res/layout目录下的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"
    >
	<ImageView  
		android:id="@+id/imageView"
    	android:layout_width="fill_parent" 
    	android:layout_height="wrap_content"
    	android:src="@drawable/img" 
    />
</LinearLayout>

运行项目即可


第二中直接在java代码里实现

1.新建android application

2.将要显示的图片img.png放到res/drawable-mdpi目录下

3.打开src目录下的java源文件,写入如下代码

import android.app.Activity;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.widget.ImageView;
public class ImgShow extends Activity {
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ImageView imageView = new ImageView(this);
        imageView.setImageBitmap(
        		BitmapFactory.decodeResource(getResources(), 
        		R.drawable.img));
        this.setContentView(imageView);
    }
}
运行即可



版权声明:

原文地址:https://www.cnblogs.com/walccott/p/4957124.html