How to call onclicklistner of animated image android - 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");
}
}
});
}
}

Related

Remove View after animation

I have a view that does an animation and then should be removed or at least sent to the back so i can reuse it. I'm having trouble attaching a listener to it.
I've been stuck on this for about 2 weeks so any help at all is really appreciated.
I'm just trying to get the card to fling off the screen, disappear and then be deleted.
package com.example.trevorwood.biggles.study;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.view.ViewCompat;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.RotateAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import com.example.trevorwood.biggles.R;
public class StudyActivity extends AppCompatActivity {
LinearLayout mFlipCardLinearLayout;
LinearLayout mCardFlippedButtons;
FrameLayout mCardFrame;
View mCurrentAnimCardContainer;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_study);
Intent intent = getIntent();
// String value = intent.getStringExtra("key");
Toolbar toolbar = (Toolbar) findViewById(R.id.study_toolbar);
toolbar.setTitleTextColor(Color.WHITE);//0xAARRGGBB
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
mFlipCardLinearLayout = (LinearLayout) findViewById(R.id.flip_card_linear_layout);
mCardFlippedButtons = (LinearLayout) findViewById(R.id.card_flipped_buttons);
mCardFrame = (FrameLayout) findViewById(R.id.card_frame);
final Drawable upArrow = getResources().getDrawable(R.drawable.ic_back_arrow);
upArrow.setColorFilter(getResources().getColor(R.color.colorWhite), PorterDuff.Mode.SRC_ATOP);
getSupportActionBar().setHomeAsUpIndicator(upArrow);
makeNewCard();
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
// Respond to the action bar's Up/HomeFragment button
case android.R.id.home:
onBackPressed();
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
public void onBackPressed() {
super.onBackPressed();
}
public void onCardClick(View view) {
flipCard();
}
public void onCardFlippedButtonsClick(View view) {
Integer numberPressed;
switch (view.getId()){
case R.id.color_button_1:numberPressed = 1;break;
case R.id.color_button_2:numberPressed = 2;break;
case R.id.color_button_3:numberPressed = 3;break;
case R.id.color_button_4:numberPressed = 4;break;
case R.id.color_button_5:numberPressed = 5;break;
default:numberPressed = 0;
}
saveCardStats(numberPressed);
flingCardAway();
resetForNewCard();
makeNewCard();
}
private void flipCard() {
FrameLayout cardFrame = (FrameLayout) findViewById(R.id.card_frame);
Integer childCount = cardFrame.getChildCount();
Log.d("Simple","childCount: "+childCount);
View cardContainer = findViewById(R.id.card_container);
View cardFace = findViewById(R.id.card_front);
View cardBack = findViewById(R.id.card_back);
FlipAnimation flipAnimation = new FlipAnimation(cardFace, cardBack);
if (cardFace.getVisibility() == View.GONE) {
mFlipCardLinearLayout.setVisibility(View.VISIBLE);
mCardFlippedButtons.setVisibility(View.GONE);
flipAnimation.reverse();
}else{
mFlipCardLinearLayout.setVisibility(View.GONE);
mCardFlippedButtons.setVisibility(View.VISIBLE);
}
cardContainer.startAnimation(flipAnimation);
}
private void saveCardStats(Integer numberPressed){
}
private void flingCardAway(){
mCurrentAnimCardContainer = findViewById(R.id.card_container);
ViewCompat.setTranslationZ(mCurrentAnimCardContainer, 1.0f);
AnimationSet anim = new AnimationSet(true);
RotateAnimation rotate1 = new RotateAnimation(0,-45, Animation.RELATIVE_TO_SELF,0.5f , Animation.RELATIVE_TO_SELF,0.5f );
rotate1.setStartOffset(100);
rotate1.setDuration(500);
anim.addAnimation(rotate1);
TranslateAnimation trans1 = new TranslateAnimation(Animation.RELATIVE_TO_PARENT, -0.1f, Animation.RELATIVE_TO_PARENT, -0.1f, Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 0.0f);
trans1.setDuration(600);
anim.addAnimation(trans1);
AlphaAnimation opacity1 = new AlphaAnimation(1.0f, 0.0f);
opacity1.setDuration(400);
opacity1.setStartOffset(200);
anim.addAnimation(opacity1);
mCurrentAnimCardContainer.setAnimation(anim);
mCurrentAnimCardContainer.setVisibility(View.VISIBLE);
final FrameLayout cardFrame = (FrameLayout) findViewById(R.id.card_frame);
anim.setAnimationListener(new Animation.AnimationListener(){
#Override
public void onAnimationStart(Animation arg0) {
}
#Override
public void onAnimationRepeat(Animation arg0) {
}
#Override
public void onAnimationEnd(Animation arg0) {
//The problem area, also this is not always called
// ((ViewGroup)mCurrentAnimCardContainer.getParent()).removeView(mCurrentAnimCardContainer);
// cardFrame.removeAllViews();
}
});
}
private void resetForNewCard(){
mFlipCardLinearLayout.setVisibility(View.VISIBLE);
mCardFlippedButtons.setVisibility(View.GONE);
}
private void makeNewCard(){
Integer childCount = mCardFrame.getChildCount();
Log.d("Simple","childCount: "+childCount);
LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.study_card, mCardFrame);
}
}
Is it just me or does making an Android app feel like you're constantly swimming against the current in a river.
Edit: If you want to see real improvement, explain why you think this is a bad question rather than just down voting.
I took out onAnimationEnd and just put the removeAllView method outside of it and it worked. Honestly I have no idea how this works. One would imagine that if you remove a View it cannot have an animation work on it.
private void flingCardAway(){
View currentAnimCardContainer = findViewById(R.id.card_container);
ViewCompat.setTranslationZ(currentAnimCardContainer, 1.0f);
AnimationSet anim = new AnimationSet(true);
RotateAnimation rotate1 = new RotateAnimation(0,-45, Animation.RELATIVE_TO_SELF,0.5f , Animation.RELATIVE_TO_SELF, 0.5f );
rotate1.setStartOffset(1);
rotate1.setDuration(400);
anim.addAnimation(rotate1);
TranslateAnimation trans1 = new TranslateAnimation(Animation.RELATIVE_TO_PARENT, -0.0f, Animation.RELATIVE_TO_PARENT, -1.5f, Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, -0.5f);
trans1.setStartOffset(1);
trans1.setDuration(550);
anim.addAnimation(trans1);
AlphaAnimation opacity1 = new AlphaAnimation(1.0f, 0.0f);
opacity1.setStartOffset(300);
opacity1.setDuration(600);
anim.addAnimation(opacity1);
currentAnimCardContainer.startAnimation(anim);
mCardFrame.removeAllViews();
}

Android - RotateAnimation onClick button restart itself

this is the java code to rotate an image when click on button. Image rotate it's perfect but if I click button again when rotation is not ended the animation restart itself, not end the animation. How can I wait the end of the animation? I found Animation.AnimationListener, I think that onAnimationEnd works great for me, but I'm not be able to integrate it in my code... please help me :-)
package com.example.helloword;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.widget.Button;
import android.widget.ImageView;
public class Rotation_test extends Activity {
private float statdegree = (float) 0.0;
private float enddegree = (float) 90.0;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_rotation_test);
// ---------------------------------------------------------------------------------------------
Button buttonRotateCenter = (Button) findViewById(R.id.rotatecenter);
final ImageView floatingImage = (ImageView) findViewById(R.id.floatingimage);
// AnimationRotation
final Animation animationRotateCenter = new RotateAnimation(statdegree,
enddegree, Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 0.5f);
animationRotateCenter.setDuration(5000L);
animationRotateCenter
.setInterpolator(new AccelerateDecelerateInterpolator());
// \AnimationRotation
buttonRotateCenter.setOnClickListener(new Button.OnClickListener() {
public void onClick(View arg0) {
floatingImage.startAnimation(animationRotateCenter);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_rotation_test, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
}
You have to take a boolean variable which tracks whether animation is in progress or not. then use this variable in animation listener and button click as below code
buttonRotateCenter.setOnClickListener(new Button.OnClickListener() {
public void onClick(View arg0) {
if(!anim_in_progress)
floatingImage.startAnimation(animationRotateCenter);
}
});
animationRotateCenter.setAnimationListener(new anim_listener());
}
boolean anim_in_progress=false;
class anim_listener implements AnimationListener
{
#Override
public void onAnimationEnd(Animation arg0) {
anim_in_progress=false;
}
#Override
public void onAnimationRepeat(Animation arg0) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationStart(Animation arg0) {
// TODO Auto-generated method stub
anim_in_progress=true;
}
}
Never tried this, but might work
buttonRotateCenter.setOnClickListener(new Button.OnClickListener() {
public void onClick(View arg0) {
if(!floatingImage.hasTransientState())
floatingImage.startAnimation(animationRotateCenter);
}
});

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));
}
}

How to start activity on animation end

This is my first app and i need to start new activity when the animation ends. what do I need to do? My code:
package com.lineage.goddess;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.TextView;
public class LineageSplashActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
startAnimation();
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
private void startAnimation() {
// TODO Auto-generated method stub
TextView logo1= (TextView) findViewById(R.id.TextView1);
Animation fade1= AnimationUtils.loadAnimation(this, R.anim.fade_in);
logo1.startAnimation(fade1);
TextView logo2= (TextView) findViewById(R.id.TextView2);
Animation fade2= AnimationUtils.loadAnimation(this, R.anim.fade_in);
logo2.startAnimation(fade2);
TextView logo3= (TextView) findViewById(R.id.TextView3);
Animation fade3= AnimationUtils.loadAnimation(this, R.anim.fade_in);
logo3.startAnimation(fade3);
TextView logo4= (TextView) findViewById(R.id.TextView4);
Animation fade4= AnimationUtils.loadAnimation(this, R.anim.fade_in2);
logo4.startAnimation(fade4);}
public void onAnimationEnd() {
Intent i = new Intent( LineageSplashActivity.this, LineageMenuActivity.class );
LineageSplashActivity.this.startActivity( i );
;
}
}
Set an AnimationListener to the animation you want to use to start your Activity.
myAnimation.setAnimationListener(new AnimationListener() {
public void onAnimationStart(Animation animation) {}
public void onAnimationRepeat(Animation animation) {}
public void onAnimationEnd(Animation animation) {
Intent i = new Intent( LineageSplashActivity.this, LineageMenuActivity.class );
LineageSplashActivity.this.startActivity( i );
}
}
So, your code will be like this:
package com.lineage.goddess;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.TextView;
public class LineageSplashActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
startAnimation();
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
private void startAnimation() {
// TODO Auto-generated method stub
TextView logo1= (TextView) findViewById(R.id.TextView1);
Animation fade1= AnimationUtils.loadAnimation(this, R.anim.fade_in);
logo1.startAnimation(fade1);
TextView logo2= (TextView) findViewById(R.id.TextView2);
Animation fade2= AnimationUtils.loadAnimation(this, R.anim.fade_in);
logo2.startAnimation(fade2);
TextView logo3= (TextView) findViewById(R.id.TextView3);
Animation fade3= AnimationUtils.loadAnimation(this, R.anim.fade_in);
logo3.startAnimation(fade3);
TextView logo4= (TextView) findViewById(R.id.TextView4);
Animation fade4= AnimationUtils.loadAnimation(this, R.anim.fade_in2);
face4.setAnimationListener(new AnimationListener() {
public void onAnimationStart(Animation animation) {}
public void onAnimationRepeat(Animation animation) {}
public void onAnimationEnd(Animation animation) {
Intent i = new Intent( LineageSplashActivity.this, LineageMenuActivity.class );
LineageSplashActivity.this.startActivity( i );
}
}
logo4.startAnimation(fade4);
}
}
Your code made my eye's bleed, so I fixed it as much as I could:
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
public class LineageSplashActivity extends Activity implements AnimationListener {
private static final int NUMBER_OF_ANIMATIONS = 4;
private int animationFinishedCount = 0;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
startAnimations();
}
private void startAnimations() {
Animation fade = AnimationUtils.loadAnimation(this, R.anim.fade_in);
fade.setAnimationListener(this);
findViewById(R.id.TextView1).startAnimation(fade);
findViewById(R.id.TextView2).startAnimation(fade);
findViewById(R.id.TextView3).startAnimation(fade);
findViewById(R.id.TextView4).startAnimation(fade);
}
#Override
public void onAnimationEnd(Animation animation) {
// When all animations have finished - start the next activity
if(++animationFinishedCount == NUMBER_OF_ANIMATIONS){
Intent intent = new Intent( this, LineageMenuActivity.class );
startActivity( intent );
}
}
#Override
public void onAnimationStart(Animation animation) {
// Nothing
}
#Override
public void onAnimationRepeat(Animation animation) {
// Nothing
}
}
And if it's not a mis-type and you actually need a different animation for the 4th textview you can remove the count check and just add the animation listener to that individual animation.

How to make a group of clickable graphical object?

my question is how can I make a set of clickable images on Android.
I can make one clickable sphere by using ShapeDrawable, however I want to make more than one at once and still be able to get clicks in all of them.
Moreover I want to position them on screen at my will, instead of auto positioned as with layouts.
The number of spheres can change as can the places.
This is the code I use to make one:
Main.java
package com.teste;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.Toast;
public class Main extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout layout = new LinearLayout(this);
CustomDrawableView mCustomDrawableView = new CustomDrawableView(this, 100, 100, 50, 50, 0xff74AC23);
CustomDrawableView mCustomDrawableView2 = new CustomDrawableView(this, 10, 10, 50, 50, 0xffffffff);
mCustomDrawableView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getBaseContext(), "Clicked green ball", Toast.LENGTH_SHORT).show();
}
});
mCustomDrawableView2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getBaseContext(), "Clicked white ball", Toast.LENGTH_SHORT).show();
}
});
layout.addView(mCustomDrawableView);
layout.addView(mCustomDrawableView2);
setContentView(layout);
}
}
CustomDrawableView.java
package com.teste;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.OvalShape;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
public class CustomDrawableView extends View {
private ShapeDrawable mDrawable;
public CustomDrawableView(Context context, int i, int j, int k, int l, int m) {
super(context);
LayoutParams params = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
super.setLayoutParams(params);
mDrawable = new ShapeDrawable(new OvalShape());
mDrawable.getPaint().setColor(m);
mDrawable.setBounds(i, j, i + k, j + l);
}
protected void onDraw(Canvas canvas) {
mDrawable.draw(canvas);
}
}
Hope you can help :)
It should be as easy as making your clas
CustomDrawableView extends View implements OnClickListener {
// all your code here
public void onClick(View v) {
// your callback function here
}
}
Take a look at one example I wrote some time ago in this answer: Android onClick method doesn't work on a custom view

Categories

Resources