Display gif on imagebutton android - android

I was wondering if it is possible to display a gif animation on an image button. I followed the example on Android API BitmapDecode sample and can now display animated gifs on the canvas.
Is it possible to display this same gif on an imagebutton. I tried using the setBackgroundDrawable() and setImageDrawable() but it gives me an empty button.
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.test);
ImageButton btn = (ImageButton) findViewById(R.id.ImageButton01);
SampleView p=new SampleView(this);
p.setDrawingCacheEnabled(true);
p.buildDrawingCache();
btn.setBackgroundDrawable(p.getBackground());
}
The SampleView() class is similar to the one provided in the BitmapDecode sample (i have removed all other code snippets and it now displays only gif animation)
Thanks,
New Guy.

I guess this should be possible just like it is with ImageViews - split up the animation into frames and then animate these frames using the android frame by frame animation. You might want to refer to http://www.app-solut.com/blog/2011/05/playing-animations-in-android/ for more information on how to use such a frame-by-frame animation

Related

How to get an imageView on Full Screen in Android?

I know there are lots of questions like these. However, they all seek to bring imageView on full screen after clicking.
I want to bring an imageView on Full Screen without clicking on it. In other words, when I click my bitmap, I should get a full-screen imageView. I tried a lot but could not achieve desired results.
So far, I have tried this thread's all possible solutions.
Moreover, I tried to use multiple scaling options.
I am self-learning android using a big nerd's ranch guide. I am attaching my code and images below
public View onCreateView(LayoutInflater inflater,
ViewGroup parent, Bundle savedInstanceState) {
imageView=new ImageView(getActivity());
String path= (String) getArguments().getSerializable(FILE_PATH_NAME);
BitmapDrawable image = PictureUtils.getScaledDrawable(getActivity(), path);
float rotation= rotateImage(getActivity(), path);
Log.i("RotateImagee", "Rotate valuee: " + rotation);
imageView.setRotation(rotation + 90);
imageView.setImageDrawable(image);
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
return imageView;
}
Images:
I want screen fully covered with this image with a proper scaling. Therefore, please show me an appropriate solution with proper scaling.
You can use only one scaletype and CENTER_INSIDE overrides FIT_XY.
Use only FIT_XY.
Senconly when you create your ImageView you don't give it layoutparams(default is wrap_content), so you need to set layoutparams to match_parent. Without it imageView will change size to wrap_content and FIT_XY will not take affect.
Here is a link if you want to make activity full screen.
you also need to remove ActionBar, Here.
The fact that the background and Toolbar are greyed-out makes me think you are displaying your ImageView inside a DialogFragment, not a normal Fragment. DialogFragments are a bit different. Getting them to display full screen is a non-trivial task. If you are not trying to display a Dialog then I suggest using a normal Fragment to display the ImageView.
Side notes:
consider rotating the Bitmap, not the ImageView
consider putting the ImageView in a layout file and inflating that layout file (you already have a LayoutInflater passed to you in onCreateView)

Android: AnimationDrawable squeezing my image

I have this weird behavior that I cannot find a solution around...
This only happens in my emulator API Level 10, on my smartphone (Android 4.1) works fine.
I wrote a dynamic animation to show a rolling dice, where 10 random frames are chosen as the frames of the animation, with a duration of 50ms.
When I press the button, the animation run, but squeezed into zero height..... (You can still see some colors from the animating dice), the layout also get messed up.
This does not happen on my phone though.
Here is the portion of the java code:
public void RollDice(View view) {
int id=-1;
AnimationDrawable diceAnimation = new AnimationDrawable();
//create 10 random frames from dice1.jpg to dice6.jpg into diceAnimation
for(int j=0;j<10;j++){
id=getResources().getIdentifier("dice"+String.valueOf(rng.nextInt(6)+1), "drawable", getPackageName());
diceAnimation.addFrame(getResources().getDrawable(id), 50);
}
//assigning and starting animation
diceAnimation.setOneShot(true);
dice.setImageDrawable(diceAnimation);
diceAnimation.start();
}
and the portion of the xml:
<ImageView
android:id="#+id/Dice1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:contentDescription="#string/diceimage"
android:src="#drawable/dice1" />
The button onclick call RollDice().
As final remark, IF I hardcode in ImageView, say android:layout_height="100dp", then the dice will show during animation, but streched into funny shape...
I tried most scaling method, adjustviewbound etc with no luck...
I don't know whether your problem is solved or not.
But I would like to add the answer to this question.
You can try this :
Firstly , set you image view layout_width from match_parent to wrap_content.
Second make use of this below code so as to make the dice disappear completely :
frameAnimation.setVisibility(GONE);
Might have an answer for your, try it out with caution, since I'm a beginner myself.
ViewGroup.MarginLayoutParams mParams = new ViewGroup.MarginLayoutParams(view.getLayoutParams());
ImageView view = (ImageView)findViewById(R.id.image_dice);
layoutParams = new RelativeLayout.LayoutParams(mParams);
layoutParams.width=120;
layoutParams.height=120;
view.setLayoutParams(layoutParams);
Applying that to your image will adjust the .xml on the fly, atleast it works for me when I'm grabbing alot of differently sized images from the web.

How to achieve this type of animation in the Android SDK?

Take a look at this design I did for an interface of a game I want to develop on Android:
http://www.moboing.com/smallshow.jpg
When a user touches a balloon, I want that star/glare animation to play with the stars dropping down. All I'm curious about is the best general direction/approach to making that animation possible while developing the app in eclipse/java.
I was thinking making a few variations of stars as transparent PNG's, and making them animate on touch but I'm a newbie so I'm not entirely sure.
You can create the animation like a sequence of images and then use the AnimationDrawable class to make it animated. Let me show you an example:
ImageView my_image = (ImageView) findViewById(R.id.my_animation);
AnimationDrawable animation = new AnimationDrawable();
// We need to add each image to our final animation setting time interval between them
animation.addFrame(getResources().getDrawable(R.drawable.img_1), 200);
animation.addFrame(getResources().getDrawable(R.drawable.img_2), 200);
animation.addFrame(getResources().getDrawable(R.drawable.img_3), 200);
animation.setOneShot(false);
my_image.setBackgroundDrawable(animation); // Set as background to see it
animation.start(); // Start playing the animation
Where:
my_animation: your ImageView into your desire Layout.
img_1, img_2, img_3: images that compounds your animation (your should
create and save them into res/drawable folder).
I hope it can help you and draw a way to do it by yourself :)
PS: this should be into a method on the activity that you want (for example, into onCreate to show it when user start).

Android: Placing a bitmap or drawable image into an XML displayed screen

Well after days of looking I come to the source.
I have main screen that is drawn and created in Eclipse Graphical layout.
It consists of buttons across the top of the screen. Below the buttons is image.png area.
On startup of app it displays correctly. Buttons at top and graphic image below the buttons.
On pushing each button I want the image to be changed. I can supply either a drawable or bitmap image (figured that out).
I need help. What I don't understand is how I get an external image to display with the XML generate screen buttons.
I have currently many test trials but none give me what I am looking for.
I want to place either a drawable or bitmap image into R.id.imgVwMain and have it display with the buttons. I know I've got extra code in what is provided.
R.id.imgVwMain as a default has an image in it at start up (ie splash screen) but I want to replace it dynamically on button push.
Code snippets:
ImageView img=new ImageView(this);
View image;
Drawable drawable= getImg(0); // GETS NEW IMAGE as drawable
img.setImageDrawable(drawable);
//setContentView(img); // when uncommented displays just the image no buttons, not what needed
//R.id.imgVwMain // the target ID to place the new image into
image = findViewById(R.id.imgVwMain);
ImageView imageView=(ImageView)findViewById(R.id.imgVwMain);
imageView.setImageBitmap(drawableToBitmap(drawable));
image.setImageResource(imageView);
setContentView(R.layout.main); //of course this just displays default of the XML
Set onClickListeners onto your buttons like so:
Button mButton = (Button) findViewById(R.id.my_button);
mButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
ImageView ivImage = (ImageView) findViewById(R.id.imgVwMain);
ivImage.setImageDrawable(getImg(0));
}
});
Now, whenever the button is clicked it set's the image of your imageview to whatever you'd like.
You can probably use addView to add an imageView dynamically and likewise call removeView to get rid of an imageView. At some point you'll need to set the layout parameters as well. Hopefully this will get you started:
myParentViewOrLayout.removeView(oldImage);
ImageView newImage = (ImageView) findViewById(R.id.imgToAdd);
//Set your layoutParams
myParentViewOrLayout.addView(newImage);

Animated dialog with AsyncTask

I need to draw a custom(Animation of Image) dialog with AsyncTask. I created a layout with TextView and ImageView to display message and play animation of the images.
Please let me know how can I update the image of the ImageView object to display animation.
Thanks,
Android_IT
Hm, in my project, I show preview of camera like this:
ImageView imagePreview = (ImageView) layout.findViewById(R.id.previewImage);
imagePreview.setImageBitmap(selectedBitmap);

Categories

Resources