Android Live Wall Papers that change random pictures from drawable - android

Can someone give such a code or at least an idea how to make such wallpapers.
I guess I got to use Vieflipper but I cant use Random with this. Am I right? Thanks.

Your question looks a bit unspecific. In any case, to put it simple, let's assume you have an ImageView that you want to change dynamically in a random manner. The structure should look something like this:
imv = (ImageView)findViewById(R.id.imageView1);
Resources rsrc= getResources();
imid = R.drawable.yourimagename;
Drawable drawable= rsrc.getDrawable(imid);
Bitmap bm = BitmapFactory.decodeResource(rsrc,drawable);
imv.setImageBitmap(bm);
In this example, imid is hardcoded, but you could easily change it dinamically. For instance, create an array list of integers containing your desired images ids and then access them in a loop or whenever a condition is met (for instance every 30 seconds).
Eg:
ArrayList<Integer> listofimagesids = new ArrayList<Integer>;
listofimagesids.add(R.drawable.yourimagename1)
listofimagesids.add(R.drawable.yourimagename2)
...
listofimagesids.add(R.drawable.yourimagenameN)
Random rand = new Random();
if (youcondition){
int n = rand.nextInt(listofimagesids.size());
Drawable drawable= rsrc.getDrawable(n);
Bitmap bm = BitmapFactory.decodeResource(rsrc,drawable);
imv.setImageBitmap(bm);
}
Of course this is just pseudo-code, but it might guide you a bit. Hope it helps.

Related

Using frame by frame animation with Universal Image Loader

Is there any good way to use frame by frame animation AnimationDrawable using UIL library. AnimationDrawable accept only BitmpaDrawable . Is there any way to convert quickly to bitmaps my images or any maybe there is method like imageLoader.getBitmap, I haven't find anything like that.
Please help , I would be very grateful for any help .
Edit I have my images in assets folder. Maybe there any way to get bitmap from cache or something else . I need open new activity , maybe several times . I need to show animation from files , but if I use decode it takes a lot of time to decode them . Please suggest something
If your images are resources you can use this for obtain Bitmap:
Bitmap icon = BitmapFactory.decodeResource(context.getResources(),
R.drawable.icon_resource);
else, if you want donwload the image you can use this:
String name = c.getString(url);
URL urlAux = new URL(name);
ImageView prueba = (ImageView)v.findViewById(R.id.iv_prueba);
if (prueba != null) {
Bitmap bitmap =
BitmapFactory.decodeStream(urlAux.openConnection().getInputStream());
prueba.setImageBitmap(bitmap);
}
Hope it helps you.

Where is this drawable located?

I used the code from this answer, and compiled everything successfully.
However, I cannot find out how to use the drawable that I have just created. Here is my code:
Drawable dr2 = getResources().getDrawable(android.R.drawable.ic_menu_manage);
Bitmap bitmap2 = ((BitmapDrawable) dr2).getBitmap();
Drawable f = new BitmapDrawable(getResources(), Bitmap.createScaledBitmap(bitmap2, 256, 256, true));
private Integer[] menu_icon = {
android.R.drawable.f,
// I can't figure out how to call Drawable f
};
After converting the drawable to the Drawable f, I am unable to find out how to recall the resized drawable. I have tried using all possible locations in my code as to where it may be (/res/drawable and a few other locations). However, I am unable to compile no matter how I try to call Drawable f. Any help would be greatly appreciated!
(And no, I have eight elements in that array, but only copy+pasted one)
It exists only in memory, not anywhere on disk. You can't reference it by id. Instead, you need to call versions of functions that take a Drawable rather than ones that take an int resourceID.

Check two drawable images

All
I have created frame animation using Animation drawable. I have 25 images in that animation. Now I have to compare Image which is the current frame in animation with the one that is stored in the res/drawable folder.
How to compare this two drawables?? == and .equals methods wont work with the drawables.
Please give me reply as soon as possible.
you can provide id to the drawable image by using "imageview.setid" and try to compare that given id of images by using "imageview.getid"(use any int for id)
if You want to compare two drawable then use following code
i.e :
drawable1 = context.getResources().getDrawable(R.drawable.ic_launcher);
drawable2 = context.getResources().getDrawable(R.drawable.ic_launcher);
if (drawable1.getConstantState().equals(drawable2.getConstantState()))
{
//write your code.
} else {
//write your code.
}

Android Bytes to Bitmap

I have this working just fine in our iPhone app, but am having problems in Android. I'm using the same urls/data in both apps. When I set my image in my ListView to the bitmap that came from the bytes, the image doesn't appear. The data is there. Here is the code where I assign the view:
if (camera.snapshot != null)
{
bMap = BitmapFactory.decodeByteArray(camera.snapshot, 0, camera.snapshot.length);
image.setImageBitmap(bMap);
}
This is where I convert the string data into bytes:
camera.snapshot = responseData.getBytes();
The images are PNG files. They come in about 4 times the size that I need them for the listview image but I would think they would size perfectly to the bounds I set the ImageView to be.
On iPhone I simply use NSData and then use a prebuilt method in ImageView to turn it into an image. It works perfectly! What am I missing here?
You probably need to use the 4-argument version of decodeByteArray: see http://developer.android.com/reference/android/graphics/BitmapFactory.html#decodeByteArray%28byte[],%20int,%20int,%20android.graphics.BitmapFactory.Options%29.
The options would depend on the type of PNG image, so that you might need to experiment with. For a generic PNG, maybe something like this?
BitmapFactory.Options opt = new BitmapFactory.Options();
opt.inDither = true;
opt.inPreferredConfig = Bitmap.Config.ARGB_8888;
You can see http://developer.android.com/reference/android/graphics/BitmapFactory.Options.html and http://developer.android.com/reference/android/graphics/Bitmap.Config.html for more detail.
Everything is fine here. So you need to debug to try and find where else is the issue. i.e. is Camera.snapshot = null ? i.e. you might not be getting the data properly. Or there could also be an issue in the layouts to show the imageview. Try setting a predefined image to imageview and see if it is shown. This way you would be able to track the problem.

Set image / background source dynamically

is there any possibility to set background image dynamically?!
I try to explain what I mean.
String picVariable = getPictureFromServer();
ImageView image = (ImageView)v.findViewById(R.id.dynamic_image);
// I know, that doesn't work, but that's the way I looking for
image.setBackgroundResource(picVariable);
Thank you in advance,
Mur
Ps.
I also read this article. It would suggested in one answer, to use java reflection to get a field of the R class by name. But I've never used reflextion before. An example would be very helpfull
Sometimes I should take a bit more time for searching :)
I found the answer reading this article, and it works fine for me:
// The server says, it should be *.png
String picName = getPictureFromServer();
picName = picName.replace(".png", "");
Resources r = getResources();
int picId = r.getIdentifier(picName, "drawable", "com.mypackage.myapp");
ImageView image = (ImageView)v.findViewById(R.id.dynamic_image);
image.setBackgroundResource(picId);
ImageView does not have any background, but for other widget (like Button), you should use setBackgroundResource(int).
Sorry, I am not sure I read the question correctly... maybe your problem is just that you are trying to use it with a Widget that does not use background ?

Categories

Resources