【问题标题】:Image randomizer that sends the current selected image trough intent发送当前选择的图像槽意图的图像随机化器
【发布时间】:2018-09-03 16:49:22
【问题描述】:

我在图像按钮上有一个图像随机化器。但是使用此图像按钮时,我无法将当前加载的图像作为意图发送。

第一次活动

    imageButton = (ImageButton) findViewById(R.id.imageButton);

    imageButton.buildDrawingCache();
    final Bitmap bitmap = imageButton.getDrawingCache();

    imageButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v){
            Intent intentLoadNewActivity = new Intent(MainActivity.this, NewActivity.class);
            intentLoadNewActivity.putExtra ("BitmapImage", bitmap);
            startActivity(intentLoadNewActivity);
        }

第二次活动

ImageView iv;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_new);

    ImageView iv = (ImageView) findViewById(R.id.iv);

    Bitmap bitmap = (Bitmap) getIntent().getParcelableExtra("BitmapImage");

    iv.setImageBitmap(bitmap);

    }

【问题讨论】:

  • 到底在挣扎什么?从按钮获取当前随机图像,发送意图?请edit您的问题,澄清您到底在努力解决什么问题以及为什么并分享您当前代码的相关部分。
  • 目前无法提供,当我无法编辑时。现在请随时查看我重新编辑的问题。
  • 你的错误是什么?
  • 我的构建运行良好。但图像未显示在第二个活动的图像视图中。所以我想我错过了一些任务。
  • 你的图片来自哪里?可绘制?资产?外部的 ?你可以只传递资源 ID 或 url 吗?会更容易...

标签: android android-intent imagebutton


【解决方案1】:

我找到了适合我的解决方案。

第一次活动

    imageButton.setImageResource(images[pickedImage])

这指定了随机图像

    imageButton = (ImageButton) findViewById(R.id.imageButton);


    imageButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v){
            Intent intentLoadNewActivity = new Intent(MainActivity.this, NewActivity.class);
//this is basically the solution
            Bundle bundle=new Bundle();
            bundle.putInt("image",(images[pickedImage]));
            intentLoadNewActivity.putExtras(bundle);
            startActivity(intentLoadNewActivity);
        }

第二次活动

    ImageView iv;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_new);

    iv = (ImageView) findViewById(R.id.iv);

    Bundle bundle=this.getIntent().getExtras();
    int pic=bundle.getInt("image");
    iv.setImageResource(pic);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-01-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多