I am facing strange Issue
If I click on the image it is rotating for first time,
if you click it again it is not rotating.
I have used toast to check if control is going inside the function, but toast is getting printed all the time.
Why Image is not rotating for second time?
here is my code..
MainActivity:
package com.example.sayantan.myapp1;
import android.annotation.TargetApi;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.animation.Animation;
import android.widget.ImageView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
ImageView imageView;
public void rotateImage(View view) {
Toast.makeText(getApplicationContext(), "in rotateImage()...", Toast.LENGTH_SHORT).show();
imageView.animate().rotation(1800f).setDuration(1500);
// Toast.makeText(getApplicationContext(), "End of rotateImage()", Toast.LENGTH_SHORT).show();
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = (ImageView) findViewById(R.id.imageView);
}
}
Layout XML
<?xml version="1.0" encoding="utf-8"?>
<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:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context="com.example.sayantan.myapp1.MainActivity">
<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:id="#+id/imageView"
android:layout_below="#+id/blue"
android:layout_centerHorizontal="true"
android:layout_marginTop="56dp"
android:src="#drawable/bart"
android:onClick="rotateImage" />
I am pretty sure that the rotation is kept at the end of the animation. So the second time you try to animate the image it doesn't move because it already is rotated to 1800.
So either reset your image to 0 or rotate it to currentRotation + 1800f.
Use rotationBy() instead of rotation();
Have you tried to call Start()?
imageView.animate().rotation(1800f).setDuration(1500).start();
hi you can try with this,
imageView.animate().rotation(1800f).setDuration(1500);
imageView.getAnimation().setRepeatCount(Animation.INFINITE);
Thanks hope this will help you.
Related
I want to show my animated gif image in full size on the splash screen, but it's not working. I also used match parent. Whats the problem here? My xml file is:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_splashscreen"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.foodie.splash.splashscreen">
<com.felipecsl.gifimageview.library.GifImageView
android:id="#+id/gifImageView"
android:scaleType="fitCenter"
android:layout_width="match_parent"
android:layout_marginRight="0dp"
android:layout_marginLeft="0dp"
android:layout_height="match_parent" />
</RelativeLayout>
And this is how it looks:
Java code:
package com.example.foodie.splash;
import android.content.Intent;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ProgressBar;
import com.felipecsl.gifimageview.library.GifImageView;
import org.apache.commons.io.IOUtils;
import java.io.IOException;
import java.io.InputStream;
public class splashscreen extends AppCompatActivity {
private GifImageView gifImageView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splashscreen);
gifImageView = (GifImageView)findViewById(R.id.gifImageView);
//Set GIFImageView resource
try{
InputStream inputStream = getAssets().open("foodie.gif");
byte[] bytes = IOUtils.toByteArray(inputStream);
gifImageView.setBytes(bytes);
gifImageView.startAnimation();
}
catch (IOException ex)
{
}
//Wait for 3 seconds and start Activity Main
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
splashscreen.this.startActivity(new Intent(splashscreen.this,MainActivity.class));
splashscreen.this.finish();
}
},5000); // 3000 = 3second
}
}
you choose a wrong scale type, sett the scaleType as FitXY instead of fit center
but of course it lead to change the scales, find out if it has something like centerCrop scale.
<com.felipecsl.gifimageview.library.GifImageView
android:id="#+id/gifImageView"
android:scaleType="fitXY"
android:layout_width="match_parent"
android:layout_height="match_parent" />
JAVA:
package com.example.scott.testapplication;
import android.app.Activity;
import android.media.Image;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
public class HomeScreen extends Activity {
RelativeLayout Backround;
Button InitButton;
ImageView image;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home_screen);
Backround = (RelativeLayout) findViewById(R.id.Backround);
InitButton = (Button) findViewById(R.id.InitButton);
image = (ImageView) findViewById(R.id.image);
InitButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
/*insert code here*/
}
});
}
}
In order to insert the image, i'm thinking that I need to use a command like ImageView image = new (image), and then use another command to draw it on the display. I dont know which commands I should use.
XML:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/Backround"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin">
<ImageView
android:layout_width ="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/image"
android:scaleType="fitXY"
android:src="#drawable/DESERT2" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Initiate"
android:id="#+id/InitButton"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:layout_gravity="center_vertical" />
</RelativeLayout>
I would like to create the image on the event of the click of a button, I also created the ImageView in the xml file. Not sure if I should change something there.
you can use
image.setImageResource(R.drawable.yourDesiredImageName);
I have managed to get a map to show on the screen. I am trying to create buttons above the map and then add actions to this. E.g. the button will change the location of the map. I am basically trying to adapt my code to this: https://www.youtube.com/watch?v=awX5T-EwLPc. However, I am using "extends Fragment Activity" which messes everything up.
My problem: I cannot create a button above the map, it is placed inside the map. I am trying to create a button above the map, and assign code so I can move to a location on the map etc. Here is my code:
MainActivity.java
package com.example.theapp;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends FragmentActivity implements OnClickListener
{
Button button;
TextView text;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button=(Button)findViewById(R.id.button1);
text=(TextView)findViewById(R.id.textView1);
}
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
text.setText("The button worked!");
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<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" >
<fragment
android:id="#+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/map"
android:layout_alignTop="#+id/map"
android:text="Button" />
<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_alignRight="#+id/map"
android:layout_marginRight="39dp"
android:text="TextView" />
</RelativeLayout>
From the code, my app displays a button on the map and a text view. When you click the button, its meant to change the text, but it doesn't do anything. I have researched tutorials and by the looks of it, I will have to create multiple fragments?
Question: How would I be able to create a button on that page, that will then pin point the user on the map?
Thank you in advance.
You are not setting any functionality to the Button... You should use
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
text.setText("that's how you do it!");
}
});
You need not to worry about "FragmentActivity" until and unless you use fragment class.
Just use this as normal Activity. it will work well.
package com.example.theapp;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends FragmentActivity implements OnClickListener
{
Button button;
TextView text;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button=(Button)findViewById(R.id.button1);
button.setOnClickListener(this);
text=(TextView)findViewById(R.id.textView1);
}
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
text.setText("The button worked!");
}
}
I modified an existing app,GestureBuilder,to accept multiple strokes.Hence to accept the letter A,D, F and so on. I created a simple app which recognises if the letter is correct or not.My problem is:
1.I have successfully recorded multi stroke letter in a file called gesture with gestureBuilder.
2.My application does not accept multi stroke gesture.But i have set fadeoffset to 1000 and gesturestroketype as multiple.I do not know why this is happening.The moment I enter next xtroke to write capital A my previous stroke dissapears.I cant write multi stroke letters/symbols.
Code:
xml file:
<LinearLayout 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"
>
<android.gesture.GestureOverlayView
android:id="#+id/gestures_overlay"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1.0"
android:fadeOffset="5000"
android:eventsInterceptionEnabled="true"
android:gestureStrokeType="multiple" />
</LinearLayout>
java file:
package com.example.gesture_letter;
import java.util.ArrayList;
import android.os.Bundle;
import android.app.Activity;
import android.gesture.Gesture;
import android.gesture.GestureLibraries;
import android.gesture.GestureLibrary;
import android.gesture.GestureOverlayView;
import android.gesture.GestureOverlayView.OnGestureListener;
import android.gesture.GestureOverlayView.OnGesturePerformedListener;
import android.gesture.Prediction;
import android.graphics.Color;
import android.util.Log;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Toast;
public class MainActivity extends Activity implements OnGesturePerformedListener {
GestureLibrary mLibrary;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
GestureOverlayView gestureoverlayview=new GestureOverlayView(this);
View inflate=getLayoutInflater().inflate(R.layout.activity_main,null);
gestureoverlayview.setGestureColor(Color.rgb(0, 255, 0));
gestureoverlayview.setGestureVisible(true);
gestureoverlayview.setUncertainGestureColor(Color.rgb(0,0,255));
gestureoverlayview.addView(inflate);
gestureoverlayview.addOnGesturePerformedListener(this);
mLibrary=GestureLibraries.fromRawResource(this,R.raw.gestures);
if(!mLibrary.load())
{
Log.i("debug","FAILED");
}
else
{
Log.i("debug","LOADED");
}
Log.i("debug","OnCreate");
setContentView(gestureoverlayview);
}
#Override
public void onGesturePerformed(GestureOverlayView arg0, Gesture arg1) {
// TODO Auto-generated method stub
ArrayList<Prediction> predictions=mLibrary.recognize(arg1);
if(predictions.size()>0)
{
Prediction prediction=predictions.get(0);
if(prediction.score>1.0)
{
String s=prediction.name;
Toast.makeText(getApplicationContext(), s,Toast.LENGTH_LONG).show();
}
else
if(prediction.score<1.0)
{
Toast.makeText(getApplicationContext(), "None",Toast.LENGTH_LONG).show();
}
}
}
}
Thank you.
I also would like a good resource for gesture application building.thank you
I do not know why, android:GestureStrokeType="multiple" did not work. It struck me to try it from the java side. I set :
gestureoverlayview.setGestureStrokeType(GestureOverlayView.GESTURE_STROKE_TYPE_MULTIPLE);
gestureoverlayview is an object of GestureOverlayView(you can name it anything)
(in main_activity.java file for newbies)
Then you'll see it works.
I think its a bug, I'm not sure.I spent weeks hovering on this :(
The problem is that you create a new GestureOverlayView :
GestureOverlayView gestureoverlayview=new GestureOverlayView(this);
You should call the view defined in the xml with findViewById(R.id.gesture_overlay);
I have two images overlapping each other, i am animating(sliding) the above image to the right of the screen up to some part of it remaining on the screen so that i can animate it back to its original position, Now i want to get click of the below image to start my new activity but i am getting the click of above image only, and i know that animating the upper image will not move its layout.
But i have tried to move the layouts of upper image but after moving the layouts the above image disappears.
Any help?
After translating the above image,set its clickability to false,like
aboveImage.setClickable(false);
Try the below code ,it will work
AnimationActivity.java:
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
public class AnimationActivity extends Activity {
ImageView aboveImage;
ImageView belowImage;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
aboveImage=(ImageView) findViewById(R.id.image_above);
belowImage=(ImageView) findViewById(R.id.image_below);
aboveImage.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
/*belowImage.setVisibility(View.VISIBLE);
overridePendingTransition(R.anim.move_to_right,R.anim.move_to_left);*/
Animation animation = new TranslateAnimation(0, 100,0, 0);
animation.setDuration(1000);
animation.setFillAfter(true);
aboveImage.startAnimation(animation);
aboveImage.setVisibility(0);
aboveImage.setClickable(false);
}
});
belowImage.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
startActivity(new Intent(getApplicationContext(),Activity2.class));
}
});
}
}
main.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageView
android:id="#+id/image_below"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/cherry" />
<ImageView
android:id="#+id/image_above"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/amalapaul" />
</RelativeLayout>