I am trying to build an Android game in which a user will have choice to select a player from 4 available players. These Players are available as Image Buttons. I want that when a user clicks a image Button, this image will be assigned to a Bitmap variable 'PLAYER' in the Main Activity.java .
Please help me out with codes to extract the Image from clicked button and assign it to the variable PLAYER.
Any help would be grateful. Thanks!!
yor can get bitmap form Imageview by using
Drawable dra = imageview.getDrawable();
BitmapDrawable bmp = (BitmapDrawable) dra;
Bitmap b = bmp.getBitmap();
and set this bitmap to your player object
method of image view.
Related
here's my problem... Would it be possible to load a picture, call the crop activity, and load the cropped images on different imageviews without changing the main image??
I have 3 imageviews. My imageview 1 is for the main image loadout, my imageview 2 is for the result of the crop activity, the same goes with imageview 3.
I am using com.theartofdev.edmodo:android-image-cropper:2.6.0....
The good thing about this one is that it already has lots of functions, but the problem is that what I want to happen is that it would load the image that I have already called in my main imageview.
Get the instances of the Views in the activity class.
Use the method below to get the image source to bitmap
Display bm returned from convertImageViewToBitmap to the second ImageView.
private Bitmap convertImageViewToBitmap(ImageView v){
Bitmap bm =
((BitmapDrawable)v.getDrawable()).getBitmap();
return bm;
}
I'm downloading the image from server and storing it in bitmap object. I want to set this image as background for button. But the button doesn't have the property setImageBitmap. So is there anyway I can set the background of button with the downloaded bitmap image? Like by converting the bitmap to drawable? Sorry, I'm new to Android, please bear with my mistakes (if any).
P.S : I want to use button control only. Because I want some text at the bottom of each buttons and I'm creating these buttons dynamically.
The best way to convert a Bitmap to drawable in android is as follows,
Drawable drawable = new BitmapDrawable(getResources(), bitmap);
where bitmap is the name the Bitmap. Then set the drawable as your Button background as follows,
btn.setBackground(drawable);
N.B: Without specifying getResources() as the first argument, you may experience inconsistent image sizing across different screen densities.
for more info refer this
http://developer.android.com/reference/android/graphics/drawable/BitmapDrawable.html
Simply use BitmapDrawable.
Drawable drawable=new BitmapDrawable(contact_pic);
Convert Bitmap to BitmapDrawable like this
Drawable drawable = new BitmapDrawable(getResources(),your_bitmap);
and then set it as background to button as ,
button.setBackgroundDrawable(drawable);
P.S. You can also do the same inline as ,
button.setBackgroundDrawable(new BitmapDrawable(getResources(),your_bitmap));
This is how to do it, I've used several times:
Drawable drawable = (Drawable)new BitmapDrawable(Bitmap);
Button button= new Button(this);
button.setBackground(drawable);
In my app there is an image that the user imported using the gallery
then doen the imageview where the picture has been inserted
there is two seekbars
the first seekbar controls the picture brightness
and the other controls the picture hue
when I use them they are working, but the problem is they don't save
like ..
I imported the picture then made its brightness its working great
but when I set the picture hue the brightness that i made disappears
and the hue starts
isn't there
iv.save;
or anything like that?
note*
final ImageView iv = (ImageView) findViewById(R.id.picture);
Yes there is.
final ImageView iv = (ImageView) findViewById(R.id.picture);
iv.setDrawingCacheEnabled(true);
Bitmap bmp=iv.getDrawingCache();
Now you can save this Bitmap to your sdcard in jpg or png format.
I have a imageview and a camera button in Activity A. By clicking on the camera button will go to my custom camera activity. After taking a picture and storing it into a folder, how can I reflect this newly picture taken in the imageview after finishing the camera activity? Any help will be appreciated ?
A(main) > B(camera activity) > A(main) Imageview is updated with new picture taken.
Thanks.
According to my understanding of your question, you want to know how to set an image, residing on sdCard, to an ImageView. Well, let's say you can get the image path from your camera activity and then you can do something like below:
String imageInSD = "/sdcard/img.PNG";
Bitmap bitmap = BitmapFactory.decodeFile(imageInSD);
ImageView myImageView = (ImageView)findViewById(R.id.imageview);
myImageView.setImageBitmap(bitmap);
I hope it'll serve the purpose, if this is what you want.
I want to be able to set the background of an image button in an xml file to be the picture the user took on the phone.
I have the picture set as cameraBitmap.
cameraBitmap = (Bitmap) intent.getExtras().get("data");
and I want to pass that picture to be the bakground of the imagebutton. The problem is, the method: setBackgroundDrawable() only can pass in a drawable. How do I go about changing that Bitmap to a drawable picture and making it the background?
I currently have:
private ImageButton theImageButton;
theImageButton.setBackgroundDrawable(cameraBitmap);
If you are using ImageButton why can't you use this
theImageButton.setImageBitmap(cameraBitmap)