Android入门之ImageView(纯java)

 Android入门之ImageView(纯java)_第1张图片

 

import android.app.Activity;
import android.graphics.*;
import android.graphics.drawable.*;
import android.os.Bundle;
import android.widget.*;
import android.widget.ImageView.ScaleType;

public class ImageViewTest extends Activity {
	
	private LinearLayout mainLayout=null;
	private ImageView iv=null;
	
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        /*mainLayout初始化*/
        mainLayout=new LinearLayout(this);
        mainLayout.setLayoutParams(new LinearLayout.LayoutParams(-1,-1));
        /*iv初始化*/
        iv=new ImageView(this);
        iv.setLayoutParams(new LinearLayout.LayoutParams(-1,-2));
        iv.setScaleType(ScaleType.CENTER);//居中显示
        
        /*加载图片方法1*/
        //Drawable d=this.getResources().getDrawable(R.drawable.icon);//此处icon对应drawable文件夹下的icon.png
        //iv.setImageDrawable(d);//设置图片
        
        /*加载图片方法2*/
        int resId=this.getResources().getIdentifier("icon","drawable",getPackageName());
        iv.setImageResource(resId);
        
        /*加载图片方法3,在方法2的基础上生成Bitmap bm*/
        //int resId=this.getResources().getIdentifier("icon","drawable", getPackageName());
        //Bitmap bm = BitmapFactory.decodeResource(this.getResources(), resId);
        //iv.setImageBitmap(bm);
        
        mainLayout.addView(iv);//添加iv
        setContentView(mainLayout);//显示manLayout
    }
}


 

你可能感兴趣的:(java,android,null,Class,import)