Android着色应用程序多个背景图像

Android着色应用程序多个背景图像,android,android-studio,Android,Android Studio,我正在创建一个android studio着色应用程序,需要它,以便用户可以选择要着色的图像。我想让他们点击一个活动上的图像按钮,这会改变按钮带他们去的下一个活动的背景,但是我不知道怎么做。任何帮助都将不胜感激。谢谢据我所知,您希望在单击当前活动的按钮时更改下一个活动的颜色。因此,您可以做的是: button1.setOnClickListener(new View.OnClickListener{ @Override public void onClick(View view){

我正在创建一个android studio着色应用程序,需要它,以便用户可以选择要着色的图像。我想让他们点击一个活动上的图像按钮,这会改变按钮带他们去的下一个活动的背景,但是我不知道怎么做。任何帮助都将不胜感激。谢谢

据我所知,您希望在单击当前活动的按钮时更改下一个活动的颜色。因此,您可以做的是:

button1.setOnClickListener(new View.OnClickListener{
   @Override
   public void onClick(View view){
      Intent intent = new Intent(mContext, MyActivity2.class);
      //Implement getDesiredColor to get the color according to your logic
      intent.putExtra("color", getDesiredColor());
      mContext.startActivity(intent);
   }
});
在第二个活动的onCreate中

onCreate(...){
  ...

  View rootLayout = //Initialize root layout here
  Intent intent = getIntent();
  if(intent.hasExtra("color")){
    rootLayout.setBackgroundColor(intent.getExtra("color"));
  }

  ...
}
onCreate(...){
  ...

  View rootLayout = //Initialize root layout here
  Intent intent = getIntent();
  if(intent.hasExtra("drawable")){
    rootLayout.setBackground(intent.getExtra("drawable"));
    //Or if you are using ImageView in your root layout to set the background image (I'm using Picasso here):
    //Picasso.with(mContext).load(intent.getExtra("drawable")).into(myBackgroundImageView);
  }

  ...
}
如果你有任何疑问,请告诉我

更新

使用drawable,您的代码将如下所示:

button1.setOnClickListener(new View.OnClickListener{
   @Override
   public void onClick(View view){
      Intent intent = new Intent(mContext, MyActivity2.class);
      //Implement getDesiredDrawable to get the drawable according to your logic
      intent.putExtra("drawable", getDesiredDrawable());
      mContext.startActivity(intent);
   }
});
在第二个活动的onCreate中

onCreate(...){
  ...

  View rootLayout = //Initialize root layout here
  Intent intent = getIntent();
  if(intent.hasExtra("color")){
    rootLayout.setBackgroundColor(intent.getExtra("color"));
  }

  ...
}
onCreate(...){
  ...

  View rootLayout = //Initialize root layout here
  Intent intent = getIntent();
  if(intent.hasExtra("drawable")){
    rootLayout.setBackground(intent.getExtra("drawable"));
    //Or if you are using ImageView in your root layout to set the background image (I'm using Picasso here):
    //Picasso.with(mContext).load(intent.getExtra("drawable")).into(myBackgroundImageView);
  }

  ...
}
更新2

您可以使用一个hashmap将每个imagebutton映射为一个drawable

e、 g

现在,你的最后一个问题是什么是rootLayout

假设您要显示此图像的活动2有如下内容:

<RelativeLayout>

  <ImageView
    ...
    android:id="id+/background_imageview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:scaleType="fitXY"
    ...
  />

</RelativeLayout>

我在这里用了颜色,你可以用可画的来代替。更新了我的答案,改为使用drawable。是的,我正在尝试使用drawable,您正在准确地解释我想要做什么,但是我仍然有点不确定如何更改代码以适合我的项目。几个问题,按钮1是什么?同样使用行intent.putExtra(“color”,getDesiredColor());我不知道在这里该怎么办。在第二个活动中,我没有得到初始化根布局。对不起,谢谢!我已经更新了我的答案来帮助你们解决这些问题。现在,button1是您的“图像按钮”。并将更新您关于getDesiredDrawable()函数的问题的答案。我已尝试实现此功能,但我遇到了很多错误。我没有注册imageButton1或hashmap中的put。以及imageButton1,在第一个活动中查看并获得所需的绘图。在第二行中,它不接受行视图rootLayout或最后一行rootLayout
//Here mBackgroundImageView is the background_imageview in your layout
Picasso.with(mContext).load(drawable).into(mBackgroundImageView);