ClickEvent on animated ImageView fires only at the start position - android

Android:
I have an animated ImageView with an onClickEvent registered. I want the ImageView to move around the screen and then click on the ImageView. But when I click on the moving imageView, nothing happens. When I click at the position of the screen, where the imageView was at the beginning, the clickEvent is fired. So the real position of the ImageView does not move with the animation. Has anybody an idea how to get the event when someone clicks the image which is moving on the screen?
My code so far:
public class OnePlayer extends Activity {
private ImageView imageView;
private int points;
private TextView timeTextView;
private TextView pointsTextView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.oneplayer);
timeTextView = (TextView) findViewById(R.id.time);
timeTextView.setText("01:00");
points = 0;
pointsTextView = (TextView) findViewById(R.id.points);
pointsTextView.setText("0/500");
Animation animation = new TranslateAnimation(0, 100, 0, 200);
animation.setDuration(2000);
animation.setRepeatCount(-1);
animation.initialize(10, 10, 10, 10);
imageView = new ImageView(this);
imageView.setImageResource(R.drawable.plus10);
imageView.setAdjustViewBounds(true);
imageView.setLayoutParams(new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
imageView.startAnimation(animation);
imageView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
v.clearAnimation();
v.setVisibility(View.GONE);
points += 10;
pointsTextView.setText(points + "/500");
}
});
RelativeLayout layout = (RelativeLayout) findViewById(R.id.oneplayergame);
layout.addView(imageView);
}
}

I have shared my solution here. Hope this helps.

Related

ImageSlider + animation views

I have ViewPager + PageAdapter for slide images. I'm trying add Alpha animation button which appears when user tap on screen and disappears when tap again.
When I add clickListner for ImageView or for some invisible layout over ImageView animation button works fine, but slider doesn`t work now. What I should do for combine these 2 functions.
imageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
final Animation aAnim1 = new AlphaAnimation(1.0f, 0.0f);
final Animation aAnim2 = new AlphaAnimation(0.0f, 1.0f);
if (VISIBLE_FLAG == true) {
aAnim1.setDuration(250);
aAnim1.setFillAfter(true);
save_btn.startAnimation(aAnim1);
exit_btn.startAnimation(aAnim1);
VISIBLE_FLAG = false;
}
else {
aAnim1.setDuration(250);
aAnim1.setFillAfter(true);
save_btn.startAnimation(aAnim2);
exit_btn.startAnimation(aAnim2);
VISIBLE_FLAG = true;
}
}
});

After translate animation button click not working

when i click a button the translate animation starts. then button placed where translate animation gets end. it working good but after animation button click not working.
thanks in advance
public class ButtonFragment extends Activity implements OnClickListener{
private Button btn;
private int width;
private Boolean flag = true;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_button_fragment);
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
width = size.x;
btn = (Button) findViewById(R.id.btn);
btn.setOnClickListener(this);
}
#Override
public void onClick(View v) {
TranslateAnimation animation = null;
switch (v.getId()) {
case R.id.btn:
if(flag == true) {
animation = new TranslateAnimation(0, width-92 , 0, 0);
flag=false;
}
else{
animation = new TranslateAnimation(width-92,0 , 0, 0);
flag = true;
}
animation.setDuration(1000);
animation.setFillAfter(true);
btn.startAnimation(animation);
break;
default:
break;
}
}
}
code proper alignment was done
As given in this page:
Another disadvantage of the view animation system is that it only modified where the View was drawn, and not the actual View itself. For instance, if you animated a button to move across the screen, the button draws correctly, but the actual location where you can click the button does not change, so you have to implement your own logic to handle this.
Try using property animation and read the link given above.

put some comment on image and hover it

I need to apply some text on the part of the image where a touch event is occurred. Its just like the Image Tagging on facebook. Please provide some clue how to go about it.
What I've done so far is that I am loading image from gallery and showing it on image viewer.
public class LoadImg extends Activity implements OnTouchListener{
private static int RESULT_LOAD_IMAGE = 1;
float x,y;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
x=0;y=0;
Button buttonLoadImage = (Button) findViewById(R.id.button1);
buttonLoadImage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Intent i = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, RESULT_LOAD_IMAGE);
}
});
Implementing the OnTouchListener, I m locating the x and y coordinates .
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
x=event.getX();
y=event.getY();
return false;
}
But imageviewer doesnt has any method to add text on the coordinates.
I dont want to preset a text/toast. I want to get a edittext/toast to get generated when a user touches the image.
Get the x, y positions using event.getX(), event.getY().
Then, at those positions add a textview to the layout. Like:
TextView tv=new TextView(this);
add layout params and left margin and top margin with those x, y values
LinearLayout.LayoutParams trparams = new
LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
trparams.leftmargin=x;
trparams.topmargin=y;
tv.setLayoutParams(trparams);
then add this textview to the layout.
If you want to do it, you can use a longClickListener and show a toast message with your tip string in it..
Toast viewToast = Toast.makeText(this, "Your tool tip", Toast.LENGTH_SHORT);
yourView.setOnLongClickListener(new OnLongClickListener() {
#Override
public void onLongClick(View v) {
viewToast.show();
}
});

How can I add images to a LinearLayout programmatically?

There is a LinearLayout and some Buttons in my project. I want when each Button clicked add a small image to LinearLayout from drawable. How can I add a image to a LinearLayout programmatically?
This is my LinearLayout in a HorizontalScrollView:
<HorizontalScrollView
android:id="#+id/horizontalScrollView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="top|center"
>
<LinearLayout
android:id="#+id/LinearLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
</LinearLayout>
</HorizontalScrollView>
and here is my activity:
public class MainActivity extends Activity
{
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final LinearLayout notes = (LinearLayout) findViewById(R.id.LinearLayout_notes);
final ImageView notes_do = new ImageView(this);
notes_do.setBackgroundResource(R.drawable.notes_do);
new Thread(new Runnable() {
#Override
public void run() {
final ImageButton img_1 = (ImageButton) findViewById(R.id.img_1_);
img_1.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if( event.getAction() == MotionEvent.ACTION_DOWN ) {
notes.addView(notes_do);
}
return true;
}
}); // end of ontouch
}
}).start(); // end of thread
} // end of oncreate
}// end of activity
LinearLayout ll = (LinearLayout)findViewById(R.id.LinearLayout1);
for(int i=0;i<5;i++)
{
ImageView ii= new ImageView(this);
ii.setBackgroundResource(R.drawable.ic_action_search);
ll.addView(ii);
}
You can do a findViewById to the linearlayout and add an imageview to it dynamically in the onclicklistener of button
LinearLayout linLay = (LinearLayout)findViewById(R.id.LinearLayout1);
ImageView image = new ImageView(this);
//add image to imageview
...
linLay.addView(image);
ImageView img = new ImageView( this);
LinearLayout rl = (LinearLayout) findViewById(R.id.LinearLayout1);
LinearLayout.LayoutParams viewParamsCenter = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
img.setImageResource(R.drawable.ic_launcher);
img.setLayoutParams(viewParamsCenter);
rl.add(img);
As you want to set the image dynamically, on the button's click. you can add the image view to the linear layout
View LinearLayout1 = findViewById(R.id.Layout1);
ImageView image1 = new ImageView(getApplicationContext());
String uri = "#drawable/myresource.png"; // Here you can set the name of
// the image dynamically
int imageResource = getResources().getIdentifier(uri, null,
getPackageName());
Drawable res = getResources().getDrawable(imageResource);
image1.setImageDrawable(res);
((ViewGroup) LinearLayout1).addView(image1);

Imageview jump to start position

i have an imageview
and i move it in timer with:
ImageView kembali = (ImageView) findViewById(idmusuh);
kembali.setVisibility(View.VISIBLE);
RelativeLayout.LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
params.setMargins(5, 0, 0, 0);
kembali.setLayoutParams(params);
so the imagview is move down 5px every timer tick
now if i click the button i want to put the imageview back to start position ..what is the code?
Try this:
kembali.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
RelativeLayout.LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
params.setMargins(0, 0, 0, 0);
kembali.setLayoutParams(params);
}
});
Given that you have moved the ImageView by increasing the margin...

Categories

Resources