I created one simple demo project, which does the animation using AnimationDrawable class of android.
Here is my java code:
public class TestAnimationActivity extends Activity {
/** Called when the activity is first created. */
ImageView imgCircleWhite,imgCircleYellow;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
imgCircleYellow = (ImageView)findViewById(R.id.imgCircleYellow);
animateState(true, imgCircleYellow);
}
public void animateState(boolean flag,ImageView imageView)
{
imgCircleYellow.setBackgroundResource(R.drawable.animate_circle_sensor_1);
AnimationDrawable yourAnimation = (AnimationDrawable) imageView.getBackground();
if(!flag)
{
//imageView.getAnimation().reset();
imageView.setBackgroundResource(R.drawable.circle_label_white_1);
yourAnimation.stop();
}
else
{
imageView.setBackgroundResource(R.drawable.animate_circle_sensor_1);
yourAnimation = (AnimationDrawable) imageView.getBackground();
yourAnimation.start();
}
}
}
Here is my main.xml file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello" />
<ImageView android:id="#+id/imgCircleYellow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/circle_label_yellow_1"/>
</LinearLayout>
Below is my animate_sensor_circle_1.xml, which I've put in drawable folder to use with:
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="false" >
<item
android:drawable="#drawable/circle_label_white_2"
android:duration="50"/>
<item
android:drawable="#drawable/circle_label_yellow_1"
android:duration="50"/>
</animation-list>
When I run the app, only the first item in the above xml file gets displayed, but animation does not occur repeatedly as it should done.
Can anyone direct me where I am going wrong?
It is not the right time to call the AnimationDrawable.start(). You should do this after the view initialization has been completed. Try this:
final ImageView imgCircleYellow = (ImageView)findViewById(R.id.imgCircleYellow);
imgCircleYellow.setBackgroundResource(R.drawable.animate_circle_sensor_1);
imgCircleYellow.post(new Runnable() {
#Override
public void run() {
animateState(true, imgCircleYellow);
}
Check this for more information: https://stackoverflow.com/a/5490922/813135
Related
How can i decrease image size when image is falling from top to bottom?
I have developed top to bottom process through below link
Android Layout Animations from bottom to top and top to bottom on ImageView click
But i am facing one problem.How can i decrease size of image when image is falling top to bottom?
Please let me share your idea or link and code.
Please see my requirement in below image.
you can apply scale transformation to that view.
Matrix matrix = new Matrix();
matrix.setScale(valueX, valueY);
view.setTransform(matrix);
For imageView
imageView.setImageMatrix(matrix);
I solved my question.I hope everyone will like it.
1.Create anim/zoom_out.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="#android:anim/decelerate_interpolator"
android:fillAfter="true">
<scale
android:duration="5000"
android:fromXScale="100%"
android:fromYScale="100%"
android:pivotX="50%"
android:pivotY="50%"
android:toXScale="50%"
android:toYScale="50%" />
<translate
android:duration="5000"
android:fromYDelta="10%"
android:toYDelta="50%" />
2.activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="10dp"
android:paddingRight="10dp">
<ImageView android:id="#+id/imgvw"
android:layout_width="wrap_content"
android:layout_height="250dp"
android:src="#mipmap/ic_launcher"/>
<Button
android:id="#+id/btnZoomIn"
android:layout_below="#+id/imgvw"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Zoom In" android:layout_marginLeft="100dp" />
<Button
android:id="#+id/btnZoomOut"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/btnZoomIn"
android:layout_toRightOf="#+id/btnZoomIn"
android:text="Zoom Out" />
</RelativeLayout>
3.MainActivity.java file
public class MainActivity extends AppCompatActivity {
private Button btnzIn;
private Button btnzOut;
private ImageView img;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnzIn = (Button)findViewById(R.id.btnZoomIn);
btnzOut = (Button)findViewById(R.id.btnZoomOut);
img = (ImageView)findViewById(R.id.imgvw);
/* btnzIn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// img.startAnimation(AnimationUtils.loadAnimation(getApplicationContext(),R.anim.zoom_in));
img.startAnimation(AnimationUtils.loadAnimation(getApplicationContext(), R.anim.slide_down));
// img.clearAnimation();
}
});*/
btnzOut.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// img.startAnimation(AnimationUtils.loadAnimation(getApplicationContext(), R.anim.slide_up));
img.startAnimation(AnimationUtils.loadAnimation(getApplicationContext(),R.anim.zoom_out));
}
});
}
I'm new to android studio. Right now I'm making a basic app that recursively counts down from 4 to 0 when you click a button on the screen. When I look at my activity_main.xml in Design view, it shows what I want, but when I run my app on my Virtual Device, it just shows a white screen with nothing on it. How can I fix this? I'll paste my code here, almost all of my code is in the activity_main.xml and MainActivity.java
Here is activity_main.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >``
<Button
android:id="#+id/button1"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="#string/button1Contents"
android:onClick="sendMessage"/>
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/button1"
android:layout_alignBottom="#+id/button1"
android:layout_toRightOf="#+id/button1"
android:text="#string/textView1Contents" />
</RelativeLayout>
and here is my MainActivity.java
public class MainActivity extends Activity {
private static final String TAG = "OneButtonRecursion";
public void sendMessage(View view) {
Button clickedButton = (Button) view;
if (clickedButton == findViewById(R.id.button1)) {
TextView cup = (TextView) findViewById(R.id.textView1);
int handFull = Integer.parseInt(cup.getText().toString());
if (handFull > 0) {
cup.setText(handFull + "");
playNextCup(cup, handFull);
}
} else {
}
}
public static void playNextCup(TextView cupIn, int handFullIn) {
handFullIn--;
cupIn.setText(handFullIn + "");
if (handFullIn != 0) {
playNextCup(cupIn, handFullIn);
Log.i(TAG, "In recursion " + handFullIn);
} else {
}
}
}
and here is my strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">One Button Recursion</string>
<string name="action_settings">Settings</string>
<string name="button1Contents">Button1</string>
<string name="textView1Contents">4</string>
</resources>
You just forgot to Override onCreate() method
onCreate() Called when the activity is starting. This is where most initialization should go: calling setContentView(int) to inflate the activity's UI,
Just Override onCreate() inside your activity it will
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
Im trying to implement a ClipDrawable appear by clicking with diferents images
I have this working with a simple image
Code:
MainActivity.java
ImageView imageview = (ImageView) findViewById(R.id.image);
drawable = (ClipDrawable) imageview.getDrawable();
drawable.setLevel(0);
imageview.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
drawable.setLevel(drawable.getLevel()+1000);
}
});
activity_main.xml
<ImageView
android:id="#+id/image"
android:src="#drawable/clip_source"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
clip_source.xml
<clip xmlns:android="http://schemas.android.com/apk/res/android"
android:drawable="#drawable/tree"
android:clipOrientation="vertical"
android:gravity="bottom"
/>
this is working but for my project I need to change the image displayed with a variable to display diferents images depending of the user input
I only find one way, using diferent files for each ClipDraw
MainActivity.java
if(aa == false){
t = (ClipDrawable) getResources().getDrawable(R.drawable.clip1);
}else{
t = (ClipDrawable) getResources().getDrawable(R.drawable.clip2);
}
setContentView(R.layout.activity_main);
ImageView imageview = (ImageView) findViewById(R.id.image);
imageview.setBackgroundDrawable(back);
imageview.setImageDrawable(t);
t.setLevel(0);
imageview.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
t.setLevel(t.getLevel()+1000);
}
});
activity_main.xml
<ImageView
android:id="#+id/image"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
clip1.xml
<?xml version="1.0" encoding="utf-8"?>
<clip xmlns:android="http://schemas.android.com/apk/res/android"
android:drawable="#drawable/farm"
android:clipOrientation="vertical"
android:gravity="bottom"
/>
clip2.xml
<?xml version="1.0" encoding="utf-8"?>
<clip xmlns:android="http://schemas.android.com/apk/res/android"
android:drawable="#drawable/werehouse"
android:clipOrientation="vertical"
android:gravity="bottom"
/>
this solve my problem but have the disadvantage of need one file for each image, and I will need at least 20
This question already has answers here:
How to fade out and in between two images?
(2 answers)
Closed 9 years ago.
I have two images loading in my splash screen. The first image opens (starting the splash screen) then the second image opens. Once the second image fades out the MainActivity starts. Now my question is how do I make my first image fade out, then fade in with my second image?
I'm not trying to cross fade between the two either. I'm trying to do a complete fade out then fade in transition.
The splash.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:id="#+id/lin_lay"
android:gravity="center" >
<ImageView
android:contentDescription="#string/desc"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/spinning_wheel_image"
android:background="#drawable/splashscreen1" />
</LinearLayout>
The mainanim.xml
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="false">
<item android:drawable="#drawable/splashscreen1" android:duration="2500" />
<item android:drawable="#drawable/splashscreen2" android:duration="4000" />
</animation-list>
The Splash.java
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
ourSong = MediaPlayer.create(Splash.this, R.raw.splashsound);
ourSong.start();
Thread timer = new Thread(){
public void run(){
try{
sleep(10500);
} catch (InterruptedException e){
e.printStackTrace();
}finally{
Intent openStartingPoint = new Intent("com.theapplication.app.STARTINGPOINT");
startActivity(openStartingPoint);
}
}
};
timer.start();
}
#Override
public void setRequestedOrientation(int requestedOrientation) {
super.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
#Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
ImageView mainimage = (ImageView)findViewById(R.id.spinning_wheel_image);
mainimage.setBackgroundResource(R.anim.mainamin);
mainanimation = (AnimationDrawable) mainimage.getBackground();
mainanimation.start();
}
You can try this demo.
may be helpful for you.
I'm doing things pretty simply but I have a lot of frames. It seems to be the amount of frames that causes the exception. I've tried to reduce the size of each frame, but that doesn't seem to effect much. This is even before the animation starts. Short of showing less frames, what can be done? Is there another approach?
Adapted from sample code:
public class XMLAnimation extends Activity
{
class MyAnimationRoutine extends TimerTask
{
MyAnimationRoutine()
{
}
#Override
public void run()
{
ImageView img = (ImageView) findViewById(R.id.simple_anim);
AnimationDrawable frameAnimation = (AnimationDrawable) img
.getBackground();
frameAnimation.start();
}
}
class MyAnimationRoutine2 extends TimerTask
{
MyAnimationRoutine2()
{
}
#Override
public void run()
{
ImageView img = (ImageView) findViewById(R.id.simple_anim);
AnimationDrawable frameAnimation = (AnimationDrawable) img
.getBackground();
frameAnimation.stop();
}
}
#Override
public void onCreate(Bundle icicle)
{
super.onCreate(icicle);
setContentView(R.layout.main);
}
#Override
protected void onResume()
{
super.onResume();
ImageView img = (ImageView) findViewById(R.id.simple_anim);
img.setBackgroundResource(R.anim.simple_animation);
MyAnimationRoutine mar = new MyAnimationRoutine();
MyAnimationRoutine2 mar2 = new MyAnimationRoutine2();
Timer t = new Timer(false);
t.schedule(mar, 100);
Timer t2 = new Timer(false);
t2.schedule(mar2, 5000);
}
}
the layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ImageView android:id="#+id/simple_anim"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_centerHorizontal="true"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Hello World, XMLAnimation"
/>
</LinearLayout>
the animation list:
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" id="selected" android:oneshot="false">
<item android:drawable="#drawable/frame1" android:duration="50" />
...
<item android:drawable="#drawable/frame40" android:duration="50" />
</animation-list>
After some minor refactoring and code clean-up, wiping data in the emulator, and starting fresh, these problems went away. Testing on the actual device, this part of code works quite smoothly.
So, sometimes, just starting fresh works out.