国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > Android > 正文

Android編程實現(xiàn)等比例顯示圖片的方法

2020-04-11 11:15:37
字體:
供稿:網(wǎng)友

本文實例講述了Android編程實現(xiàn)等比例顯示圖片的方法。分享給大家供大家參考,具體如下:

在android中,由于密度的影響,如果想得到圖片的寬高是不行的,具體為什么我就大概說一下,具體的請搜索度娘或者古哥吧。 原因是如果你把圖片放在drawable-mdpi里,而手機是屬于drawable-hdpi的話,圖片是被自動放大,就這樣取到的寬與高未必就是正確的。那么如何讓android上面顯示的圖片是基于原來圖片的比例呢,首先你可以在res目錄下創(chuàng)建一個drawable-nodpi的目錄,這個目錄下的圖片是不根據(jù)dpi的多少來進行拉伸或者縮小滴。然后,就是根據(jù)屏幕的寬 和 圖片的寬高 得出圖片在屏幕顯示的高,寬是固定的,就是屏幕的寬,所以不用算了。

private void getWidth_Height() {  Display display = getWindowManager().getDefaultDisplay();  int width = display.getWidth(); // deprecated  int height = display.getHeight(); // deprecated  Bitmap mBitmap = createImageWithResouce(R.drawable.history4);  image.setLayoutParams(new LayoutParams(width, width / getBitmapWidth(mBitmap) * getBitmapHeight(mBitmap)));  image.setImageBitmap(createImageWithResouce(R.drawable.history4));}private Bitmap createImageWithResouce(int resourceID) {  Bitmap bit = BitmapFactory.decodeResource(getResources(), R.drawable.history4);  return bit;}private int getBitmapWidth(Bitmap bitmap){  return bitmap.getWidth();}private int getBitmapHeight(Bitmap bitmap){  return bitmap.getHeight();}// 釋放bitmapprivate void releaseBitmap(Bitmap bitmap){  if (bitmap!=null && !bitmap.isRecycled()) {   bitmap.recycle();   bitmap = null;  }}

建議使用如下的這種,應(yīng)用了LruCache作為管理

public class ImageUtil { private LruCache<String, Bitmap> mMemoryCache; private final Context mContext; private static ImageUtil imageUtil; private static Object obj = new Object(); private int memClass; private int cacheSize; private ImageUtil(Context mContext) {  this.mContext = mContext;  createLruCache(mContext); } private void createLruCache(Context mContext) {  memClass = ((ActivityManager) mContext.getSystemService(Context.ACTIVITY_SERVICE)).getMemoryClass();  cacheSize = 1024 * 1024 * memClass / 8;  mMemoryCache = new LruCache<String, Bitmap>(cacheSize) {   @Override   protected int sizeOf(String key, Bitmap value) {    // TODO Auto-generated method stub    return value.getRowBytes();   }  }; } public static ImageUtil getInstance(Context mContext) {  if (imageUtil == null) {   synchronized (obj) {    if (imageUtil == null) {     imageUtil = new ImageUtil(mContext);    }   }  }  return imageUtil; } public void adjustImageSize(ImageView imageView, int imageResourceId) {  Bitmap mBitmap = null;  Display display = ((MainActivity) mContext).getWindowManager().getDefaultDisplay();  int width = display.getWidth(); // deprecated  int height = display.getHeight(); // deprecated  Bitmap bitmapCache = mMemoryCache.get(imageResourceId + "");  if (bitmapCache != null) {   mBitmap = bitmapCache;  } else {   mBitmap = createImageWithResouce(mContext, imageResourceId);   mMemoryCache.put(imageResourceId + "", mBitmap);  }  RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(width, width    / getBitmapWidth(mBitmap) * getBitmapHeight(mBitmap));  layoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);  imageView.setLayoutParams(layoutParams);  imageView.setBackgroundDrawable(new BitmapDrawable(mBitmap));  // imageView.setImageBitmap(mBitmap); } private static Bitmap createImageWithResouce(Context context, int resourceID) {  Bitmap bit = BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_launcher);  return bit; } private int getBitmapWidth(Bitmap bitmap) {  return bitmap.getWidth(); } private int getBitmapHeight(Bitmap bitmap) {  return bitmap.getHeight(); }}

希望本文所述對大家Android程序設(shè)計有所幫助。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 光山县| 囊谦县| 射阳县| 东海县| 汾阳市| 河东区| 攀枝花市| 溧阳市| 虹口区| 南乐县| 永登县| 疏勒县| 景宁| 松江区| 河津市| 祁连县| 马山县| 隆子县| 通许县| 嵊泗县| 延川县| 庆元县| 灵寿县| 静安区| 三明市| 忻城县| 南郑县| 海原县| 藁城市| 阿荣旗| 务川| 聂荣县| 城固县| 于都县| 武川县| 大田县| 玉环县| 黄浦区| 沈丘县| 玉溪市| 黔西|