DisplayMetricsTest

package hyz.com;
import zte.com.cn.R;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
	
public class DisplayMetricsTest extends Activity 
{
	private TextView textview1;
	private Button button1,button2;
	private ImageView mImageView01;
	//获取手机屏幕分辨率的类
    private DisplayMetrics dm;
    public void onCreate(Bundle savedInstanceState) 
    {
    	super.onCreate(savedInstanceState);
    	setContentView(R.layout.main);
    	textview1 = (TextView)findViewById(R.id.textview1);
    	mImageView01 = (ImageView)findViewById(R.id.myImageView1);
    	button1 = (Button)findViewById(R.id.button1);
    	button2 = (Button)findViewById(R.id.button2);
    	button1.setOnClickListener(new Button.OnClickListener()
    	{
    		public void onClick(View v)
    		{
    			dm = new DisplayMetrics();
    			getWindowManager().getDefaultDisplay().getMetrics(dm);
    			//获得手机的宽带和高度像素单位为px
    			String str =
    					"widthPixels:" + dm.widthPixels + "\n" +
    					"heightPixels:" + dm.heightPixels + "\n" +
    					"density:" + dm.density + "\n" +
    					"densityDpi:" + dm.densityDpi + "\n" +
    					"scaledDensity:" + dm.scaledDensity + "\n" +
    					"xdpi:" + dm.xdpi + "\n" +
    					"ydpi:" + dm.ydpi + "\n" +
    					"DENSITY_DEFAULT:" + DisplayMetrics.DENSITY_DEFAULT + "\n" +
    					"DENSITY_HIGH:" + DisplayMetrics.DENSITY_HIGH + "\n" +
    					"DENSITY_LOW:" + DisplayMetrics.DENSITY_LOW + "\n" +
    					"DENSITY_MEDIUM:" + DisplayMetrics.DENSITY_MEDIUM; 					
    				
    			textview1.setText(str);
    			
    		}
    	});   
    	button2.setOnClickListener(new Button.OnClickListener()
    	{
    		public void onClick(View v)
    		{
    			show();
    		}
    	}); 
	}
    //全屏显示图片
    private void show()
    {
    	DisplayMetrics dm=new DisplayMetrics();
    	getWindowManager().getDefaultDisplay().getMetrics(dm);
    	Bitmap bmp=BitmapFactory.decodeResource(getResources(), R.drawable.hi);
    	/* Matrix比例*/ 
    	float scaleWidth = ((float) dm.widthPixels) / bmp.getWidth();
    	float scaleHeight = ((float) dm.heightPixels) / bmp.getHeight() ;
    	Matrix matrix = new Matrix(); 
    	matrix.postScale(scaleWidth, scaleHeight);
    	/* ReSize文件至屏幕分辨率 */
    	Bitmap resizedBitmap = Bitmap.createBitmap(bmp,0,0,bmp.getWidth(),bmp.getHeight(),matrix,true);
    	//新建Drawable放大图片文件至全屏幕
    	BitmapDrawable myNewBitmapDrawable = new BitmapDrawable(resizedBitmap); 
    	mImageView01.setImageDrawable(myNewBitmapDrawable); 
    	mImageView01.setVisibility(View.VISIBLE);
    }
	  
}


 

你可能感兴趣的:(String,Class,手机,float,button,Matrix)