1

图像第一次完美移动到其位置,即细节活动图像视图,并且也完美返回到主要活动,但是当我下次单击同一图像时,过渡动画将图像移动到不正确(太高)的细节偏移活动,一旦动画完成,图像就会“变形”到正确的位置。

这是我的 DetailActivity.java 文件:

package com.akshitjain.popularmovies;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.ViewTreeObserver;
import android.widget.ImageView;
import android.widget.TextView;


import com.squareup.picasso.Callback;
import com.squareup.picasso.Picasso;

public class DetailActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_detail);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        postponeEnterTransition();
    }

    ImageView backdropImageView = (ImageView)       findViewById(R.id.detail_image_view);
    final ImageView posterImageView = (ImageView) findViewById(R.id.detail_poster);
    TextView overviewTextView = (TextView) findViewById(R.id.overview_text_view);
    TextView releaseDateTextView = (TextView) findViewById(R.id.release_date);
    TextView userRatingTextView = (TextView) findViewById(R.id.user_rating);
    TextView genreTextView = (TextView) findViewById(R.id.genre);
    genreTextView.setText("");
    Genre genreObject = new Genre();
    String genreName;

    Intent intent = getIntent();

    if (intent != null && intent.hasExtra(Constants.MOVIE_OBJECT_PARCELABLE_EXTRA)) {
        Movies movies = intent.getParcelableExtra(Constants.MOVIE_OBJECT_PARCELABLE_EXTRA);
        int[] genre;
        genre = movies.genre;
        for (int i = 0; i < genre.length; ++i) {
            genreName = genreObject.getGenreName(genre[i]);
            genreTextView.append(genreName);
            if (i < genre.length - 1) {
                genreTextView.append(", ");
            }
        }

        setTitle(movies.originalTitle);
        Picasso.with(getApplicationContext())
                .load((Constants.IMAGE_BASE_URL + Constants.POSTER_SIZE_LARGE).trim() + movies.backdropPath)
                .into(backdropImageView);
        Picasso.with(getApplicationContext())
                .load((Constants.IMAGE_BASE_URL + Constants.POSTER_SIZE_SMALL).trim() + movies.posterPath)
                .into(posterImageView, new Callback() {
                            @Override
                            public void onSuccess() {
                                posterImageView.getViewTreeObserver().addOnPreDrawListener(
                                        new ViewTreeObserver.OnPreDrawListener() {
                                            @Override
                                            public boolean onPreDraw() {
                                                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                                                    posterImageView.getViewTreeObserver().removeOnPreDrawListener(this);
                                                    startPostponedEnterTransition();
                                                }
                                                return true;
                                            }
                                        }

                                );
                            }

                            @Override
                            public void onError() {
                            }
                        }

                );
        overviewTextView.setText(movies.overview);
        releaseDateTextView.setText(movies.releaseDate);
        userRatingTextView.setText(movies.userRating);
    }

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_detail, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    switch (id) {
        case android.R.id.home:
            supportFinishAfterTransition();
            return true;
        case R.id.action_settings:
            return true;
    }
    return super.onOptionsItemSelected(item);
}
}
4

1 回答 1

1

尝试这个。

posterImageView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            Picasso.with(getApplicationContext())
            .load((Constants.IMAGE_BASE_URL + Constants.POSTER_SIZE_SMALL).trim() + movies.posterPath)
            .into(posterImageView, new Callback() {
                        @Override
                        public void onSuccess() {
                             startPostponedEnterTransition();
                        }

                        @Override
                        public void onError() {
                        }
                    });
        }
}
于 2015-12-27T09:22:01.160 回答