Android -TranslateAnimation not working - android

I am trying to start a shining affect animation on an Image view.
the problem is that the animation does not show on screen.
It is work only when I start it inside a button listener.
this is my shine_effect.xml file:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:centerColor="#beffffff"
android:endColor="#00ffffff"
android:startColor="#00ffffff" />
</shape>
this is my layout xml file:
<?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:background="#drawable/gradient"
android:orientation="vertical">
<RelativeLayout
android:id="#+id/relativeLayout"
android:layout_width="170dp"
android:layout_height="170dp"
android:layout_centerInParent="true">
<ImageView
android:id="#+id/img"
android:layout_width="170dp"
android:layout_height="170dp"
android:contentDescription="#string/desc"
android:src="#drawable/logo" />
<ImageView
android:id="#+id/shine"
android:layout_width="50dp"
android:layout_height="170dp"
android:layout_marginStart="-50dp"
android:contentDescription="#string/desc"
android:src="#drawable/shine_effect" />
</RelativeLayout>
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/relativeLayout"
android:layout_centerHorizontal="true"
android:layout_marginTop="43dp"
android:text="#string/pending"
android:textColor="#android:color/white" />
</RelativeLayout>
and my activity onCreate method:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chat_pending);
img=(ImageView)findViewById(R.id.img) ;
shine=(ImageView)findViewById(R.id.shine) ;
shine_anim=new TranslateAnimation(0, img.getWidth()+ shine.getWidth(),0,
0);
shine_anim.setDuration(550);
shine_anim.setFillAfter(true);
shine_anim.setRepeatMode(Animation.RESTART);
shine_anim.setInterpolator(new AccelerateDecelerateInterpolator());
shine_anim.setRepeatMode(Animation.INFINITE);
shine.startAnimation(shine_anim);
}

It is because the ImageView has not layouted yet so the width is 0. You can give it a layout cycle and start the animation then.
img.getViewTreeObserver().addOnGlobalLayoutListener(
new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
img.getViewTreeObserver().removeOnGlobalLayoutListener(this);
// Create and start animation
}
});
Alternatively if your image width is always static, you can convert e.g. that 170dp to px and use that value when creating the TranslateAnimation instead of using getWidth() methods of views.

Call your animation in onWindowFocusChanged method
#Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
if(hasFocus) {
shine_anim = new TranslateAnimation(0, img.getWidth() + shine.getWidth(), 0,
0);
shine_anim.setDuration(550);
shine_anim.setFillAfter(true);
shine_anim.setRepeatMode(Animation.RESTART);
shine_anim.setInterpolator(new AccelerateDecelerateInterpolator());
shine_anim.setRepeatMode(Animation.INFINITE);
shine.startAnimation(shine_anim);
}
}

Related

How to translate image across the width of relativelayout in android

I'm trying to animate two images one from left to right and other from right to left all at the same time. For this I have set the left margin for left image equal to negative of its width and same for right image so that they are out of the view.
Now in my mainactivity I'm translating them using translationX but nothing is happening.
Here is xml file
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/rLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/bg"
tools:context="com.example.BeX.MainActivity" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="200dp"
android:layout_height="133dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="#drawable/logo" />
<ImageView
android:id="#+id/left"
android:layout_width="100dp"
android:layout_height="75dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_marginLeft="-100dp"
android:src="#drawable/left" />
<ImageView
android:id="#+id/right"
android:layout_width="114dp"
android:layout_height="75dp"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginRight="-114dp"
android:src="#drawable/right" />
</RelativeLayout>
Here is mainactivity.java
import android.app.Activity;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.RelativeLayout;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTheme(R.style.AppBaseTheme1);
setContentView(R.layout.activity_main);
RelativeLayout r = (RelativeLayout)findViewById(R.id.rLayout);
ImageView left = (ImageView)findViewById(R.id.left);
ImageView right = (ImageView)findViewById(R.id.right);
left.animate().translationX(r.getWidth()).setDuration(2000);
right.animate().translationX(-r.getWidth()).setDuration(2000);
}
}
Please tell me what is the correct or possible way to do that
Thanks in advance
There are two base types of Animation in Android.
The first one.
In order to animate translation use this example:
TranslateAnimation r = new TranslateAnimation(0,0,0,200);
r.setDuration(2000L);
r.setRepeatCount(0);
r.setFillAfter(true);
view.startAnimation(r);
The second one.
In your case, you use ViewPropertyAnimator class.
This is the second implementation of animation in Android.
left.animate().translationX(r.getWidth()).setDuration(2000)
In your case you have setup the Object animator but you haven't started it.
try this:
left.animate().translationX(r.getWidth()).setDuration(2000).start() :)
EDIT
This have to work:
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:id="#+id/rLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#null">
<ImageView
android:id="#+id/imageView1"
android:layout_width="200dp"
android:layout_height="133dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="#drawable/aqu" />
<ImageView
android:id="#+id/left"
android:layout_width="100dp"
android:layout_height="75dp"
android:src="#drawable/ar" />
<ImageView
android:id="#+id/right"
android:layout_width="114dp"
android:layout_height="75dp"
android:src="#drawable/ari" />
</RelativeLayout>
Java-file
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.dat);
RelativeLayout r = (RelativeLayout)findViewById(R.id.rLayout);
ImageView left = (ImageView)findViewById(R.id.left);
ImageView right = (ImageView)findViewById(R.id.right);
float distance = 300.f;
TranslateAnimation anim = new TranslateAnimation(0,0,0,distance);
anim.setFillAfter(true);
anim.setDuration(12000L);
left.startAnimation(anim);
}
But anyway if you want to animate using your ide write this:
ObjectAnimator anim0 = ObjectAnimator.ofFloat(right, "translationX", 0,300);
anim0.setDuration(10000L);
anim0.start();
It really works. I've tested.

Android Set Imageview position without anim

I have this Imageview
activity1.xml
<ImageView
android:id="#+id/imageView1"
android:layout_width="match_parent"
android:layout_height="300dp"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:src="#drawable/logobrand"
android:visibility="visible" />
with this result
i animate it with this animation
anim.xml
<?xml version="1.0" encoding="utf-8"?>
<set
xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="#android:anim/linear_interpolator"
android:fillAfter="true">
<translate
android:fromXDelta="0%p"
android:toYDelta="-35%p"
android:duration="1000" />
</set>
So i obtains that :
but in my other activity with how can i have the same position of my ImageView without use animation ?
Activity2.xml
<ImageView
android:id="#+id/imageView1"
android:layout_width="match_parent"
android:layout_height="300dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:src="#drawable/logobrand"
android:visibility="visible" />
Because when i tried i obtains that...
I search to have same position of my imageview between both activities
Thanks
in both activities replace android:layout_height="300dp" to android:layout_height="wrap_content"
and add in second activityandroid:layout_marginto="Xdp" to look like first activity !
activity1.xml
<ImageView
android:id="#+id/imageView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:src="#drawable/logobrand"
android:visibility="visible" />
activity2.xml
<ImageView
android:id="#+id/imageView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="35dp"
android:src="#drawable/logobrand"
android:visibility="visible" />
hope it's help
You can calculate marginTop for your layout (no need to change any thing) using this :
// waiting for layout to be created.
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
// top of your logo when when center vertical
int originalPosition = iV1.getTop();
System.out.println("original position :: " + originalPosition);
// vertical upward shift using your animation property
int verticalShift = (int) (parentView.getHeight() * 0.35f);
System.out.println("vertical shift :: " + verticalShift);
// final margin from top
int marginTop = originalPosition - verticalShift;
System.out.println("actual margin top :: " + marginTop);
// testing it on another view
FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) iV2
.getLayoutParams();
lp.setMargins(0, originalPosition - verticalShift, 0, 0);
iV2.setLayoutParams(lp);
}
}, 100);
I hope this will help.

Animation in Viewpager tab change fadein / fadeout as like Linkedin introduction screen

I want to implement same kind of animation such as linked in does in android application for its Introduction(Login / register) screen.
I am using view pager for Introduction screen and i want to implement fadein fadeout animation on background image change, As per swipe right to left or vice versa.
I want to implement fadein and fadeout animation on background image change according to swipe of screen.
any help is appreciated.
Please take a look at my layout code
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageView
android:id="#+id/background_image"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="centerCrop" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:weightSum="7" >
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginRight="10dp"
android:layout_weight="1"
android:gravity="right"
android:orientation="horizontal" >
<ImageView
android:id="#+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginRight="5dp"
android:src="#drawable/icon_skip" />
<TextView
android:id="#+id/skip_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Skip"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#android:color/white" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="4"
android:gravity="bottom"
android:orientation="vertical" >
<ImageView
android:id="#+id/imageView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="#drawable/logo" />
<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/pager"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:context="com.xyz.View.IntroductionScreen" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="2"
android:gravity="center"
android:orientation="vertical" >
<Button
android:id="#+id/connection_bt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_marginLeft="40dp"
android:layout_marginRight="40dp"
android:background="#drawable/button"
android:text="CONNEXION"
android:textColor="#android:color/white" />
<Button
android:id="#+id/register_bt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="40dp"
android:layout_marginRight="40dp"
android:layout_marginTop="10dp"
android:background="#drawable/button"
android:text="INSCRIPTION"
android:textColor="#android:color/white" />
</LinearLayout>
</LinearLayout>
And View pager fragment layout is
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:id="#+id/text_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="vertical" >
<TextView
android:id="#+id/tagline_tv1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:singleLine="true"
android:text="Laissez votre prochain job"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#android:color/white" />
<TextView
android:id="#+id/details_tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:maxLines="2"
android:text="vous trouver"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#android:color/white" />
</LinearLayout>
</RelativeLayout>
sample Splashs creen this is what i want to implement.
Thank you
This is a lag free one and also handles the Buttons
Main Idea:
1) first create transparent background for your fragments.
2) Create LayerDrawable and add background image of each fragment as an item. Then add your LayerDrawable as a background of your viewpager.
3) in onCreate method set alpha of each layer correctly so just upper one has alpha value of 255.
4) set for each view of your FragmentStatPagerAdapter a tag that corresponds to drawable index that you declared in the LayerDrawable. for example when you open the app FragmentA is showing so its tag must correspond to upper drawable that is 2 (beginning from 0). last page tag must be 0 corresponds to lowest drawable.
5) change drawable of each view at the function transformPage
6) for adding the button use RelativeLayout.
In order to place buttons on top of all views use RelativeLayout. Later children are placing higher on the Z axis. You can see it in the code:
now lets see code:
MainActivity
public class MainActivity extends FragmentActivity {
ViewPager viewPager=null;
int numberOfViewPagerChildren = 3;
int lastIndexOfViewPagerChildren = numberOfViewPagerChildren - 1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewPager = (ViewPager) findViewById(R.id.pager);
viewPager.setAdapter(new MyAdapter(getSupportFragmentManager()));
final LayerDrawable background = (LayerDrawable) viewPager.getBackground();
background.getDrawable(0).setAlpha(0); // this is the lowest drawable
background.getDrawable(1).setAlpha(0);
background.getDrawable(2).setAlpha(255); // this is the upper one
viewPager.setPageTransformer(true, new ViewPager.PageTransformer() {
#Override
public void transformPage(View view, float position) {
int index = (Integer) view.getTag();
Drawable currentDrawableInLayerDrawable;
currentDrawableInLayerDrawable = background.getDrawable(index);
if(position <= -1 || position >= 1) {
currentDrawableInLayerDrawable.setAlpha(0);
} else if( position == 0 ) {
currentDrawableInLayerDrawable.setAlpha(255);
} else {
currentDrawableInLayerDrawable.setAlpha((int)(255 - Math.abs(position*255)));
}
}
});
}
class MyAdapter extends FragmentStatePagerAdapter
{
public MyAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int i) {
Fragment fragment=null;
if(i==0)
{
fragment=new FragmentA();
}
if(i==1)
{
fragment=new FragmentB();
}
if(i==2)
{
fragment=new FragmentC();
}
return fragment;
}
#Override
public int getCount() {
return numberOfViewPagerChildren;
}
#Override
public boolean isViewFromObject(View view, Object object) {
if(object instanceof FragmentA){
view.setTag(2);
}
if(object instanceof FragmentB){
view.setTag(1);
}
if(object instanceof FragmentC){
view.setTag(0);
}
return super.isViewFromObject(view, object);
}
}
}
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/layerdrawable" >
</android.support.v4.view.ViewPager>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="horizontal"
android:layout_marginBottom="48dip" >
<Button
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Sign in"
android:layout_margin="16dip"
android:background="#2ec6e4"
android:textColor="#FFFFFF" />
<Button
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Join us"
android:background="#2ec6e4"
android:layout_margin="16dip"
android:textColor="#FFFFFF"
/>
</LinearLayout>
</RelativeLayout>
LayerDrawable
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
<bitmap
android:id="#+id/Idofbg3"
android:gravity="fill"
android:src="#drawable/bg3" />
</item>
<item>
<bitmap
android:id="#+id/Idofbg2"
android:gravity="fill"
android:src="#drawable/bg2" />
</item>
<item>
<bitmap
android:id="#+id/Idofbg1"
android:gravity="fill"
android:src="#drawable/bg1" />
</item>
</layer-list>
for lazy people who just do not want to declare fragments:
FragmentA
public class FragmentA extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_a,container,false);
return v;
}
}
fragment_a.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:id="#+id/FragmentA"
android:background="#android:color/transparent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="This is Fragment A"
android:textColor="#FFFFFF"
android:id="#+id/textView"
android:gravity="center"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true" />
</RelativeLayout>
Set a ViewPager.PageTransformer to the ViewPager and achieve the desired animation using aplha and translation animation properties.
The most important input is the position parameter passed to transformPage callback. The position value indicates how the view is positioned currently.
Assuming the views in ViewPager are full width, here is how position value need to be interpreted.
------------------------------------------------------------------------------------
position | what does it mean
------------------------------------------------------------------------------------
0 | view is positioned in the center and fully visible to the user.
-1 | view is positioned in the left and not visible to the user.
1 | view is positioned in the right and not visible to the user.
>-1 & <0 | view is being scrolled towards left and is partially visible.
>0 & <1 | view is being scrolled towards right and is partially visible.
------------------------------------------------------------------------------------
mPager.setPageTransformer(true, new ViewPager.PageTransformer() {
#Override
public void transformPage(View view, float position) {
// Ensures the views overlap each other.
view.setTranslationX(view.getWidth() * -position);
// Alpha property is based on the view position.
if(position <= -1.0F || position >= 1.0F) {
view.setAlpha(0.0F);
} else if( position == 0.0F ) {
view.setAlpha(1.0F);
} else { // position is between -1.0F & 0.0F OR 0.0F & 1.0F
view.setAlpha(1.0F - Math.abs(position));
}
// TextView transformation
view.findViewById(R.id.textView).setTranslationX(view.getWidth() * position);
}
});
Here is the layout:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:layout_alignParentTop="true"
android:id="#+id/imageView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/textView" />
</RelativeLayout>
Here is the screen record:

After Slide Up/Down Animation Textview onClickListener not working

I'm using Slide Up/Down Animation for my four TextView. Following is my XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/scrollView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/bg" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/shadow"
android:orientation="vertical"
android:weightSum="2.0" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1.0" >
<TextView
android:id="#+id/textViewblue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_centerInParent="true"
android:layout_marginLeft="25dp"
android:background="#drawable/blue"
android:gravity="center"
android:text="blue" />
<TextView
android:id="#+id/textVieworange"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_centerInParent="true"
android:layout_marginRight="25dp"
android:background="#drawable/orange"
android:gravity="center"
android:text="orange" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1.0">
<TextView
android:id="#+id/textViewpink"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerInParent="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="25dp"
android:background="#drawable/pink"
android:gravity="center"
android:text="pink" />
<TextView
android:id="#+id/textViewgreen"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerInParent="true"
android:layout_alignParentTop="true"
android:layout_marginRight="25dp"
android:background="#drawable/green"
android:gravity="center"
android:text="green" />
</RelativeLayout>
</LinearLayout>
and Following is my Class
public class HomeScreenActivity extends Activity implements OnClickListener {
TextView textViewblue, textVieworange, textViewpink,
textViewgreen;
Animation mAnimation_up, mAnimation_down;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home_screen);
mAnimation_up = new TranslateAnimation(
TranslateAnimation.RELATIVE_TO_SELF, 0f,
TranslateAnimation.RELATIVE_TO_SELF, 0f,
TranslateAnimation.RELATIVE_TO_SELF, 1.6f,
TranslateAnimation.RELATIVE_TO_SELF, 0f);
mAnimation_up.setDuration(1000);
mAnimation_up.setRepeatCount(-1);
mAnimation_up.setRepeatMode(Animation.REVERSE);
mAnimation_up.setInterpolator(new LinearInterpolator());
mAnimation_down = new TranslateAnimation(
TranslateAnimation.RELATIVE_TO_SELF, 0f,
TranslateAnimation.RELATIVE_TO_SELF, 0f,
TranslateAnimation.RELATIVE_TO_SELF, 0f,
TranslateAnimation.RELATIVE_TO_SELF, 1.6f);
mAnimation_down.setDuration(1000);
mAnimation_down.setRepeatCount(-1);
mAnimation_down.setRepeatMode(Animation.REVERSE);
mAnimation_down.setInterpolator(new LinearInterpolator());
initialize();
}
private void initialize() {
textViewblue = (TextView) findViewById(R.id.textViewblue);
textVieworange = (TextView) findViewById(R.id.textVieworange);
textViewpink = (TextView) findViewById(R.id.textViewpink);
textViewgreen = (TextView) findViewById(R.id.textViewgreen);
textViewblue .setOnClickListener(this);
textVieworange .setOnClickListener(this);
textViewpink .setOnClickListener(this);
textViewgreen .setOnClickListener(this);
textViewblue .setAnimation(mAnimation_down);
textViewpink .setAnimation(mAnimation_down);
textVieworange .setAnimation(mAnimation_up);
textViewgreen .setAnimation(mAnimation_up);
}
#Override
public void onClick(View v) {
Intent intentmenu;
switch (v.getId()) {
case R.id.textViewblue:
Toast.makeText(this, "blue", Toast.LENGTH_LONG).show();
break;
case R.id.textVieworange:
Toast.makeText(this, "orange", Toast.LENGTH_LONG).show();
break;
case R.id.textViewpink:
Toast.makeText(this, "pink", Toast.LENGTH_LONG).show();
break;
case R.id.textViewgreen:
Toast.makeText(this, "reeng", Toast.LENGTH_LONG).show();
break;
}
}
}
The problem is that when any TextViewslide for int position means when animation starts the onClickListener works only at its XML position not for its updated position.
Please suggest me the solution for that, how can I detect the updated position for to detect onClickListener on TextView.
Thanks.
Maybe that's a bit complicated to make it. As you're using View animation system, the behavior is under expectation. Although the Views are translated to other places, they're just being
drawn there. From the system's perspective, they're in fact always at the original place where you define in xml file. If you want the click event at the new place, you should use the new property animation system.
The onClickListener is not working because views are shifted from their original place due to animation. To solve this create a same layout below your animation and initially make its Gravity as GONE and when your animation finishes up make its Gravity as Visible and your animation layout visibility as Gone.
also be careful that all the id's of animation and original layout would not be same.

multiple bitmaps drawn while rotating

I have an activity that rotates png image by the SeekBar progress. And it draws me multiple needles. why it does that?
The needle and the speedometer are not perfect, they are just for testings :)
In onCreate:
#Override
public void onCreate(Bundle savedInstanceState) {
...
iv = (ImageView)findViewById(R.id.imageView1);
bMap = BitmapFactory.decodeResource(getResources(), R.drawable.rodykle);
workingBitmap = Bitmap.createBitmap(bMap);
mutableBitmap = workingBitmap.copy(Bitmap.Config.ARGB_8888, true);
yourCanvas = new Canvas(mutableBitmap);
later i use this void
public void drawneedle(){
yourCanvas.rotate(speed, mutableBitmap.getWidth()/2, mutableBitmap.getHeight()/2);
yourCanvas.save(Canvas.MATRIX_SAVE_FLAG);
yourCanvas.drawBitmap(mutableBitmap, 0,0, null);
yourCanvas.restore();
iv.setImageBitmap(mutableBitmap);
}
Layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:mtr="http://schemas.android.com/apk/res/com.mindtherobot.samples.thermometer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="top"
android:orientation="vertical" >
<SeekBar
android:id="#+id/seekBar1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="30dp"
android:paddingRight="30dp"
android:max="270" />
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/spidometras" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="40sp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true" />
</RelativeLayout>
</LinearLayout>
Do you clear the inner circle of the previously drawn content?
You can do that with canvas.drawColor(0, Mode.CLEAR);
Also, in my opinion, you need to add the configChanges flag to your activity's tag in the AndroidManifest.xml file, to prevent onCreate to run more than once when activity rotates.
android:configChanges="orientation|screenLayout|screenSize|smallestScreenSize"
To know that the activity rotated, you need to override the
onConfigurationChanged(Config newConfig)
method and check the new orientation of the newConfig variable

Categories

Resources