java - Is there any way to refactor this code or re-write it in a compact form? -


i'm working on android app produces image effects on image. below snapshot of app:

as can see, on bottom - there horizontal scrollbar, , user touches 1 of images on horizontal scrollbar, same effect applied on above image.

i've total of 26 image effects , therefore 26 images in horizontal scrollbar. now, in code, i've find images , set onclicklistener()'s 1 particular listener.

i'm accomplishing task in following way:

    sepiagreenishimage      = (imageview) findviewbyid(r.id.sepiagreenish);     embossimage             = (imageview) findviewbyid(r.id.emboss);     sharpenimage            = (imageview) findviewbyid(r.id.sharpen);     slightyellowishimage    = (imageview) findviewbyid(r.id.ligth_yellow);     slightbluishimage       = (imageview) findviewbyid(r.id.light_blue);     slightreddishimage      = (imageview) findviewbyid(r.id.light_red);     slightgreenishimage     = (imageview) findviewbyid(r.id.light_green);     negativeimage           = (imageview) findviewbyid(r.id.negative);     greyscaleimage          = (imageview) findviewbyid(r.id.greyscale);     tintseventyimage        = (imageview) findviewbyid(r.id.tint_at_70);     tintthirtyimage         = (imageview) findviewbyid(r.id.tint_at_30);     snowimage               = (imageview) findviewbyid(r.id.snow);     darkimage               = (imageview) findviewbyid(r.id.darken);     noiseimage              = (imageview) findviewbyid(r.id.noise);     flipimage               = (imageview) findviewbyid(r.id.flip);     rotateimage             = (imageview) findviewbyid(r.id.rotate);     gaussianblurimage       = (imageview) findviewbyid(r.id.blur);     reddishimage            = (imageview) findviewbyid(r.id.reddish);     bluishimage             = (imageview) findviewbyid(r.id.bluish);     greenishimage           = (imageview) findviewbyid(r.id.greenish);     blackfilterimage        = (imageview) findviewbyid(r.id.black_filter);     increasedsepiaimage     = (imageview) findviewbyid(r.id.increased_sepia);     spiabluishimage         = (imageview) findviewbyid(r.id.sepia_bluish);     brightimage             = (imageview) findviewbyid(r.id.brighten);     mirrorimage             = (imageview) findviewbyid(r.id.mirror);  

and following way, i'm setting onclicklistener:

    sepiagreenishimage.setonclicklistener(this);     embossimage.setonclicklistener(this);     sharpenimage.setonclicklistener(this);     slightyellowishimage.setonclicklistener(this);     slightbluishimage.setonclicklistener(this);     slightreddishimage.setonclicklistener(this);     slightgreenishimage.setonclicklistener(this);     negativeimage.setonclicklistener(this);     greyscaleimage.setonclicklistener(this);     tintseventyimage.setonclicklistener(this);     tintthirtyimage.setonclicklistener(this);     snowimage.setonclicklistener(this);     darkimage.setonclicklistener(this);     noiseimage.setonclicklistener(this);     flipimage.setonclicklistener(this);     rotateimage.setonclicklistener(this);     gaussianblurimage.setonclicklistener(this);     reddishimage.setonclicklistener(this);     bluishimage.setonclicklistener(this);     greenishimage.setonclicklistener(this);     blackfilterimage.setonclicklistener(this);     increasedsepiaimage.setonclicklistener(this);     spiabluishimage.setonclicklistener(this);     brightimage.setonclicklistener(this);     mirrorimage.setonclicklistener(this); 

now, question how can apply kind of refactoring? because i'm repeating myself lot. kind of foreach loop or similar thing might me lot!

to programmatically, like:

viewgroup buttongroup = (viewgroup) findviewbyid(r.id.image_effect_buttons); (int = 0; < buttongroup.getchildcount(); i++) {     imageview imageview = (imageview) buttongroup.getchildat(i);     imageview.setonclicklistener(this); } 

where "image_effect_buttons" id of whatever view (guessing linearlayout?) contains image buttons. note cause problems if viewgroup contains other children aside imageviews want attach listener to, it's simplest way avoid enumerating them explicitly.


Comments

Popular posts from this blog

c++ - Function signature as a function template parameter -

algorithm - What are some ways to combine a number of (potentially incompatible) sorted sub-sets of a total set into a (partial) ordering of the total set? -

How to call a javascript function after the page loads with a chrome extension? -