Is there a way to generate a drawable object from a layout?
In fact, I need a cropped part of my initial layout, and my idea is to transform the layout into a drawable object and then to crop drawable.
A simple version:
Bitmap snapshot = null;
Drawable drawable = null;
yourView.setDrawingCacheEnabled(true);
yourView.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_LOW); //Quality of the snpashot
try {
snapshot = Bitmap.createBitmap(yourView.getDrawingCache(), sizes and stuff); // You can tell how to crop the snapshot and whatever in this method
drawable = new BitmapDrawable(snapshot)
} finally {
yourView.setDrawingCacheEnabled(false);
}
Related
How to get ImageView src programmatically and set it in another ImageView
Drawable drawable = imageViewA....?
imageViewB.setImageDrawable(drawable);
You can do something like this:
Drawable drawable = imageViewA.getDrawable();
if(drawable != null){
imageViewB.setImageDrawable(drawable);
}
You can use setImageResource(int)
imageView.setImageResource(R.drawable.bg_image);
Instead of get drawable you do...
Bitmap bitmap = ((BitmapDrawable)image.getDrawable()).getBitmap();
and set to new image
newImageView.setImageBitmap(bitmap);
I have problem when I try to set an image to a ImageView browsed from assets folder. When I place the same image in the drawable folder and use imageView.setImageResource(R.id.image); it is larger and fits the screen better than the image browsed by the previous method. Is there a way to resize the image so that it fits the screen exactly the same way as usual.
Here is the layout file and the code in java.
AssetManager manager = getActivity().getAssets();
InputStream input = null;
try {
input = manager.open("images/" + mQuestion.getQImage() + ".png");
} catch (IOException e) {
e.printStackTrace();
}
Bitmap image = BitmapFactory.decodeStream(input);
ImageView qImageView = (ImageView) questionView.findViewById(R.id.ImageView_qImage);
qImageView.setImageBitmap(image);
<ImageView
android:id="#+id/ImageView_qImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="#dimen/questionTopBottomMargin"
android:background="#color/radioButtonStrokeColor"
android:contentDescription="#string/hello_world"
android:padding="1px"
android:scaleType="fitXY" />
When you place an image in drawable folder then the images are classified on density of screen while if you put images in assest folder then android need to calculate density.
If you want to use images from assest folder then this code will help you.
Options opts = new BitmapFactory.Options();
opts.inDensity = DisplayMetrics.DENSITY_HIGH;
drawable = Drawable.createFromResourceStream(context.getResources(), null, is, srcName, opts);
see here
Image loaded from assets folder comes in different size than res/drawable
Android: Accessing images from assets/drawable folders
If you place images in the drawables folder they will be scaled by some functions it they load them from resources. This will not happen with images in assets folder which you load yourself.
Have a look at android docs
You should always use images from the drawable folder
There are different drawable folders in android
(drawable-hdpi,drawable-mdpi,drawable-ldpi)
Android will decide which folder image should be drawn depending on
the phone requirements and resolution
Use 9-patch image for auto-scaling
When you load from assets, the "source" density is unknown, so this
autoscaling is not performed. Hence, there is difference.
You just define the imageView width and height in the xml layout then you should try this code it works perfectly for me
public Bitmap getImageBitmap(String imgName){
AssetManager mgr = getAssets();
InputStream inputStream = null;
try {
inputStream = mgr.open(imgName + ".png");
return BitmapFactory.decodeStream(inputStream);
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
ImageView imageView = (ImageView) findViewById(R.id.imageView1);
Bitmap bitmap = getImageBitmap(imageName);
imageView.setImageBitmap(bitmap);
I need to quickly and safely check if an android.widget.ImageView currently has a Bitmap of non zero value attached. What is a quick way to do this.
Update my image view is set initially drawable set to "logo.png" so if I know that it is currently set to logo.png than I know that the bitmap has not yet been set. So how can I check if background drawable is currently logo.png
Thanks
Use getDrawable() method, if it return null then nothing assigned
if( mImageView.getDrawable() != null){
//Image Assigned
}else{
//nothing assigned
}
Assign ImageView to variable in onCreate:
someImage = (ImageView) findViewById(R.id.imageView);
Then a simple if-else statement:
Bitmap bm = ((BitmapDrawable) someImage.getDrawable()).getBitmap();
boolean hasDrawable = (bm != null);
if(hasDrawable)
{
hasImage();
}
else
{
Toast.makeText(AppSettings.this, "No Bitmap", Toast.LENGTH_SHORT).show();
}
Kotlin Extension Solution:
Add this to simplify checks and make it more readable
fun ImageView.hasDrawable() = drawable != null
and then call
myView.hasDrawable()
Get Bitmap attached to ImageView
Check this out.
It looks like you have to create a bitmap from the ImageView and check to see if it is null.
imageView.setDrawingCacheEnabled(true);
imageView.buildDrawingCache(true);
Bitmap bitmap = Bitmap.createBitmap(imageView.getDrawingCache());
imageView.setDrawingCacheEnabled(false);
I have some buttons which i set the background with setBackgroundResource(R.drawable.cal_box_center); and the problem I'm having is that my background is a gradient which has this banding effect (annoying) and I've read that in order to remove this you'll need to set the Bitmap.Config.ARGB_8888. I looked into the API and the way to do this is to use decodeStream etc, but how can i use setBackgroundResource and still set the Config to ARGB_8888?
Thanks in advance.
You can use this code snippet:
// create button
Button btn = new Button(getApplicationContext());
//decode the resource(You can also use decodeStream and other decode method of
//BitmapFactory)
Bitmap btm = BitmapFactory.decodeResource(getResources(), R.drawable.cal_box_center);
//create another copy of your bitmap and specify Config
Bitmap newBtm = btm.copy(Bitmap.Config.ARGB_8888, true);
//use your newBtm to create a BitmapDrawable
BitmapDrawable btmDrwble = new BitmapDrawable(newBtm);
// finally set the drawable as your button's background
btn.setBackgroundDrawable(btmDrwble);
I have a RelativeLayout object and want to dynamically change the background image with a dynamically created Bitmap object (it changes its color dynamically).
I saw that when I wanted to update the background image of the RelativeLayout object that I can only choose setBackgroundDrawable() which requires a Drawable object as a parameter.
My question is, how can I convert the dynamically created Bitmap object into a Drawable object?
BitmapDrawable(obj) convert Bitmap object into drawable object.
Try:
RelativeLayout relative = (RelativeLayout) findViewById(R.id.relative1);
Drawable dr = new BitmapDrawable(bit);
(view).setBackgroundDrawable(drawable);
I hope this will help you.
You can do it by this way
Drawable drawable = new BitmapDrawable(bitmap);
RelativeLayout r;
r = (RelativeLayout) findViewById(R.id.relativelayout1);
ll.setBackgroundDrawable(drawable);
Try this,
Drawable drawable = new BitmapDrawable(bitmap);
RelativeLayout rl=(RelativeLayout) findViewById(R.id.main1);
Bitmap myImage = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
Drawable dr = new BitmapDrawable(myImage);
rl.setBackgroundDrawable(dr);
Drawable d = new BitmapDrawable(bitmap);
//Create Reference for RelativeLayout
RelativeLayout layout = (RelativeLayout) findViewById(R.id.rl);
//set Background to layout reference using the following method
Drawable drawable = getResources().getDrawable(R.drawable.bg);
layout.setBackground(drawable);
Note: R.id.rl is id of RelativeLayout
R.drawable.bg id the Image in drawable folder