Swaping images of two imageView not workng properly in android - 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();
}

Related

Gif image is not working in nexus one device

I am using gif image in my application. It is not working in nexus one device.
View view = getLayoutInflater().inflate(layout, null);
ImageView imageView = (ImageView view.findViewById(R.id.img_view_id);
GifAnimationDrawable big = new GifAnimationDrawable(getResources().openRawResource(R.raw.flights_loading_loader));
big.setOneShot(false);
imageView.setImageDrawable(big);
big.setVisible(true, true);
mProgressDialog.setContentView(view);

How to remove the memory for dynamically added images in android

I am adding dynamically 20 images to the fragment . Here problem is when i destroy fragment and reloading, the memory was not deleting at destroy time and adding new memory for again 20 images.
my code is:
for(int i=0;i<machImagesCursor.getCount();i++)
{
machImagesCursor.moveToPosition(i);
ImageView imageView=new ImageView(getActivity());
imageView.setId(i);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
100, 75);
imageView.setLayoutParams(layoutParams);
String imageName=machImagesCursor.getString(machImagesCursor.getColumnIndex("M_ImageName"));
String path = Environment.getExternalStorageDirectory().getPath()
+ "/fleetsyncimages/" +imageName ;
Log.d("image-path",path);
imageView.setImageURI(Uri.parse(path));
hScrollView.addView(imageView);
}
at onDestroy
for(int i=0;i<hScrollView.getChildCount();i++)
{
ImageView iv=(ImageView)hScrollView.getChildAt(0);
// iv.setImageBitmap(null);
Drawable d = iv.getDrawable();
((BitmapDrawable) d).getBitmap().recycle();
}
In my logcat: the heap is keep on increasing.
Thanks In advance.
Not sure if the onDestroy() code is necessary, but
ImageView iv=(ImageView)hScrollView.getChildAt(0);
doesn't seem correct, you should get child i, instead of 0 all the time.
Are you using any other technique to save images? Like store them in an array in onRetainNonConfigurationInstance(). Drawables hold a reference to the current Context, so you could leak your whole context by keeping Drawables between Activity instances.

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..

Displaying random images

im novice to android.For displaying random images i used arraylist.
ArrayList<Integer> List = new ArrayList<Integer>();
for(int i=0;i<10;i++)
{
List.add(i);
System.out.println("Random Images"+List.add(i));
}
Collections.shuffle(List);
for(int i=0;i<10;i++){
imageArr[i] = (ImageView)findViewById(id[List.get(i)]);
System.out.println("Display Images"+List.get(i));
}
}
it is running correctly in logcat but what should do to display images on emulator screen. Pls Suggest
You will need an ImageView to display Images on the Screen.
You can display drawable, bitmaps, ...
The easiest way to do this create your main.xml like this:
<ImageView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/myImageView" />
Then call in your code (maybe in the onCreate):
ImageView imgView = (ImageView) findViewById(R.id.myImageView);
Then make a Drawable Array or an ArryList with bitmaps, whatever.
Get a Random value (like Math.random()) and fetch a random image from array or arraylist like
Drawable drawable = drawableArray[YOURRANDOMNUMBER];
and set the Drawable to the imageview.
imgView .setImageDrawable(drawable);
Hope this helps :)
Give this a read and see if it helps you: http://www.higherpass.com/Android/Tutorials/Working-With-Images-In-Android/

Copy Bitmap contents of one ImageView to anoher

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.

Categories

Resources