简体   繁体   English

更改背景图像的颜色

[英]Change the color of a background Image

I have a plain white background with a few clouds on it. 我的背景是纯白色,上面有几朵云。 When the user clicks an object on screen the white part of the background image changes color. 当用户单击屏幕上的对象时,背景图像的白色部分会更改颜色。 The clouds remain untouched by the color change. 颜色变化不会影响云层。

Until now I had been making each individual backgroud image in photoshop and loading them into the onclick method. 到目前为止,我一直在photoshop中制作每个单独的背景图像,并将它们加载到onclick方法中。 But now I'm wondering is it possible to have just one background image and when the user clicks an object android can change the background color of the image using the predefined colors.xml file? 但是现在我想知道是否可能只有一个背景图像,并且当用户单击一个对象时,android是否可以使用预定义的colors.xml文件更改图像的背景颜色?

Have an image for cloud and make the background color for your layout to White color. 为云生成图像,并将布局的背景色设置为白色。 When the user clicks object have a listener on the view to listen click and change the color of layout dynamically. 当用户单击对象时,视图上会有一个侦听器,以侦听单击并动态更改布局的颜色。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/YourPicture"
    android:id=@+id/root_layout>
 <-- your Views-->
 </LinearLayout>

Your Activity file: 您的活动文件:

yourView.setOnClickListener(v -> {
 rootLayout.setBackground(ContextCompat.getDrawable(context, R.drawable.draw));
 });

It's depends how did you draw the image. 这取决于您如何绘制图像。 If the background you want to change is transparent you can do: 如果要更改的背景是透明的,则可以执行以下操作:

imageView.setBackgroundColor(color)

不,我认为不可能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM