What the title says. At first, I created a method to swap from one image to the other one, and it works. When I add the 2nd method though, to reverse the action and swap back to the first one, nothing changes. It does not even revert from the first image to the second one. Below is the code:
public void fade(View view) {
ImageView laxus = (ImageView) findViewById(R.id.laxus_shirt);
ImageView laxos2 = (ImageView) findViewById(R.id.laxus2);
laxus.animate().alpha(0f).setDuration(1000);
laxos2.animate().alpha(1f).setDuration(1000);
}
public void fadetoblack(View view) {
ImageView laxusius = (ImageView) findViewById(R.id.laxus_shirt);
ImageView laxosios2 = (ImageView) findViewById(R.id.laxus2);
laxosios2.animate().alpha(0f).setDuration(1000);
laxusius.animate().alpha(1f).setDuration(1000);
}
Thank you in advance.
You need to call start() to trigger the animation.
laxus.animate().alpha(0f).setDuration(1000).start();
laxos2.animate().alpha(1f).setDuration(1000).start();
I would suggest you to use Util method to perform crossfading. Something like below
public static void crossFade(View incomingView, View outGoingView, int outGoingViewVisibility) {
outGoingView.setAlpha(1);
ViewCompat.animate(outGoingView).alpha(0).setListener(new ViewPropertyAnimatorListener() {
#Override
public void onAnimationStart(View view) {
}
#Override
public void onAnimationEnd(View view) {
view.setVisibility(outGoingViewVisibility);
}
#Override
public void onAnimationCancel(View view) {
}
}).start();
incomingView.setVisibility(View.VISIBLE);
incomingView.setAlpha(0);
ViewCompat.animate(incomingView).setListener(null).alpha(1).start();
}
Maybe it will be an option to use ViewSwitcher.
Add a viewSwitcher with 2 ImageViews.
<ViewSwitcher xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/switcher"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/laxus"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<ImageView
android:id="#+id/laxus2"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</ViewSwitcher>`
On Activity onCreate add code
ViewSwitcher viewSwitcher=(ViewSwitcher)findViewById(R.id. switcher); // initiate a ViewSwitcher
Animation in = AnimationUtils.loadAnimation(this,android.R.anim.slide_in_left); // load in animation
Animation out = AnimationUtils.loadAnimation(this,android.R.anim.slide_out_right); // load out animation
viewSwitcher.setInAnimation(in); // set in Animation for ViewSwitcher
viewSwitcher.setOutAnimation(out); // set out Animation for ViewSwitcher
When you want to switch between ImageViews call method viewSwitcher.showNext(); or viewSwitcher.showPrevious();
To verify which view is displayed use viewSwitcher.getCurrentView();
Related
I am a hybrid apps developer and new to android. I am trying to fix an android related issue for my hybrid app.
I have the following two methods on my activity: onStart and onPause.
When the app starts I need it to start as usual. When the app is on pause, I need to show an image (my companyLogo). If I set it with setContentView(R.layout.activity_base), the image is shown when the app starts again. Is there a way to dynamically create an image, and show it when app is on pause? Also, how do I remove the image once it starts? Since I am not hands on in Android, I am not sure how to go about it.
I suppose I need to create some sort of a Dialog and set an image view inside it.
Also, I am not sure, how to remove this image, when the app Starts again.
My code snippet:
public void onStart() {
Toast.makeText(getApplicationContext(), "App is in foreground", Toast.LENGTH_LONG).show();
}
public void onPause() {
Toast.makeText(getApplicationContext(), "App is in background", Toast.LENGTH_LONG).show();
setContentView(R.layout.activity_base);
}
This code is im run it. i think u need this.
inside ur activity
ImageView img_view;
TextView txt_show;
int images[] = {R.drawable.ic_launcher, R.drawable.ic_launcher, R.drawable.ic_launcher};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_player);
img_view = (ImageView) findViewById(R.id.img_show);
txt_show = (TextView) findViewById(R.id.txt_my);
}
private void animate(final ImageView imageView, final int images[], final int imageIndex, final boolean forever) {
int fadeInDuration = 1000;
int timeBetween = 5000;
int fadeOutDuration = 1000;
imageView.setVisibility(View.INVISIBLE);
imageView.setImageResource(images[imageIndex]);
Animation fadeIn = new AlphaAnimation(0, 1);
fadeIn.setInterpolator(new DecelerateInterpolator());
fadeIn.setDuration(fadeInDuration);
Animation fadeOut = new AlphaAnimation(1, 0);
fadeOut.setInterpolator(new AccelerateInterpolator());
fadeOut.setStartOffset(fadeInDuration + timeBetween);
fadeOut.setDuration(fadeOutDuration);
AnimationSet animation = new AnimationSet(false);
animation.addAnimation(fadeIn);
animation.addAnimation(fadeOut);
animation.setRepeatCount(1);
imageView.setAnimation(animation);
animation.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
public void onAnimationEnd(Animation animation) {
if (images.length - 1 > imageIndex) {
animate(imageView, images, imageIndex + 1, forever);
} else {
if (forever) {
animate(imageView, images, 0, forever);
}
}
}
#Override
public void onAnimationRepeat(Animation animation) {
}
});
}
#Override
protected void onPause() {
super.onPause();
txt_show.setVisibility(View.GONE);
img_view.setVisibility(View.VISIBLE);
animate(img_view, images, 0, false);
}
#Override
protected void onResume() {
super.onResume();
img_view.setVisibility(View.GONE);
txt_show.setVisibility(View.VISIBLE);
}
inside ur xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/txt_my"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_gravity="center"
android:gravity="center"
android:text="buvan"
android:textColor="#color/colorPrimary" />
<ImageView
android:id="#+id/img_show"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
onResume is called of minimize from cme to screen.
try this one
#Override
protected void onResume() {
super.onResume();
setContentView(R.layout.activity_main);
}
The feature you want will not work without FLAG_SECURE, because when you send the app in the background, the view you are viewing, is an OS-handled view and only OS has the permission to update the view. For that reason, you have to call FLAG_SECURE to notify OS that this view needs to be secured. Otherwise, changing views would have worked.
I think you got the idea.
I'm trying to crossfade two ImageViews by invoking a method when the first image is clicked, then we fade into the second image(alpha set to 0 initially), then I would like to fade back into the first image after clicking on the second image.
It works when crossfading from one image to the other using only one method, but if when I add the other method to crossfade back to the previous image, nothing happens when I click on the image.
public class MainActivity extends AppCompatActivity {
public void narutoFade(View view){
ImageView naruto =(ImageView) findViewById(R.id.naruto);
ImageView narutosage =(ImageView) findViewById(R.id.narutosage);
naruto.animate().alpha(0f).setDuration(2000);
narutosage.animate().alpha(1f).setDuration(2000);
}
public void narutoSageFade(View view) {
ImageView naruto2 = (ImageView) findViewById(R.id.naruto);
ImageView narutosage2 = (ImageView) findViewById(R.id.narutosage);
narutosage2.animate().alpha(0f).setDuration(2000);
naruto2.animate().alpha(1f).setDuration(2000);
}
}
Maybe you should think about declaring both as Fields, declare them only once in onCreate()/onResume(), and write one crossfade method that serves both :
public class MainActivity extends AppCompatActivity {
private ImageView naruto, narutosage;
#Override
protected void onCreate(Bundle savedInstanceState) {
...
naruto =(ImageView) findViewById(R.id.naruto);
narutosage =(ImageView) findViewById(R.id.narutosage);
}
public void crossfade(View fadeIn, View fadeOut) {
fadeIn.animate().alpha(1).setDuration(2000);
fadeOut.animate().alpha(0).setDuration(2000);
}
}
and in your Button onClicks you just call :
crossfade(naruto, narutosage);
or
crossfade(narutosage, naruto);
I have a problem with my android app. I'm using a simple Textview with a vertical scrollBar to display lyrics from a song. The problem is that in my activity I set a Onclick event on this same Textview. So when I scroll the lyrics in the textview, the activity registers a click event when I release my finger from the screen. I don't want the onClick event to happen after I scroll.
Here is what I have done so far but it does not work really well since im using a onLongClick event wich is not precise enough:
public class NowPlayingActivity extends Activity implements ckListener,OnLongClickListener
{
private TextView lyrics;
private static final String TAG_LYRICS = "LYRICS";
#Override
protected void onCreate(Bundle savedInstanceState)
{
this.lyrics = (TextView) this.findViewById(R.id.now_playing_Lyrics);
this.lyrics.setOnClickListener(this);
this.lyrics.setMovementMethod(new ScrollingMovementMethod());
this.lyrics.setOnLongClickListener(this);
}
public void onClick(View v)
{
String tag = (String) v.getTag();
if (tag.equals(NowPlayingActivity.TAG_LYRICS))
{
if (this.scrolled) //this way, the click action doesnt occur after a scroll
{
this.scrolled = false;
}
else
{
this.scrolled = false;
this.artwork.setVisibility(View.VISIBLE);
this.lyrics.setVisibility(View.GONE);
}
}
public boolean onLongClick(View arg0)
{
this.scrolled = true;
return this.scrolled;
}
what can I do to make it more "accurate" (so I dont have to make a longClick for it to work)
thanks!
Put your textview inside scrollview.
<ScrollView
android:id="#+id/content_scroll"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_margin="7dip"
android:scrollbars="none" >
<TextView
android:id="#+id/fileContent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dip" />
</ScrollView>
Then it should work properly. Hope it helps
Well I have an activity which contains nothing but a relative layout with brown background,
and I want to start another activity if the users clicks anywhere on the screen, how would i do that ??
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.mainscreen);
}
First, give ID to your RelativeLayout by putting android:id="#+id/relativeLayout" in your layout file then.
RelativeLayout l = (RelativeLayout) findViewById(R.id.relativeLayout1);
l.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
// startActivity here
}
});
or without using your RelativeLayout just implement the onTouchEvent(MotionEvent) of activity
public boolean onTouchEvent(MotionEvent event)
{
if(event.getAction() == MotionEvent.ACTION_UP)
{
// start activity
}
return super.onTouchEvent(event);
}
Note: Not yet tried this code.
Add onClick attribute to your layout xml, and implement onClick method in your activity to start a new activity.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/myLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:onClick="onClick"
android:orientation="vertical" >
In your activity add an onClick method.
public void OnClick(View v) {
startActivity(new Intent(this,MyNewActivity.class));
}
I think your Mainscreen has Parent layout either Linear,Relative,Frame. Take reference of that and handle click listener.
Ex:
If your Parent layout is Linear.
LinearLayout linear=(LinearLayout)findViewById(R.id.linear);
linear.setOnClickListener(this);
Can anyone tell me what's wrong with this implementation? All I want to do here is have two overlapping views that swap places when you tap the screen. Unless I'm just using it wrong, View.bringToFront() does nothing?
Below is all the code in my app. Note that I added padding to the 'backView' just to make sure the two were actually overlapping. Indeed I could see both on the screen. While tapping the top view does indeed trigger the onClick method, nothing visibly changes in response to the calls to bringToFront.
public class MainActivity extends Activity implements OnClickListener {
private ImageView frontView;
private ImageView backView;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
frontView = (ImageView) findViewById(com.example.R.id.FrontView);
backView = (ImageView) findViewById(com.example.R.id.BackView);
frontView.setOnClickListener(this);
backView.setOnClickListener(this);
backView.setPadding(10,0,0,0);
}
private boolean flag;
public void onClick(View v) {
if (!flag) {
backView.bringToFront();
}
else {
frontView.bringToFront();
}
flag = !flag;
}
}
and the corresponding layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/FrontView"
android:src="#drawable/front"
/>
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/BackView"
android:src="#drawable/back"
/>
</RelativeLayout>
Maybe it's the layout I'm using? I'm not sure... I've tried FrameLayout and LinearLayout as well.
I would try swapping content views instead of ImageViews.
Put each imageView in a different layout and then it is easy:
public void onClick(View v) {
if (!flag) {
setContentView(R.layout.main_front);
frontView = (ImageView) findViewById(com.example.R.id.FrontView);
frontView.setOnClickListener(this);
}
else {
setContentView(R.layout.main_back);
backView = (ImageView) findViewById(com.example.R.id.BackView);
backView.setOnClickListener(this);
}
flag = !flag;
}
There are a couple of Components that you can use that do this for you.
ViewAnimator, ViewFlipper and ViewSwitcher. You can set the animations you require etc and they hand the rest.
here's one example.
http://www.androidpeople.com/android-viewflipper-example/
Given your example, do you have to call invalidate() on the parent after you've called bringToFront() ?