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);
Related
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());
I'm pretty new in Android and I am trying to make a Mancala (it's kind of an African game) app. I am looking for a way to update an Image View from one image to another programmatically (for those who are familiar with the game- I want the images of the empty holes to update to images with balls every time the player plays).
I tried to look for an answer but I didn't find one. Here is what I've tried:
imgHome2=(ImageView)findViewById(R.drawable.home0);
You can do same by using Drawable:
private ImageView imageView;
on onCreate()
imageView = (ImageView) findViewById(imageView);
Create new method for set drawable on ImageView.
private void setDrawableOnImageView(Drawable drawable){
imageView.setImageDrawable(drawable);
}
How to call above method:
Drawable drawable = getResources().getDrawable(R.drawable.ic_launcher);
setDrawableOnImageView(drawable);
You can change drawable as your requirement.
in findViewById you should pass an id not a drawable
imgHome2=(ImageView)findViewById(R.id.myimageviewid)
and than you can change the image by
imgHome2.setBackground(R.drawable.home0);
I have an ImageView. I am using imageView.setImageBitmap to set my image as background to ImageView. But it sets my image to ImageView's source (i.e. android:src) , but it doesn't set my image to ImageView's background (i.e. android:background).
When I use imageView.setImageBitmap, it looks like as if I used imageView.setImageResource not imageView.setBackgroundResource.
How can I handle this if I want to set my image to background using imageView.setImageBitmap. I know by I can do this by making custom ImageView. But is it possible without custom ImageView? If its possible, please let me know how to do it.
I have tested and it's done
I think you are getting Bitmap
So you have to convert Bitmap to BitmapDrawable
like
BitmapDrawable ob = new BitmapDrawable(getResources(), bitmap)
then just set bitmap with below function
imageView.setBackground(ob);
by this way you can do it..
Try following code to set your own bitmap as background.
Bitmap bitmap = ...;
ImageView imageView = ...;
Drawable drawable = new BitmapDrawable(getResources(), bitmap);
imageView.setBackground(drawable);
try this..
Create drawable using bitmap & use setBackgroundDrawable(Drawable d) method for imageview.
Drawable d = new BitmapDrawable(getResources(),bitmap);
imageview.setBackgroundDrawable(d);
Please Use the following line to set image background.
imageview.setBackground(getResources().getDrawable(R.drawable.uporder));
Here uporder is an image resource present in your drawablefolder.
I prefer to use this because is not deprecated and it works in the lowers versions of android.
ImageView imageView;
Bitmap bitmap;
Drawable drawable = new BitmapDrawable(getResources(),bitmap);
imageView.setImageDrawable(drawable);
call setBackgroundDrawable withe BitmapDrawable param
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());
I have a bitmap:
Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.myimage);
I want it so when a user presses on the bmp, I get a trace message (I will add in what I need it to do)
Anyone have any ideas?
Actually, Bitmap is a picture that can't exist on your layout without any container. A container for a picture in Android is ImageView. So to make a clickable ImageView with your picture inside you should use:
ImageView imgView = (ImageView)findViewById(R.id.img);
imgView.setImageBitmap(bmp);
imgView.setOnClickListener(new View.OnClickListener());
Hope this helps.
It may be more appropriate to use an ImageButton rather than an ImageView. Although, you could implement the onTouchEvent and register a touch event listener on the main element that the Bitmap relies in. Or, register an onTouchListener for the ImageView.
You will have to set an ImageView's background Bitmap as bmp, and then set a click listener on the ImageView.
Take a look at ImageView's setImageBitmap method, and View.OnClickListener