【问题标题】:Get new random image when button is clicked - android单击按钮时获取新的随机图像 - android
【发布时间】:2015-12-04 01:59:04
【问题描述】:

我正在开发一个应用程序,一旦单击按钮,我就会转到一个新活动,其中 3 个随机图像设置为一个 imageView。如果用户不喜欢所选的 3 个图像,他们可以单击一个按钮以生成 3 个新的随机图像。我有两个问题...

  1. 我将 imageView 设置为特定大小,但是当图像显示时,它们有时会变小并且水平显示。我希望它们垂直显示,并且全部显示为 imageView 的大小。如何更改代码以执行此操作?

  2. 当我单击按钮时,我会显示新图像,但我收到错误消息,提示我执行几次后内存不足。如何更改代码以停止使用所有内存?

这是我的代码

public ImageView shirts;
public ImageView pants;
public ImageView shoes;
Button reload;
private Bitmap currentBitmap = null;
String[] projection = new String[]{
        MediaStore.Images.Media.DATA,
};


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.suggested_outfit);
    shirts = (ImageView)findViewById(R.id.shirtImage);
    pants = (ImageView)findViewById(R.id.pantsImage);
    shoes = (ImageView)findViewById(R.id.shoesImage);

    shirts.setImageBitmap(getImage());
    pants.setImageBitmap(getImage());
    shoes.setImageBitmap(getImage());

    reload = (Button)findViewById(R.id.getNewOutfit);
    reload.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            finish();
            startActivity(getIntent());
        }
    });
}


public Bitmap getImage () {
    Uri images = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
    Cursor cur = getContentResolver().query(images,
            projection,"", null, ""
    );
    final ArrayList<String> imagesPath = new ArrayList<String>();
    if (cur.moveToFirst()) {

        int dataColumn = cur.getColumnIndex(
                MediaStore.Images.Media.DATA);
        do {
            imagesPath.add(cur.getString(dataColumn));
        } while (cur.moveToNext());
    }
    cur.close();
    final Random random = new Random();
    final int count = imagesPath.size();
    int number = random.nextInt(count);
    String path = imagesPath.get(number);

    currentBitmap = BitmapFactory.decodeFile(path);

    return currentBitmap;
}

}

我读到重新加载活动不是一个好习惯,但我想不出一种方法来生成 3 个新的随机图像。如果有更好的方法可以做到这一点,我愿意接受任何建议。

【问题讨论】:

    标签: android image random


    【解决方案1】:

    通过onClick 方法获取图像必须有

    do {
       String path = cur.getString(dataColumn);
       if(path.contains("yourDirectory")){ //in your case, your image directory
          imagesPath.add(path);
       }
    } while (cur.moveToNext());
    

    在请求的意图/活动中

    【讨论】:

      【解决方案2】:

      解码位图会消耗大量内存,在您的代码中,您在单击按钮时多次解码位图可能会导致 OutOfMemory。尝试一次解码所有位图并将它们保存到列表中。当单击生成随机位图时,您创建列表位图的随机索引并通过该索引检索对象。希望这有帮助!

      【讨论】:

      • 不知道该怎么做,因为每次按下按钮时我都会重新加载活动。即使我将它保存到列表中,它是否会继续解码位图,因为我每次都在刷新活动?
      • 为什么需要重启activity,这是一个代价高昂的行为,每次activity重新创建Android OS都需要重新分配资源。您可以创建随机位图而无需重新启动活动。除了 Bitmap 大小可能会导致 OutOfMemory,尝试重新调整 Bitmap 的大小
      【解决方案3】:

      你可以使用这个工具:image loader 它有内存管理器。您可以在图像加载完成时设置图像视图。

      【讨论】:

      • 我无法下载任何软件包。我需要编写代码。
      • 使用 BitmapFactory.decodeStream(is, null, options) 而不是使用 BitmapFactory.decodeFile().decodeStream 调用 JNI>>nativeDecodeAsset() 来解码 Bitmap。
      猜你喜欢
      • 1970-01-01
      • 2012-12-17
      • 2021-02-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多