Androidプログラミングの実現などの割合で画像を表示する方法

4108 ワード

この例は、Androidプログラミングの実現などのスケールで画像を表示する方法を説明する。皆さんに参考にしてあげます。具体的には以下の通りです。
Androidでは密度の影響で、写真の幅の高さを求めるのは無理です。具体的にはなぜですか?なぜなら、あなたが画像をdrawable-mdpiに置くと、携帯はdrawable-hdpiに属しています。画像は自動的に拡大されています。このまま取った幅と高さは必ずしも正しいとは限りません。どのようにandroidの上に表示させる画像は元の画像の比率に基づいていますか?まず、resディレクトリの下にdrawable-nodpiのディレクトリを作成することができます。このディレクトリの下の画像はdpiの数によって引き伸ばしたり縮小したりしません。そして、スクリーンの広さと画像の幅から画像がスクリーンに表示される高さを引き出すということです。幅は固定されています。スクリーンの広さですから、いいです。

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();
}
//   bitmap
private void releaseBitmap(Bitmap bitmap){
  if (bitmap!=null && !bitmap.isRecycled()) {
   bitmap.recycle();
   bitmap = null;
  }
}

以下のように、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プログラムの設計に役に立ちます。