这对我有用
int width;
int height;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_matn_activity3);
Display display = getWindowManager().getDefaultDisplay();
width = display.getWidth(); // deprecated
height = display.getHeight(); // deprecated
SpannableString ss = new SpannableString("ABC");
Drawable d = getResources().getDrawable(R.drawable.ic_launcher);
double ratio= (double)((double)(d.getIntrinsicWidth())/(double)(d.getIntrinsicHeight()));
//this shows the original ratio of image
if(d.getIntrinsicWidth()
d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
Log.v("small","small");
}else{
Log.v("big","big");
d.setBounds(0, 0,(int)(width*.95),(int)(d.getIntrinsicHeight()/ratio*.95));
//i dont know why but if you put 1 instead of .95 again you will see 2 images
//this is exactly the magic width you have said
}
}
它可能没有什么问题,因为我的项目很大且与众不同,我无法放入确切的代码
.但是通常可以