I have an animated *.svg file. I cannot find a way to play it in my android activity. I also need to know when the loop is over.
how to achieve this?
You could use https://shapeshifter.design/ in order to create an AnimatedVectorDrawable from plain SVG (see: https://developer.android.com/reference/android/graphics/drawable/AnimatedVectorDrawable)
After creating the drawable and including it to the Assets Folder you can load the drawable and assing it to an ImageView, which is defined in your xml Layout.
See the following code:
final AnimatedVectorDrawableCompat animatedVectorDrawableCompat =
AnimatedVectorDrawableCompat.create(getApplicationContext(),
R.drawable.animatedvector);
ImageView imageView = findViewById(R.id.imageView);
imageView.setImageDrawable(animatedVectorDrawableCompat);
final Animatable animatable = (Animatable) imageView.getDrawable();
animatable.start();
R.drawable.animatedvector is the AnimatedVectorDrawable you want to load, while R.id.imageView is the ImageView, which should show the AnimatedVectorDrawable.
Hope this helps ;-)
Related
am using sticker view in my project . it's a library that extend imageView and let you move and rotate image on the screen
https://github.com/nimengbo/StickerView
my problem is that i can't set color filter for images , i have tested every thing
such as
mCurrentView.setColorFilter(getResources().getColor(R.color.red_alpha_95), PorterDuff.Mode.MULTIPLY);
mCurrentView.getDrawable().setColorFilter(getResources().getColor(R.color.red_alpha_95), PorterDuff.Mode.MULTIPLY);
or even making a new drawable from resource and add that drawable to image view ;
i will be greauful if you have any experience with setColorFilter issue or know another lib or approach to move image view on the screen and share it with me
I'm use tint using compat support lib.
int color = ContextCompat.getColor(context, R.color.red_alpha_95);
Drawable drawable = mCurrentView.getDrawable();
Drawable wrap = DrawableCompat.wrap(drawable);
DrawableCompat.setTint(wrap.mutate(), color);
I'm pretty new in Android and I am trying to make a Mancala (it's kind of an African game) app. I am looking for a way to update an Image View from one image to another programmatically (for those who are familiar with the game- I want the images of the empty holes to update to images with balls every time the player plays).
I tried to look for an answer but I didn't find one. Here is what I've tried:
imgHome2=(ImageView)findViewById(R.drawable.home0);
You can do same by using Drawable:
private ImageView imageView;
on onCreate()
imageView = (ImageView) findViewById(imageView);
Create new method for set drawable on ImageView.
private void setDrawableOnImageView(Drawable drawable){
imageView.setImageDrawable(drawable);
}
How to call above method:
Drawable drawable = getResources().getDrawable(R.drawable.ic_launcher);
setDrawableOnImageView(drawable);
You can change drawable as your requirement.
in findViewById you should pass an id not a drawable
imgHome2=(ImageView)findViewById(R.id.myimageviewid)
and than you can change the image by
imgHome2.setBackground(R.drawable.home0);
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);
I have an ImageView. In its onClick I get its Drawable:
Drawable dr = ((ImageView) v).getDrawable();
And set it to a dialog's ImageView:
zoomedImage.setImageDrawable(dr);
But when I close the dialog or the activity is resumed. The image at the original position gets stretched and is shown larger than its size, leading to only a portion of the image is visible in the ImageView.
Is this a case of deep copy or there is another problem?
If it is, how can do I deep copy the original Drawable so that I could set the copy to zoomed image?
Thanks in advance.
Finally I succeed!
I had similar problem, when I used color filter on my drawable it changed the drawable, its very close to the solution of the other people here, but only this worked for me:
Drawable drwNewCopy = dr.getConstantState().newDrawable().mutate();
I managed to copy the drawable using following code:
drawable.mutate().getConstantState().newDrawable();
Here mutate() makes the drawable mutable to avoid sharing its state, and getConstantState().newDrawable() creates a new copy.
Thus different ImageViews use different drawables and there's no stretching.
Use BitmapFactory to convert the drawable into bitmap separately make or perform changes on it.
The above solutions won't work for me, But it works
val myDrawable = DrawableCompat.wrap(view.background).mutate() as GradientDrawable
myDrawable.setColor(ContextCompat.getColor(view.context, R.color.White))
I am try to show a image in android using java code not xml.
I have done it using xml file but my requirement is using
java code to get more funcionality .
thanks in advance for help........
IF you want to load image from drawable folder, you can using:
ImageView imgView=(ImageView) findViewById(R.id.imgView);
Drawable drawable = getResources().getDrawable(R.drawable.img);
imgView.setImageDrawable(drawable);
You have to use one default imageview in android xml when you are running app that time
in activity you have to write this code so it will replace you image with another image.
Check following code
ImageView imgView=(ImageView) findViewById(R.id.default_imgView);
imgView.setImageResource(R.drawable.yourimagename);
Using Drawable class you can show Image
Drawable drawable = Drawable.createFromPath(imagePath);
image.setImageDrawable(drawable);
I hope you are using ImageView to display the image in XML file with id iv.
In java file
ImageView iv = (ImageView)findViewById(R.id.iv);
iv.setImageResource(R.drawable.image1); // image1 is image file available in drawables folder
use ImageView and learn more about it from here imageview referecne
you can set image from bitmap,uri, from resources, etc...
frist add the images to drawable folder
and using XML image view
image = (ImageView) findViewById(R.id.imageView1);
image.setImageResource(R.drawable.image_1);
image.setImageResource(R.drawable.image_2);
..
You can use the following code
ImageView img = new ImageView(this); /*In new version of Android Studio you don't need to do castling.*/
img = findViewById(R.id.android_image);
Drawable img_drawable=getResources().getDrawable(R.drawable.draw_img);
img_cookie.setImageDrawable(img_drawable);
The code below worked fine for me.
ImageView imageViewVar = (ImageView) findViewById(R.id.imageViewId);
imageViewVar.setImageResource(R.drawable.yourImgFile);