android animation through button - android

I am creating a dash board application. This is what it looks like:
Now I will explain to you how it is working. When I am clicking the "UP" button the red hand moves up and stay in the 30. The "DOWN" button is used for down the hand from 30 to 0. The BUTTON5 is used for moving the hand from 0 (starting position) to 5. When I am clicking BUTTON10 it means it will move from 5 to 10. BUTTON-5 is used for down the hand from 10 to 5 and BUTTON0 is used for down the hand from 5 to 0.
This is my code:
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;
public class dash1 extends Activity {
ImageView img, img1;
Animation an, an1, an2, an3, an4, an5;
Button bt, bt2, bt3, bt4, bt5, bt6;
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
img = (ImageView) findViewById(R.id.ImageView01);
final Bitmap bmp = BitmapFactory.decodeResource(getResources(),
R.drawable.minute);
final int w = bmp.getWidth();
final int h = bmp.getHeight();
final Matrix mat = new Matrix();
mat.postRotate(245);
Bitmap rtd = Bitmap.createBitmap(bmp, 0, 0, w, h, mat, true);
BitmapDrawable bmd = new BitmapDrawable(rtd);
img.setImageDrawable(bmd);
an = AnimationUtils.loadAnimation(this, R.anim.anim);
an1 = AnimationUtils.loadAnimation(this, R.anim.anim1);
an2 = AnimationUtils.loadAnimation(this, R.anim.anim2);
an3 = AnimationUtils.loadAnimation(this, R.anim.anim3);
an4 = AnimationUtils.loadAnimation(this, R.anim.anim4);
an5 = AnimationUtils.loadAnimation(this, R.anim.anim5);
bt = (Button) findViewById(R.id.Button01);
bt2 = (Button) findViewById(R.id.Button02);
bt3 = (Button) findViewById(R.id.Button03);
bt4 = (Button) findViewById(R.id.Button04);
bt5 = (Button) findViewById(R.id.Button05);
bt6 = (Button) findViewById(R.id.Button06);
bt.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
an.setFillAfter(true);
img.startAnimation(an);
}
});
bt2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
img.startAnimation(an1);
}
});
bt3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
an2.setFillAfter(true);
img.startAnimation(an2);
}
});
bt4.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
an3.setFillAfter(true);
img.startAnimation(an3);
}
});
bt5.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
an2.setFillAfter(true);
img.startAnimation(an4);
}
});
bt6.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
an5.setFillAfter(true);
img.startAnimation(an5);
}
});
}
}
Now I want to add a button "+" instead of button5 and button10 (for increment) and "-" for the button-5 and 0. How can I do the increment by a single button. I don't know how to do this. Here for rotating I am using animation. This is the xml code for rotation. Anybody know how to do this means just tell me.
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false">
<rotate android:toYScale="0.0"
android:pivotX="50%"
android:pivotY="50%"
android:duration="3599"
android:fromDegrees="0"
android:toDegrees="205">
</rotate>
</set>

Related

How to increase and decrease TextView font size in Android

I have 2 buttons for increasing and decreasing TextView font size:
btnZoomin.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
txtview.setTextSize(txtview.getTextSize()+1);
}
});
btnZoomout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
txtview.setTextSize(txtview.getTextSize()-1);
}
});
But are experiencing difficulty, as it will not work.
If anyone is still facing the text size issue. You can use this code:
To increase size: tvText.setTextSize(0, tvText.getTextSize() + 2.0f);
To decrease size tvText.setTextSize(0, tvText.getTextSize() - 2.0f);
txtview.setTextSize(TypedValue.COMPLEX_UNIT_PX,txtview.getTextSize()-1);
txtview.setTextSize(TypedValue.COMPLEX_UNIT_PX,txtview.getTextSize()+1);
How can you see in the DOCS setTextSize needs 2 arguments: the TypedValue and the new size.
Your code is fine, you just have to add the TypedValue :
txtview.setTextSize(TypedValue.COMPLEX_UNIT_SP,txtview.getTextSize()-1);
txtview.setTextSize(TypedValue.COMPLEX_UNIT_SP,txtview.getTextSize()+1);
Use this text.setTextSize(TypedValue.COMPLEX_UNIT_SP,14);
Sorry for the previous code, that was wrong but i think this will work for you,
package com.example.sample;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
int counter = 0;
private int size;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView txt = (TextView) findViewById(R.id.txt);
Button btn = (Button) findViewById(R.id.button1);
counter = (int) txt.getTextSize();
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), txt.getTextSize() + "",
Toast.LENGTH_LONG).show();
size = counter++;
txt.setTextSize(size);
}
});
}
}

How to call onclicklistner of animated image android

i am working on android app
in which i have an animated image.
my code is
Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth()/2;
left = new TranslateAnimation(0, hight, width, hight);
left1= new TranslateAnimation( 480, 10, 0, 10);
left.setDuration(2000);
left.setAnimationListener(this);
b1 =(ImageView)findViewById( R.id.balloon);
b1.setOnClickListener(this);
b1.startAnimation(left);
#Override
public void onClick(View v) {
Toast.makeText(this, "Clicked", 27).show();
}
using this code i am able to animate ballon or my picture but i the onclick lisnter only works when animation is completed i want onclicklistner should work during animation how to do this.
sorry for bad english
Override onAnimationStart and onAnimationEnd functions in your AnimationListener. Keep a variable to check whether animation is playing when the image clicked.
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.Toast;
public class MainActivity extends Activity {
private ImageView imageView;
private boolean animationPlaying;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TranslateAnimation animation = new TranslateAnimation( 480, 10, 0, 10);
animation.setDuration(2000);
animation.setAnimationListener(new AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
animationPlaying = true;
}
#Override
public void onAnimationRepeat(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
animationPlaying = false;
}
});
imageView = (ImageView) findViewById(R.id.imageView1);
imageView.startAnimation(animation);
imageView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if(animationPlaying) {
Toast.makeText(getBaseContext(), "Click", Toast.LENGTH_SHORT).show();
} else {
Log.d("ANIMATION", "click missed because animation was not playing");
}
}
});
}
}

XML Animation executing synced

I've made some simple animations which I called breath because the object slowly get bigger and smaller in a loop.
Then I associated this animation to 3 imagebuttons and everything works ok, but when I interact with a button, which do another "vibrate" animation, all of them restart the animation together, while the expected behaviour was that only the pressed button would have restarted the animation.
I would like to understand that a bit better, and maybe how to achieve that every button's animation have its own lifecycle.
That's the code:
breath.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="#android:anim/accelerate_decelerate_interpolator" >
<scale
android:duration="1500"
android:startOffset="0"
android:fromXScale="0.9"
android:fromYScale="0.9"
android:pivotX="50%"
android:pivotY="50%"
android:toXScale="1.0"
android:toYScale="1.0"
android:repeatMode="reverse"
android:repeatCount="infinite" />
</set>
and that's the java code:
package com.anesoft.android.citmania;
import com.anesoft.android.citmania.models.Defines;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Typeface;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
public class MenuActivity extends Activity implements OnClickListener, AnimationListener {
ImageButton opzioni;
ImageButton play;
ImageButton espansioni;
ImageView logo;
TextView title,title2;
Animation breath,rotate_r,rotate_l,vibrate;
int flag = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu);
logo = (ImageView)findViewById(R.id.logo);
title = (TextView)findViewById(R.id.title);
title2 = (TextView)findViewById(R.id.title2);
opzioni = (ImageButton)findViewById(R.id.options);
play = (ImageButton)findViewById(R.id.play);
espansioni = (ImageButton)findViewById(R.id.expansions);
breath = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.breath);
rotate_r = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.rotate_right);
rotate_l = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.rotate_left);
vibrate = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.vibrate);
vibrate.setAnimationListener(this);
Typeface tf = Typeface.createFromAsset(getAssets(), "font/Canter_Light.otf");
Typeface tf2 = Typeface.createFromAsset(getAssets(), "font/Canter Bold.otf");
title.setTypeface(tf);
title2.setTypeface(tf2);
title.setText("CIT");
title.setTextSize(Defines.TITLE_SIZE);
title2.setText(".MANIA");
title2.setTextSize(Defines.TITLE_SIZE);
opzioni.startAnimation(breath);
play.startAnimation(breath);
espansioni.startAnimation(breath);
logo.setOnClickListener(this);
opzioni.setOnClickListener(this);
play.setOnClickListener(this);
espansioni.setOnClickListener(this);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu, menu);
return true;
}
#Override
public void onClick(View v) {
int id = v.getId();
if (id == R.id.options) {
Intent i = new Intent(this, OptionsActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
startActivity(i);
} else if (id == R.id.play) {
Intent i = new Intent(this, LevelPickerActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
startActivity(i);
} else if(id == R.id.expansions){
espansioni.startAnimation(vibrate);
Toast.makeText(this, "Implementando", Toast.LENGTH_SHORT).show();
//espansioni.startAnimation(bounce);
}else if(id == R.id.logo){
if(flag==0){
logo.startAnimation(rotate_r);
flag=1;
}else{
logo.startAnimation(rotate_l);
flag=0;
}
}
}
#Override
public void onAnimationEnd(Animation arg0) {
if (arg0 == vibrate) {
espansioni.startAnimation(breath);
}
}
#Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub
}
}
You can see how I start the vibrate animation and then when IT finishes I restart the breath one only in the button being tapped, but all of them restart the animation in a synced way.
Thanks in advance
If you want the animation to work independently on all 3 buttons, you'll need to create 3 instances of the animation.
Once the animation is halted, it gives its views a new state. If you gave 3 views the same animation, they will all receive the new state.

I can not pass the parameter i to the button[i]

I am trying to play a sound on button click and I am trying to make it call the .start() function only but the problem is I set the i parameter in the onclick function and so it does not pass the i to the onclicklistner function any ideas how to fix that ??
here is my code
package com.example.buttonsdemo;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.app.Activity;
import android.support.v4.widget.SimpleCursorAdapter.ViewBinder;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity implements View.OnClickListener{
//Creating Sound arrays
int i=0;
MediaPlayer[] mediaplayer = new MediaPlayer[120];
Button button[] = new Button [120];
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Initialize the mediaplayer
for(int z=0;z<120;z++)
{
mediaplayer[z]=null;
}
//Initialize Button Array
for(int x=0;x<120;x++)
{
button[x]=new Button(this);
button[x].setOnClickListener(this);
}
//Creating Media player array
mediaplayer[0]= MediaPlayer.create(this,R.raw.akali);
mediaplayer[1]= MediaPlayer.create(this,R.raw.alistar);
button[i].setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
switch(v.getId())
{
case R.id.akali:
i=0;
break;
case R.id.alistar:
i=1;
break;
}
mediaplayer[i].start();
}
} );
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
}
Inside your Listener, you want to use a variable which is declared inside your Listener.
You are trying to use a variable (i) which is declared in code which is not run when the Listener is run.
new View.OnClickListener() {
#Override
public void onClick(View v) {
int idx;
switch(v.getId()) {
case R.id.akali:
idx=0;
break;
case R.id.alistar:
idx=1;
break;
}
mediaplayer[idx].start();
}
}
Or invoke the respective start() method right in the case.
Dump the 'i' declared. Replace method with this (the "Button theButton" needs to stay 'final').
for(int z=0;z<120;z++)
{
final Button theButton = button[z];
theButton.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
// TODO Auto-generated method stub
// do what you want.
}
});
}
I believe your onCreate should be created like this:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Initialize the mediaplayer
for(int z=0;z<120;z++)
{
mediaplayer[z]=null;
}
mediaplayer[0]= MediaPlayer.create(this,R.raw.akali);
mediaplayer[1]= MediaPlayer.create(this,R.raw.alistar);
//Initialize Button Array
for( x=0;x<120;x++)
{
button[x]=new Button(this);
button[x].setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mediaplayer[x].start();
}
} );
}
}
So basically you just need to make several button on the fly which produces different sound when clicked. Try this code:
Button myButton[] = new Button[100];
MediaPlayer mySound = new MediaPlayer();
for (int i=0; i<100; i++) {
myButton[i] = new Button(this);
myButton[i].setId(i+1);
myButton[i].setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Resources res = context.getResources();
int soundId = res.getIdentifier("sound" + v.getId(), "raw", context.getPackageName());
mySound.create(getApplicationContext(), soundId);
mySound.start();
}
});
}

I want to change from an xml layout to surfaceview using java

OK so I'm using xml to set this menu which is supported by the following java code
package starting.rt;
import java.util.List;
import java.util.Random;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class Menu extends Activity implements OnClickListener{
View.OnTouchListener gestureListener;
TextView display;
Button begin;
Button random;
Button game;
TextView counter;
Button next;
Button previous;
Button moreapps;
Button rate;
Random myRandom;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(starting.rt.R.layout.menu);
begin = (Button) findViewById(starting.rt.R.id.Begin);
random = (Button) findViewById(starting.rt.R.id.Random);
display = (TextView) findViewById(starting.rt.R.id.tvResults);
counter = (TextView) findViewById(starting.rt.R.id.tvCounter);
next = (Button) findViewById(starting.rt.R.id.Next);
previous = (Button) findViewById(starting.rt.R.id.Previous);
moreapps = (Button)findViewById(R.id.More);
rate = (Button) findViewById(R.id.rate);
game = (Button) findViewById(R.id.game);
// display.setOnTouchListener(this.gestureListener);
begin.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent openStartingPoint = new Intent("starting.rt.RelationshipTipsActivity");
startActivity(openStartingPoint);
}});
moreapps.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent goToMarket;
goToMarket = new Intent(Intent.ACTION_VIEW, Uri.parse("market://search?q=pub:\"Wompa\""));
startActivity(goToMarket);
}});
game.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent openStartingPoint = new Intent("starting.rt.GameView");
startActivity(openStartingPoint);
}});
rate.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse("market://details?id=" + getPackageName()));
startActivity(i);
}});}
public void onClick(View v) {
// TODO Auto-generated method stub
}
}
Now what's supposed to be happening is when they click on the game which starts a new java class called GameView it crashes on clicked. Every other button works.
This is the code from GameView
package starting.rt;
import java.util.ArrayList;
import java.util.List;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
public class GameView extends SurfaceView {
private GameLoopThread gameLoopThread;
private List<Sprite> sprites = new ArrayList<Sprite>();
private List<TempSprite> temps = new ArrayList<TempSprite>();
private long lastClick;
private Bitmap bmpBlood;
public GameView(Context context) {
super(context);
gameLoopThread = new GameLoopThread(this);
getHolder().addCallback(new SurfaceHolder.Callback() {
public void surfaceDestroyed(SurfaceHolder holder) {
boolean retry = true;
gameLoopThread.setRunning(false);
while (retry) {
try {
gameLoopThread.join();
retry = false;
} catch (InterruptedException e) {}
}
}
public void surfaceCreated(SurfaceHolder holder) {
createSprites();
gameLoopThread.setRunning(true);
gameLoopThread.start();
}
public void surfaceChanged(SurfaceHolder holder, int format,
int width, int height) {
}
});
bmpBlood = BitmapFactory.decodeResource(getResources(), R.drawable.blood1);
}
private void createSprites() {
sprites.add(createSprite(R.drawable.bad1));
// sprites.add(createSprite(R.drawable.bad2));
// sprites.add(createSprite(R.drawable.bad3));
// sprites.add(createSprite(R.drawable.bad4));
// sprites.add(createSprite(R.drawable.bad5));
// sprites.add(createSprite(R.drawable.bad6));
// sprites.add(createSprite(R.drawable.good1));
// sprites.add(createSprite(R.drawable.good2));
// sprites.add(createSprite(R.drawable.good3));
// sprites.add(createSprite(R.drawable.good4));
// sprites.add(createSprite(R.drawable.good5));
// sprites.add(createSprite(R.drawable.good6));
}
private Sprite createSprite(int resouce) {
Bitmap bmp = BitmapFactory.decodeResource(getResources(), resouce);
return new Sprite(this, bmp);
}
#Override
protected void onDraw(Canvas canvas) {
canvas.drawColor(Color.BLACK);
for (int i = temps.size() - 1; i >= 0; i--) {
temps.get(i).onDraw(canvas);
}
for (Sprite sprite : sprites) {
sprite.onDraw(canvas);
}
}
#Override
public boolean onTouchEvent(MotionEvent event) {
if (System.currentTimeMillis() - lastClick > 300) {
lastClick = System.currentTimeMillis();
float x = event.getX();
float y = event.getY();
synchronized (getHolder()) {
for (int i = sprites.size() - 1; i >= 0; i--) {
Sprite sprite = sprites.get(i);
if (sprite.isCollition(x, y)) {
sprites.remove(sprite);
temps.add(new TempSprite(temps, this, x, y, bmpBlood));
break;
}
}
}
}
return true;
}
}
The GameView calls a few other classes for things part of the game but it crashes before it can start. I think it's crashing because it's switching from xml layout to the java surfaceview. Help would be appreciated :) Thanks!
First of all, you should always post in your questions the stacktrace with the exception from the Logcat if your app crashes.
You can't start a SurfaceView directly, instead your custom SurfaceView must be embedded in an Activity like any other view in android. For example:
public class GameViewActivity extends Activity {
#Override
public void onCreate(Bundle savedInstance) {
super.onCreate(savedInstance);
setContentView(new GameView(this));
}
}

Categories

Resources