Set an imageview instance by another one - android

Here is my code :
ImageView iv1 = (ImageView) findViewById(R.id.image1);
ImageView iv2 = (ImageView) findViewById(R.id.image2);
I would like to know if it's possible to set the R.id.image1 to the same as R.id.image2 dynamically.
R.id.image1 contains no image but if R.id.image2 got an image I would like that R.id.image1 display the same image as R.id.image2

Anything under R is defined at compile time based off of your resources. (Layouts, images, dimens, etc.) That means that you can't change what R.id.image1 means at runtime but you can have multiple ImageViews point to the same resource:
ImageView image1 = (ImageView)findViewById(R.id.image1);
ImageView image1Dup = (ImageView)findViewById(R.id.image1);
Alternatively you can set the image from one ImageView to another one.
image1.setImageDrawable(image2.getDrawable());

Related

How to change imageview content between color and image programmatically?

I'm trying to create a program where when i click on an image view i switch the content of it between a color and an image. I tried this 2 controls: setBackgroundCOlor and setColorFilter but if the imageview is already showing an image the first one will set the color of a layer behind the image, and the second one will color all the image with that color, so the next times i will not see the image anymore.
I used this code lines:
these for the background
ImageView imagecolor= (ImageView) findViewById(R.id.imageView);
imagecolor.setBackgroundColorsetColorFilter(Color.BLACK, PorterDuff.Mode.MULTIPLY);
and these for the colorfilter
ImageView imagecolor= (ImageView) findViewById(R.id.imageView);
imagecolor.setColorFiltersetColorFilter(Color.BLACK, PorterDuff.Mode.MULTIPLY);
to set the imageview i used this code
ImageView imagecolor= (ImageView) findViewById(R.id.imageView);
imagecolor.setImageResource(R.drawable.image);
I was wondering if there is a control that allows me to work with images and colors on the same layer...
If you want to have your bitmap replaced with solid color, just do ordinary setImageResource(), i.e.
iv.setImageResource(Color.BLACK);

Displaying bitmaps

I need help with displaying a bitmap. I am using a class which is extending Activity. Where would I put this?
Mybitmap = BitMapfactory.decode resource(getresource,R.drawable.bit);
Also how would I draw it on the screen?
you should use ImageView...
put ImageView in you layout with "myImage" id and in activity do like this :
ImageView mImg = (ImageView) findViewById(R.id.myImage);
mImg.setImageBitmap(Mybitmap);

UIImage on Android

I'm migrating an iOS application to Android.
On iOS I have an UIImage object and I don't know which object can I use on Android. I thought I can use a Drawable.
What do you recommend me?
iOS equal to UIImageView in android is ImageView.
PROGRAMATICALLY DOING IT
ImageView imgView = new ImageView(this);
//setting image position and there are other properties to position ur imageview
imgView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));
//adding view to your layout(linear, relative....etc)
yourLayout.addView(imgView);
//Add this is how you set your Image
this.imgView.setImageBitmap(localBitmap);
IF YOU ARE USING XML FILES
//Get the reference of imageview
imgView = (ImageView) findViewById(R.id.imageView1);
//here you add your image
imgView.setImageResource(R.drawable.yourImage);

Using multiple imageview from one source

I have one Imageview in my activity_main.xml and I'd like to draw two from this in a different position. I tried this without succes:
image = (ImageView)findViewById(R.id.imageView1);
image2 = (ImageView)findViewById(R.id.imageView1);
LayoutParams params = (LinearLayout.LayoutParams) image.getLayoutParams();
LayoutParams params2 = (LinearLayout.LayoutParams) image2.getLayoutParams();
params.topMargin = 50;
params.leftMargin = 50;
image.setLayoutParams(params);
params2.topMargin = 100;
params2.leftMargin = 100;
image2.setLayoutParams(params2);
It draws just one piece. I also tried the setImageBitmap:
image2.setImageBitmap(((BitmapDrawable)image.getDrawable()).getBitmap());
How should I solve this?
You are only seeing one ImageView because there is only one ImageView in your layout. Both image and image2 reference the same id (R.id.imageView1) in your layout:
image = (ImageView)findViewById(R.id.imageView1);
image2 = (ImageView)findViewById(R.id.imageView1);
You can solve this 2 ways as I see it:
Inflate your ImageViews one at a time and add them to your layout at runtime. This is a bit more complicated for what you are trying to do.
Add another ImageView to your xml layout (set the id to R.id.imageView2) with the appropriate margins you want. Then just reference each ImageView in code and edit them separately.
Solution #2 would look something like this in your code (after adding a 2nd ImageView to your xml layout):
image = (ImageView)findViewById(R.id.imageView1);
image2 = (ImageView)findViewById(R.id.imageView2);
You can't re-use the same ImageView like that. You need to add another ImageView to your layout xml file:
<ImageView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="#+id/imageView2" />
and then change this line:
image2 = (ImageView)findViewById(R.id.imageView1);
to this:
image2 = (ImageView)findViewById(R.id.imageView2);
also on a side note, you should avoid using names like imageView1 and imageView2. Those are not descriptive at all, they make it harder to understand what it is that your code is actually doing. Consider using variable names that are more descriptive of what the variable does. It will make your code easier to understand and maintain.

setImageResource for an ImageView from another ImageView

Strange request here, but is there any way you set an imageview to hold the same drawable as another imageview by getting it from the original imageview. what i mean is:
ImageView image1 = new ImageView(this);
image1.setimageResource(R.drawable.blah1);
ImageView image2 = new ImageView(this);
//now i want something like this, which doesn't work. i want to get it from image1
image2.setImageResource(image1.getDrawable());
so please how can i achieve this from image1?.. Thank you :)
Use setImageDrawable instead of setImageResource
For setting Image Resources from another image view
You need to use two methods of ImagView
setImageDrawable()
getDrawable()
Your code is all perfect except one error
You need to use setImageDrawable() in place of setImageResource()
following is your code implemention using above methods with correction
ImageView image1 = new ImageView(this);
image1.setimageResource(R.drawable.blah1);
ImageView image2 = new ImageView(this);
// just change this to setImageDrawable
image2.setImageDrawable(image1.getDrawable());

Categories

Resources