AnimationDrawable start() vs ImageView setImageDrawable(...) on Android - android

I have 2 png`s with some fixeble size(a.png and b.png)
I have a button. I want to have situation when touch on button and release some animation (for example 1 frame- a.png, 2 frame - b.png)
When I have xml:
...
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView_id"
android:layout_alignParentBottom="true"
android:paddingBottom="50dp"
android:paddingRight="25dp"
android:src="#drawable/a"
/>
...
When I
view.setImageDrawable(context.getDrawable(R.drawable.b));
in OnClick(...) interface I get my goal(change image in ImageView)
enter image description here,
But when I make
AnimationDrawable pro2 = (AnimationDrawable)view.getBackground();
pro2.start();
enter image description here.
I make Animation with animation-list and 2 items with a.png and b.png, Why I cant set animation instead setImageDrawable(...)?

The question is not clear but i suppose you want to do the same task with animation Drawable for that you need to create an xml drawable of animation-list and then set it as background in the imageview . You can refer this link for more understanding on this topic .
A little snippet to achieve this effect
ImageView mImageViewFilling = (ImageView) findViewById(R.id.imageview_animation_list_filling);
((AnimationDrawable) mImageViewFilling.getBackground()).start();

Related

Cannot load a bitmap to image view

I am just learning how to develop android apps but i came across an error in which i cannot load a decoded bitmap to my image view
My image view is declared in linear layout (vertical) as
<ImageView>
android:id="#+id/image"
android:layout_height="match_parent"
android:width="match_parent"
/>
And i am adding bitmap by this process in onCreate() method
ImageView iv=(ImageView)findViewById(R.id.image);
Bitmap bm=BitmapFactory.decodeResource(getResources(),R.drawable.ic_launcher);
iv.setImageBitmap(bv);
When i run this code it shows me a dialog box showin your project contains errors ...
Please help me
Thanks in advance
Firstly you need to rightly define you ImageView, it should be, without right bracket >:
<ImageView
android:id="#+id/image"
android:layout_height="match_parent"
android:width="match_parent"
/>
Secondly, if you want to set image from resources, you can simply use setImageResource():
ImageView iv=(ImageView)findViewById(R.id.image);
iv.setImageResource(R.drawable.ic_launcher);
Maybe try loading the drawable instead of the bitmap.
iv.setImageDrawable(getResources().getDrawable(R.drawable.ic_launcher);
See here for more details

How to make a view as always on top?

I have a TextView and it is on an image. When I click the button, TextView's background color will change, but image won't disappear. For example:
My TextView at the beginning:
When I click a button:
If you want to do this using only one TextView then its may be not possible. So, I will suggest you to do this using FrameLayout. you can write your layout as below.
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#FFFFFF" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" />
</FrameLayout>
When you want to change the color behind the TextView then change the ImageView background as below...
ImageView mageView view = (ImageView) findViewById(R.id.image);
view.setBackgroundColor(Color.BLUE);
If it must be a TextView and an ImageView you could also wrap it within a FrameLayout (which is supposed to contain only one child). A FrameLayout will place all containing children elements on the same "place", so the get overlapped.
Or when you need more elements, you could also use a RelativeLayout and give your elements the same layout rules (e.g. all centered).
You should use ImageView instead of TextView, the:
public void onClick(View v){
imageView.setBackgroundColor(Color.GREEN);
}
ImageButton is better option for you.Image source will be star as u said.then onclick you change the background. if want set Text in this
android:drawableTop="#drawable/any_drawable"
android:text="#string/any_text"

how to fix this imageview error?

Allow me to explain. I have :
a button with picture (located at #drawable/pic),
linear layout (id=linear1)
the button xml is below :
<ImageButton
android:id="#+id/imageButton1"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_marginRight="10dp"
android:scaleType="fitXY"
android:src="#drawable/pic"
android:maxWidth="80dp"
android:maxHeight="80dp"
tools:ignore="ContentDescription" />
the linear layout xml is as follow :
<LinearLayout
android:id="#+id/layout1"
android:layout_width="match_parent"
android:layout_height="80dp"
android:gravity="center"
android:orientation="horizontal"
tools:ignore="UselessLeaf" >
what i want is, when i click the button i want to create/generate imageview programatically inside the linearlayout and i want to fill it with the same picture for the button (pic). The code is below :
//initiate imageview
ImageView img=new ImageView(this);
//get drawable from button
Drawable blabla=btn1.getDrawable();
//set drawable to imageview
img.setImageDrawable(blabla);
//set height and width of imageview to 50dp
LinearLayout.LayoutParams parms = new LinearLayout.LayoutParams(50,50);
img.setLayoutParams(parms);
img.setScaleType(ScaleType.FIT_XY);
//place imageview to linearlayout
layoutTempat.addView(img);
The code works fine with displaying the imageview with the same image as the button have.
however the problem is : when i set the imageview to 50dp programatically, the image inside button changed too.. how can it happened ? i am so confused as iam a newbie..
Thanks before.
The two views are sharing the same drawable.
It's plausible your manipulations on one view are being sent to the underlying drawable, effecting how its displayed in the other view -- frankly I don't know. But assuming the case is as you described, this problem is easily fixed by cloning the drawable as follows:
Drawable dr = btn1.getDrawable().getConstantState().newDrawable();
img.setImageDrawable(dr);

Cube Animation with only two Images in an ImageView

I need to show an Image on ImageView & onClick of that Image , I wanna show another image on that imageView only with some Cube-like Transition Animation as shown in the below figure with "onTouch" .
I have been searching some examples on this site which , all are intended for an activity .I need to implement it for an ImageView .
Presently I am using some ViewFlipper with two images and trying to implement it,
<ViewFlipper android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/a"
android:layout_weight="1">
<ImageView
android:contentDescription="#string/action_settings"
android:id="#+id/alphaA"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="#drawable/a"/>
<ImageView
android:contentDescription="#string/action_settings"
android:id="#+id/imgA"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="#drawable/small_aeroplane1"/>
</ViewFlipper>
..........
like that I have 4 View Flippers upto now.
I need to know , how to add cube transitions for the above case. OR is there any better way to implement CUBE TRANSITION between only two images
I think these two links will help you in your future projects.
1) https://code.google.com/p/transitionviewpager/source/browse/
2) https://github.com/jfeinstein10/JazzyViewPager
You'll have to use ViewPager,
check https://github.com/jfeinstein10/JazzyViewPager
or here is a ready ViewPager class with 3D cube animation
https://github.com/inovex/ViewPager3D/blob/master/src/de/inovex/android/widgets/ViewPager3D.java

Android ImageView changes background image

How can i make a ImageView button change the background image?
Like you hit the imageview button and then the background changes to what i set it to
You've got lots of methods for it
ImageView i;
i.setImageBitmap(bm);
i.setImageDrawable(drawable);
i.setImageResource(resId);
All you need to do is to check documentation and you'll find an answer. No need to post such questions on StackOverFlow. Good luck!
<ImageView
android:id="#+id/pic1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:src="#drawable/before_pic" />
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click Here"
android:onClick="switchPic" />
/**
* Called when button is clicked.
*/
public void switchPic(View view) {
ImageView after = (ImageView) findViewById(R.id.pic1);
after.setImageResource(R.drawable.after_pic);
}
In XML, assuming the ImageView tag is in your parent view so it would fill the background, set it to your first image, "before_pic". When the Button is clicked it calls the switchPic function.
In the switchPic function, first we create an ImageView object called 'after' and pass it the same ImageView "pic1". Then re-inflate it with the new image "after_pic" with this line of code: after.setImageResource(R.drawable.after_pic);
button.setBackgroundDrawable(drawable);
button.setBackgroundResource(R.drawable.YOUR_DRAWABLE);
These are your options for a button.

Categories

Resources