Copy Bitmap contents of one ImageView to anoher - android

This has me baffled. I need to copy the Bitmap from one ImageView into another. I do not want to simply copy one ImageView to another because I need to do some changes to the bitmap on its way over.
Here is some code that doesn't work.
ImageView ivSrc = (ImageView) findViewById(R.id.photo);
ivSrc.setDrawingCacheEnabled(true);
Bitmap bmSrc1 = ivSrc.getDrawingCache(); // will cause nullPointerException
Bitmap bmSrc2 = Bitmap.createBitmap(ivSrc.getDrawingCache());//bmSrc2 will be null
View vSrc = (View) ivSrc.getParent();
vSrc.setDrawingCacheEnabled(true);
Bitmap bmSrc3 = Bitmap.createBitmap(vSrc.getDrawingCache()); //black bitmap
//To test the bitmaps:
ImageView ivDest = (ImageView) findViewById(R.id.photo2);
ivDest.setImageBitmap(bmSrc1); //bmSrc1, 2, 3 results shown above
I have to going about this wrong because doing a copy should be so easy. TIA

Not used the drawing cache, but wouldn't you need to call buildDrawingCache() ?
The way I'd do it:
Bitmap bmSrc1 = ((BitmapDrawable)ivSrc.getDrawable()).getBitmap();
Bitmap bmSrc2 = bmSrc1.copy(bmSrc1.getConfig(), true);
Note that bmSrc2 is mutable, i.e. you can stick it in a Canvas and do whatever you like with it before drawing it somewhere.

Related

Swaping images of two imageView not workng properly in android

I have made a demo for filmstrip in Android. I have taken 9 ImageViews in a LinearLayout, set images to them. However, I am getting an old bitmap while swapping the second time. What am I missing?
void swap(ImageView iv1, ImageView iv2) {
iv1.buildDrawingCache();
Bitmap b1 = iv1.getDrawingCache();
iv2.buildDrawingCache();
Bitmap b2 = iv2.getDrawingCache();
iv2.setImageBitmap(b1);
iv1.setImageBitmap(b2);
tempView.clear();
iv_image_1.postInvalidate();
iv_image_2.postInvalidate();
iv_image_3.postInvalidate();
iv_image_4.postInvalidate();
iv_image_5.postInvalidate();
iv_image_6.postInvalidate();
iv_image_7.postInvalidate();
iv_image_8.postInvalidate();
iv_image_9.postInvalidate();
}

How to get the drawable from relative layout in android

What i have done:
I have set the bitmap for a relative layout as below
RelativeLayout root;
root= (RelativeLayout) itemView.findViewById(R.id.root);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 4;
Bitmap preview_bitmap = BitmapFactory.decodeResource(context.getResources(),godImages[position], options);
Drawable drawable = new BitmapDrawable(context.getResources(),preview_bitmap);
root.setBackgroundDrawable(drawable);
What i am trying to do:
I am trying to get the bitmap from the relativelayout back again that was set
so that i can pass to another activity.
Note: i am aware of using the intents to padd data between activities only i want to know how to get the bitmap
Any ideas on How to achieve this ?
There are a couple of ways you can do this:
1. Try:
RelativeLayout layout = (RelativeLayout)findViewById(R.id.rel);
layout.setDrawingCacheEnabled(true);
bitmap = Bitmap.createBitmap(layout.getDrawingCache());
layout.setDrawingCacheEnabled(false);
2. Or try:
RelativeLayout layout = (RelativeLayout)findViewById(R.id.rel);
Bitmap b = Bitmap.createBitmap( layout.getLayoutParams().width, layout.getLayoutParams().height, Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(b);
layout.layout(layout.getLeft(), layout.getTop(), layout.getRight(), layout.getBottom());
layout.draw(c);
To get the current ImageView
1. To get current position, use
ViewPager viewPager = getView().findViewById(r.id.pager);
int position = viewPager.getCurrentItem();
2. To get the current View, use
View currentView = viewPager.getChildAt(int position);
This currentView is the View created by the onCreateView() method of the current Fragment.
3. To get the ImageView, use
ImageView currentImage = (ImageView)currentView.findViewById(R.id.image);
where R.id.image is the ImageView object you want from the current Fragment.

What is quick way to check if ImageView has bitmap assigned?

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);

Call to `getDrawingCache` returns null when scroll is enabled

What is missing in this code? This same code works on ICS. On API 8 the scroll appears and some content goes out of screen. How to get the drawing cache in this case?
Code:
TableLayout page = (TableLayout) findViewById(R.id.page);
page.setDrawingCacheEnabled(true);
page.buildDrawingCache();
// getDrawingCache returns null...
Bitmap pageBmp = Bitmap.createBitmap(page.getDrawingCache(true));
page.destroyDrawingCache();
page.setDrawingCacheEnabled(false);
I solved it. Created a bitmap of view size and drew the view into it.
TableLayout page = (TableLayout) findViewById(R.id.page);
Bitmap pageBmp = Bitmap.createBitmap(page.getWidth(), page.getHeight(),
Config.ARGB_8888);
Canvas canvas = new Canvas(pageBmp);
page.draw(canvas);
Used the pageBmp bitmap..

Android - setBackgroundResource on Button

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);

Categories

Resources