2

I'm trying to learn Titanium mobile development.

I have an image of a black star on a transparent background and I wanted to write an app that randomises its color on touch.

I saw that there are some "blend mode" features, but they are iOS only. Is there a way to do this that works across iOS and Android?

Many thanks

Chris
  • 1,501
  • 17
  • 32

4 Answers4

2

No you simply can't change the color of an image in Titanium. If you want to achieve your goal i would simplify the shape for example take a circle. To draw a circle use this code:

var circle = Ti.UI.createImageView({
        width: 100,
        height: 100,
        borderRadius: 50,
        backgroundColor: 'black'
});

To change it's color use this event listener:

circle.addEventListener("click", function(e){
    circle.backgroundColor = "red";
});

This will only change the color to red on the first click. If you want to randomize the colors you'll have to create an array with colors in it. Then use a shuffle function to get a random color to eventually set it. This article might be helpfull: shuffle

Good luck!

Community
  • 1
  • 1
Skoempie
  • 1,171
  • 10
  • 18
  • Thanks, but I'm really looking for a solution that works on an image, not just a view. Is there a way I can read the image data into a blob, then manipulate it with an image library, then write it back to an image and display in an image view? It can't be impossible. – Chris Dec 03 '14 at 23:36
0

May be these two links helpful for you.

Get Pixel Info

image to pixel

Krishna
  • 46
  • 6
0

I see some options to solve this kind of problem (you'll have to test them):

  1. One of the great things about Titanium is that it allows you to write your own custom native modules that you can have as part of your project. You can even write your own UI components - so basically anything you can do with native - you can theoretically do with Titanium.

  2. Assuming you don't want to work with native code, and want to stick to just what Titanium offers is playing with overlays like in the following example: https://developer.appcelerator.com/question/143156/placing-a-transparent-imageview-on-top-of-another-imageview

  3. Another way might be having a view with a background color (which you will randomize) and on top of it an image where everything that's surrounding the star has a solid color, and the star shape is transparent.

developer82
  • 13,237
  • 21
  • 88
  • 153
0

In the end, I used a HTML canvas element inside a webview to solve my problem.

I wrote a node package for breaking images into regions and rendering them to a canvas.

https://www.npmjs.com/package/regioned-image

Chris
  • 1,501
  • 17
  • 32