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

时间:2016-02-17 12:28:32

标签: android android-studio

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

1 个答案:

答案 0 :(得分:0)

根据我的理解,您希望在按钮点击当前活动时更改下一个活动的颜色。所以,你能做的是:

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"));
  }

  ...
}

如果您有任何疑问,请与我联系。

<强>更新

使用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("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

您可以将每个图像按钮映射到一个可绘制的哈希映射。

e.g。

HashMap<Integer, Integer> mViewIdToDrawableMap = new HashMap<>();
mViewIdToDrawableMap.put(mImageButton1.getId(), R.drawable.image1);
mViewIdToDrawableMap.put(mImageButton2.getId(), R.drawable.image2);
mViewIdToDrawableMap.put(mImageButton3.getId(), R.drawable.image3);



public int getDesiredDrawable(View view){
    return mViewIdToDrawableMap.get(view.getId());
}

你将如何称呼这个功能:

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(view));
      mContext.startActivity(intent);
   }
});

现在,您的上一个问题是什么是rootLayout?

让我们说你想要展示这张图片的你的活动2像layyout那样有点像这样:

<RelativeLayout>

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

</RelativeLayout>

在你的Activity2的onCreate中,做这样的事情(在我之前解释过之后得到drawable):

//Here mBackgroundImageView is the background_imageview in your layout
Picasso.with(mContext).load(drawable).into(mBackgroundImageView);